Raspberry建立Wifi熱點

OverviewCreated by Ladyada

pi_ap.jpg
Would you like to use your Pi as a WiFi router? Or maybe have it as a special filtering access point? Setting up a Pi as an access point (AP) is a bit more advanced than using it as a client, but its still only a half hour of typing to configure. If you want to, this tutorial will make it so the Pi broadcasts a WiFi service and then routes internet traffic to an Ethernet cable. Since its all Linux you can go in and update or configure it however you like.

I used the following pages as a guide to create this tutorial, please note many of them will not work completely, but check them out if you are interested!
Currently tested working on Raspbian only

What you'll needCreated by Ladyada

You'll need a few things to run this tutorial:
Our Pi starter pack + a Wifi adapter will be all you need and even comes with more fun stuff you can play with

PreparationCreated by Ladyada

This tutorial assumes you have your Pi mostly set up and ready to go.

Please follow the tutorials in order to 

  1. Install the OS onto your SD card
  2. Boot the Pi and configure 
    Don't forget to change the default password for the 'pi' acccount!
  3. Set up and test the Ethernet and Wifi connection
  4. Connect with a USB console cable (optional)
When done you should have a Pi that is booting Raspbian, you can connect to with a USB console cable and log into the Pi via the command line interface.

It is possible to do this tutorial via ssh on the Ethernet port or using a console cable.

If using a console cable, even though the diagram on the last step shows powering the Pi via the USB console cable (red wire) we suggest not connecting the red wire and instead powering from the wall adapter. Keep the black, white and green cables connected as is. 
gpio_closeup.jpg
expandFS.gif
Don't forget to expand the SD card, or you may run out of space!

Check Ethernet & WifiCreated by Ladyada

Before continuing make sure the Ethernet cable is connected in and you can ping out from the Pi
ifconfigtestether.gif
You will also want to set up your WiFi dongle. run sudo shutdown -h now and then plug in the WiFi module when the Pi is off so you don't cause a power surge.

When it comes back up check with ifconfig -a that you see wlan0 - the WiFi module.
ifconfiga.gif

Install softwareCreated by Ladyada

Next up we install the software onto the Pi that will act as the 'hostap' (host access point) You need internet access for this step so make sure that Ethernet connection is up!

 sudo apt-get install hostapd isc-dhcp-server

