Overview
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!
- http://qcktech.blogspot.com/2012/08/raspberry-pi-as-router.html
- http://itsacleanmachine.blogspot.com/2013/02/wifi-access-point-with-raspberry-pi.html
- http://esrlabs.com/android-transporter-for-the-nexus-7-and-the-raspberry-pi/
- http://elinux.org/RPI-Wireless-Hotspot
What you'll need
- Raspberry Pi model B - Ethernet is required
- Ethernet cable
- WiFi adapter - Not all WiFi adapters work, we know for sure it works with the ones in the Adafruit shop!
- SD Card (4GB or greater) with Raspbian on it. You can either DIY it or buy a ready-made Raspbian card
- Power supply for your Pi & a Micro USB cable
- USB Console cable (optional) - this makes it a little easier to
debug the system
- Case for your Pi (optional)
- A SD or MicroSD card reader (optional)
Preparation
Please follow the tutorials in order to
- Install the OS onto your SD card
- Boot
the Pi and configure
Don't forget to change the default password for the 'pi' acccount! - Set
up and test the Ethernet and Wifi connection
- Connect with a USB console cable (optional)
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.
Check Ethernet & Wifi
When it comes back up check with ifconfig -a that you see wlan0 - the WiFi module.
Install software
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)
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.confFind the lines that say
option domain-name "example.org";and change them to add a # in the beginning so they say
option domain-name-servers ns1.example.org, ns2.example.org;
#option domain-name "example.org";Find the lines that say
#option domain-name-servers ns1.example.org, ns2.example.org;
# If this DHCP server is the official DHCP server for the localand remove the # so it says
# network, the authoritative directive should be uncommented.
#authoritative;
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;
- subnet 192.168.42.0 netmask 255.255.255.0 {
- range 192.168.42.10 192.168.42.50;
- option broadcast-address 192.168.42.255;
- option routers 192.168.42.1;
- default-lease-time 600;
- max-lease-time 7200;
- option domain-name "local";
- option domain-name-servers 8.8.8.8, 8.8.4.4;
- }
Run
sudo nano /etc/default/isc-dhcp-serverand scroll down to INTERFACES="" and update it to say INTERFACES="wlan0"
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
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
- iface wlan0 inet static
- address 192.168.42.1
- netmask 255.255.255.0
Assign a static IP address to the wifi adapter by running
sudo ifconfig wlan0 192.168.42.1
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=
- interface=wlan0
- driver=rtl871xdrv
- ssid=Pi_AP
- hw_mode=g
- channel=6
- macaddr_acl=0
- auth_algs=1
- ignore_broadcast_ssid=0
- wpa=2
- wpa_passphrase=Raspberry
- wpa_key_mgmt=WPA-PSK
- wpa_pairwise=TKIP
- rsn_pairwise=CCMP
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
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=1on 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
- sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
- sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
- sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo iptables -t nat -STo make this happen on reboot (so you don't have to type it every time) run
sudo iptables -S
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
up iptables-restore < /etc/iptables.ipv4.natto the very end
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.zipto download the new version (check the next section for how to compile your own updated hostapd) then
unzip adafruit_hostapd.zipto uncompress it. Move the old version out of the way with
sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.ORIGAnd move the new version back with
sudo mv hostapd /usr/sbinset it up so its valid to run with
sudo chmod 755 /usr/sbin/hostapd
First test!
Finally we can test the access point host! Run
sudo /usr/sbin/hostapd /etc/hostapd/hostapd.confTo 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.
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 startyou can always check the status of the host AP server and the DHCP server with
sudo service isc-dhcp-server start
sudo service hostapd statusTo start the daemon services. Verify that they both start successfully (no 'failure' or 'errors')
sudo service isc-dhcp-server status
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
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 Test
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
If you can't connect at all, something is wrong with hostapd
First check what ifconfig (mac/linux) or ipconfig (windows) says. You should have IP address in the 192.168.42.10-50 range
If that doesn't work, something is wrong with hostapd or dhcpd (more likely)
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 hostapd
- 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...
- Download linux 3.4.4_4749
- 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)
- Boot the Pi from the SD card
- sudo mv /boot/RTL8192xC_USB_linux_v3.4.4_4749.20121105.zip .
- unzip RTL8192xC_USB_linux_v3.4.4_4749.20121105.zip
- mv RTL8188C_8192C_USB_linux_v3.4.4_4749.20121105/ rtl
- cd rtl
- cd wpa_supplicant_hostapd
- unzip wpa_supplicant_hostapd-0.8_rtw_20120803.zip
- cd wpa_supplicant_hostapd-0.8/
- cd hostapd
- make
- *have a sandwich*
- when done, hostapd binary is in the directory