Hello everyone. Today I want to show you how you can turn your Raspberry Pi into a Wireless Router. I know there are many posts of this kind out there in the net but I wanted to show you the easiest solution I found on the net so far since it can be quite troublesome to find a matching Wireless adapter and to set up hostapd.
What do you need?
- A Raspberry Pi Model B (plus power adapter, SD card and a LAN cable. And I recommend to set up SSH)
- A Wireless adapter. I used the EDIMAX EW-7811UN Wireless USB Adapter I bought from Amazon (Link here) since it is quite cheap and it worked for me pretty well.
- Raspbian wheezy as Operating System. You can download it from here.
- A (wireless) router to download the necessary software packages.
Hardware Setup
Install the Raspbian wheezy Operating System on your SD card and put it into your Raspberry Pi. Next plug your LAN cable into your Raspberry Pi and connect the other end to your (wireless) router you use to access the internet. As mentioned above I would recommend to enable SSH on your Raspberry Pi so you can configure it without having a monitor, keyboard, etc. ready.
Turn your Pi into a wireless router
Now to the most interesting steps.
The first thing you need to do is to update your Raspberry Pi by running the following commands: (It´s always a good idea to update first to prevent issues with outdated versions, etc.)
sudo apt-get update
sudo apt-get upgrade
Next install the software packages for the DHCP server. (Ignore error messages if you receive some. They are normal since you haven´t configured the DHCP server yet.)
sudo apt-get install isc-dhcp-server
The next step is to install hostapd. Since our wireless adapter (EDIMAX EW-7811UN Wireless USB Adapter) needs a driver not supported by hostapd we need to download a custom version of hostapd (Thanks to this guy (http://jenssegers.be/) we have this simple solution. Please give him some credit for his work.). Simply run the following commands to install this custom version:
wget https://github.com/jenssegers/RTL8188-hostapd/archive/v1.1.tar.gz
tar -zxvf v1.1.tar.gz
cd RTL8188-hostapd-1.1/hostapd
sudo make
sudo make install
The above commands will take some time so please be patient.
Next we need to configure our DHCP server.
sudo nano /etc/dhcp/dhcpd.conf
Find the following lines and place a hashtag (#) in front of them to remove them from the configuration file:
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;
Now search for the following line and remove the hashtag in front of it:
#authoritative;
Now go to the end of the file and add the following configuration:
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.10 192.168.0.250;
option broadcast-address 192.168.0.255;
option routers 192.168.0.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local-network";
option domain-name-servers 8.8.8.8, 8.8.4.4;
}
You can alter the above configuration to fit your needs. The above configuration will set up your DHCP server to hand out IP addresses from 192.168.0.10 to 192.168.0.250 and will set the DNS server to Googles DNS servers 8.8.8.8 and 8.8.4.4.
Don´t forget to save the changes.
Next we need to configure on which Interface our DHCP server will hand out IP addresses. To do so run this command:
sudo nano /etc/default/isc-dhcp-server
Now search for:
INTERFACES=""
And add the name of your WLAN Interface to it like this: (My WLAN Interface is called “wlan0”)
INTERFACES="wlan0"
Next we need to assign a static IP address to out WLAN Interface. To do so run the following command:
sudo nano /etc/network/interfaces
Now assign the IP address you chose as “option routers” in the dhcpd.conf file (in our example: 192.168.0.1) to your WLAN Interface. Your file should now look like this:
auto lo
iface lo inet loopback
iface eth0 inet dhcp
#allow-hotplug wlan0
#iface wlan0 inet manual
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.0.1
netmask 255.255.255.0
Ok. Now we have finished setting up our DHCP server.
For the next step we need to setup hostapd. To do so run the following command:
sudo nano /etc/hostapd/hostapd.conf
Now search for ssid and wpa_passphrase and enter an SSID Name (for example: MyPi) and Password (for example: Password1) for your future wireless network. That´s it. We finished configuring hostapd.
Next we need to enable NAT. First we need to make sure that our Raspberry Pi will forward packages. To do so we run:
sudo nano /etc/sysctl.conf
And search for:
#net.ipv4.ip_forward=1
Remove the hashtag in front of the line and save your changes.
Run the following command to enable the forwarding immediately:
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
Next we need to configure rules to forward all traffic from our WLAN Interface (wlan0) to our cable bound network (eth0) to make sure that Clients connected to our WLAN network can access the internet. To do so run the following commands:
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
Now run the following command to save the rules otherwise they will be lost when you turn off your Raspberry Pi:
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Ok. We finished configuring NAT. The last step is to make sure that all services and configurations we made above will be set up at start so that we do not need to run them every time we start our Raspberry Pi.
To automatically start our DHCP server at start run the following command:
sudo update-rc.d isc-dhcp-server enable
To automatically start hostapd at start run the following command:
sudo update-rc.d hostapd enable
Unfortunately I received the following error:
update-rc.d: using dependency based boot sequencing
update-rc.d: error: no runlevel symlinks to modify, aborting!
If you receive the same error run the following commands:
sudo ln -s /etc/init.d/hostapd /etc/rc2.d/S02hostapd
sudo update-rc.d hostapd enable
After I did that it worked for me. (Thanks to this thread I was able to solve this problem: http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=37898)
Now we need to make sure that the traffic forwarding rules will also get loaded at start. To do so open the interfaces file by running:
sudo nano /etc/network/interfaces
Add the following line to the end of the file:
up iptables-restore < /etc/iptables.ipv4.nat
Ok. We are almost there. Now reboot your Raspberry Pi by running:
sudo reboot
Wait till your Raspberry Pi has rebooted. You should now find a new SSID with the name you chose (for example: MyPi). If everything worked you should be able to connect to it by entering your Password (for example: Password1) and you should be able to access the internet.
Well, that´s it. Your Raspberry Pi is now a wireless router. I hope you liked my post and I hope to see you again next time 🙂
By the way my configuration files are stored here if you want to have a look at them. And please have a look at the sources if you want to know more about this topic.
Sources:
http://raspberrypihq.com/how-to-turn-a-raspberry-pi-into-a-wifi-router/
http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=37898
http://www.maketecheasier.com/set-up-raspberry-pi-as-wireless-access-point
http://www.daveconroy.com/turn-your-raspberry-pi-into-a-wifi-hotspot-with-edimax-nano-usb-ew-7811un-rtl8188cus-chipset/
http://elinux.org/RPI-Wireless-Hotspot
http://jacobsalmela.com/raspberry-pi-and-routing-turning-a-pi-into-a-router/