(You may need to sudo apt-get update if the Pi can't seem to get to the apt-get repositories)
aptgethostapd.gif
(text above shows udhcpd but that doesnt work as well as isc-dhcp-server, still, the output should look similar)

Set up DHCP server


Next we will edit /etc/dhcp/dhcpd.conf, a file that sets up our DHCP server - this allows wifi connections to automatically get IP addresses, DNS, etc.

Run this command to edit the file
 sudo nano /etc/dhcp/dhcpd.conf 
Find the lines that say
 option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

and change them to add a # in the beginning so they say
 #option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;

Find the lines that say
 # If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;

and remove the # so it says
 # If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;



authoritatinve.gif
Then scroll down to the bottom and add the following lines
Copy Code
  1. subnet 192.168.42.0 netmask 255.255.255.0 {
  2. range 192.168.42.10 192.168.42.50;
  3. option broadcast-address 192.168.42.255;
  4. option routers 192.168.42.1;
  5. default-lease-time 600;
  6. max-lease-time 7200;
  7. option domain-name "local";
  8. option domain-name-servers 8.8.8.8, 8.8.4.4;
  9. }
iscdhcpconf.gif
Save the file by typing in Control-X then then return

Run
 sudo nano /etc/default/isc-dhcp-server
and scroll down to INTERFACES="" and update it to say INTERFACES="wlan0" 
dhcpwlan0.gif

Set up wlan0 for static IP


If you happen to have wlan0 active because you set it up, run sudo ifdown wlan0
There's no harm in running it if you're not sure
ifdownwlan0.gif
Next we will set up the wlan0 connection to be static and incoming. run sudo nano /etc/network/interfaces to edit the file

Find the line auto wlan0 and add a # in front of the line, and in front of every line afterwards. If you don't have that line, just make sure it looks like the screenshot below in the end! Basically just remove any old wlan0configuration settings, we'll be changing them up

Depending on your existing setup/distribution there might be more or less text and it may vary a little bit

Add the lines
Copy Code
  1. iface wlan0 inet static
  2. address 192.168.42.1
  3. netmask 255.255.255.0
After allow hotplug wlan0 - see below for an example of what it should look like. (ignore our hyphen in allow-hotplug tho, its a typo!) Any other lines afterwards should have a # in front to disable them
staticip.gif
Save the file (Control-X Y <return>) 

Assign a static IP address to the wifi adapter by running 
sudo ifconfig wlan0 192.168.42.1
ifconfigwlan0.gif

Configure Access Point


Now we can configure the access point details. We will set up a password-protected network so only people with the password can connect.

Create a new file by running sudo nano /etc/hostapd/hostapd.conf

Paste the following in, you can change the text after ssid= to another name, that will be the network broadcast name. The password can be changed with the text after wpa_passphrase=
Copy Code
  1. interface=wlan0
  2. driver=rtl871xdrv
  3. ssid=Pi_AP
  4. hw_mode=g
  5. channel=6
  6. macaddr_acl=0
  7. auth_algs=1
  8. ignore_broadcast_ssid=0
  9. wpa=2
  10. wpa_passphrase=Raspberry
  11. wpa_key_mgmt=WPA-PSK
  12. wpa_pairwise=TKIP
  13. rsn_pairwise=CCMP
If you are not using the Adafruit wifi adapters, you may have to change the driver=rtl871xdrv to saydriver=nl80211 or something, we don't have tutorial support for that tho, YMMV!
edithostapdconf.gif
Save as usual. Make sure each line has no extra spaces or tabs at the end or beginning - this file is pretty picky!

Now we will tell the Pi where to find this configuration file. Run sudo nano /etc/default/hostapd

Find the line #DAEMON_CONF="" and edit it so it says DAEMON_CONF="/etc/hostapd/hostapd.conf"
Don't forget to remove the # in front to activate it!

Then save the file
hostapdconf.gif

Configure Network Address Translation(如果需要AP需要上網則需要這步)


Setting up NAT will allow multiple clients to connect to the WiFi and have all the data 'tunneled' through the single Ethernet IP. (But you should do it even if only one client is going to connect)

Run sudo nano /etc/sysctl.conf

Scroll to the bottom and add 
 net.ipv4.ip_forward=1
on a new line. Save the file. This will start IP forwarding on boot up

Also run 
 sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
to activate it immediately

sysctrl.gif
Run the following commands to create the network translation between the ethernet port eth0 and the wifi portwlan0
Copy Code
  1. sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  2. sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
  3. sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
You can check to see whats in the tables with
 sudo iptables -t nat -S
sudo iptables -S
To make this happen on reboot (so you don't have to type it every time) run

 sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
iptables.gif
run sudo nano /etc/network/interfaces and add 
 up iptables-restore < /etc/iptables.ipv4.nat
to the very end


etcnetworkinterface.gif

Update hostapd(上面的hostapd爲x86架構的,如果在Pi上使用需要換爲ARM架構)


Before we can run the access point software, we have to update it to a version that supports the WiFi adapter.
First get the new version by typing in 
 wget http://www.adafruit.com/downloads/adafruit_hostapd.zip 
to download the new version (check the next section for how to compile your own updated hostapd) then 
 unzip adafruit_hostapd.zip 
to uncompress it. Move the old version out of the way with 
 sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.ORIG 
And move the new version back with 
 sudo mv hostapd /usr/sbin
set it up so its valid to run with
 sudo chmod 755 /usr/sbin/hostapd

swaphostapd.gif

First test!


Finally we can test the access point host! Run
 sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf
To manually run hostapd with our configuration file. You should see it set up and use wlan0 then you can check with another wifi computer that you see your SSID show up. If so, you have successfully set up the access point.
testap.gif
If you get an INVALID ARGUMENT warning from hostapd, you may need to compile your own version of hostapd - there's instructions here http://forums.adafruit.com/viewtopic.php?f=19&t=47716#p240781
pi_ap.gif
You can try connecting and disconnecting from the Pi_AP, debug text will display on the Pi console but you won't be able to connect through to the Ethernet connection yet. 
Cancel the test by typing Control-C in the Pi console to get back to the Pi command line

Finishing up!


OK now that we know it works, time to set it up as a 'daemon' - a program that will start when the Pi boots.
Run the following commands
 sudo service hostapd start 
sudo service isc-dhcp-server start
you can always check the status of the host AP server and the DHCP server with
 sudo service hostapd status
sudo service isc-dhcp-server status
To start the daemon services. Verify that they both start successfully (no 'failure' or 'errors')
Then to make it so it runs every time on boot
 sudo update-rc.d hostapd enable 
sudo update-rc.d isc-dhcp-server enable
updaterc.gif
Extra: Removing WPA-Supplicant

Depending on your distro, you may need to remove WPASupplicant. Do so by running this command:

sudo mv /usr/share/dbus-1/system-services/fi.epitest.hostap.WPASupplicant.service ~/

and then rebooting (sudo reboot)

Connect and TestCreated by Ladyada

Now that we have the software installed on a Pi, it's time to connect to it and test the connection. I'm using a Windows computer but any kind should work fine

On the Pi, run the command tail -f /var/log/syslog to watch the system log data, handy for checking and debugging whats going on!

Connect with another computer to the AP you made in the previous step
connectAP.gif
Enter the WPA key you specified in the previous step
key.gif
connecting.gif
In the Pi syslog you should see stuff like this! It indicates that a client connected, at what time and what IP address was given to them

If you can't connect at all, something is wrong with hostapd
tailf.gif
On your computer, open up a Terminal (mac/linux) or Start->Run->cmd to open up a command line

First check what ifconfig (mac/linux) or ipconfig (windows) says. You should have IP address in the 192.168.42.10-50 range
ipconfig.gif
Try pinging the Pi, its address is 192.168.42.1 - on windows it will ping 3 times and quit. On mac/linux press Control-C to quit after a few pings. You should get successful pings as seen below

If that doesn't work, something is wrong with hostapd or dhcpd (more likely)
ping192.gif
Next try pinging 8.8.8.8, if this doesn't work but the previous does, something is wrong with dhcpd or the NAT configuration (more likely)
ping8888.gif
Finally, we'll check that DNS works, try pinging www.mit.edu. If this doesn't work, something is wrong withdhcpd

If everything is good so far, try browsing the internet, sending email, etc. You are now using your Pi as a Wifi Router!

More!


Its possible to set up your router for open or WEP access, but we don't cover that here (and it's not as secure!) You might want to search around for tutorials such as this one that cover hostapd options

Compiling hostapdCreated by Ladyada

You may have noticed that one step is downloading a copy of hostapd from adafruit.com and swapping it with yours. In case you want to compile your own, here's how (its easy but not necessary if you are OK with using our binary)

  1. Go to the Realtek downloads page http://152.104.125.41/downloads/downloadsView.aspx?Langid=1&PNid=21&PFid=48&Level=5&Conn=4&ProdID=27...
  2. Download linux 3.4.4_4749
  3. Copy the zip to the SD card using any computer which will place it in the Pi's /boot directory (or somehow get that file onto your Pi)
  4. Boot the Pi from the SD card
  5. sudo mv /boot/RTL8192xC_USB_linux_v3.4.4_4749.20121105.zip .
  6. unzip RTL8192xC_USB_linux_v3.4.4_4749.20121105.zip
  7. mv RTL8188C_8192C_USB_linux_v3.4.4_4749.20121105/ rtl
  8. cd rtl
  9. cd wpa_supplicant_hostapd
  10. unzip wpa_supplicant_hostapd-0.8_rtw_20120803.zip
  11. cd wpa_supplicant_hostapd-0.8/
  12. cd hostapd
  13. make
  14. *have a sandwich*
  15. when done, hostapd binary is in the directory
=========================================================================
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章