This is a bit special because it is a “checklist” or “actionlist” in order to turn my Raspberry Pi 2 model B into a WiFi router. I bought a starter kit with a WiFi dongle as this one to achieve this goal without having to worry about component compatibility and to have a Rasbian version already installed.
Once you have received your Raspberry Pi, and connected all your devices (hdmi, keyboard, mouse, Wifi dongle, SD card and the power supply), branch it to your internet router with an ethernet cable. Open a terminal and enter these commands:
$ apt-get update
$ apt-get upgrade
Now we are ready to start our configuration.
Before to start
If like me you don’t have a QWERTY keyboard and you don’t live in the UTC+0 timezone you will need to change that to make your experience more confortable.
To update the keyboard layout follow these instructions:
$ sudo nano /etc/default/keyboard
Find the key/value: XKBLAYOUT="gb"
and replace the gb value with your country code (e.g. fr for France). Once the file is modified, if you use nano, enter the CTRL+O
to save the modification and then CTRL+X
to quit.
To update your timezone enter the following command and follow the instructions:
$ sudo tzselect
Let’s install the router software.
Installation
In order to turn the Raspberry Pi as a WiFi router and access point you need to install some utilities:
- hostapd: a user space daemon for wireless access point and authentication servers. In simple words, hostapd turns your Raspberry Pi into a WiFi access points allowing decent amount of configuration options (for security for example).
- isc-dhcp-server: an implementation of the Internet Systems Consortium’s DHCP server. A DHCP server is responsible for assigning addresses automatically to computers and devices that connect to the access point.
To start we are going to install the hostapd deamon. Like most of the WiFi dongle sold with the Raspberry Pi are based on the RTL8188CUS chipset, we need to install a custom version of hostapd:
$ cd /tmp
$ sudo wget https://github.com/jenssegers/RTL8188-hostapd/archive/v1.1.tar.gz
$ sudo tar -zxvf v1.1.tar.gz
$ cd RTL8188-hostapd-1.1/wpa_supplicant
$ sudo make
$ sudo make install
$ cd ../hostapd
$ sudo make
$ sudo make install
The build can take some time, so just be patient. 🙂
Then, to install the isc-dhcp-server utility run the following command:
$ sudo apt-get install isc-dhcp-server
Once finished we can go to the next: the configuration.
Configuration
The DHCP server
To configure the DHCP server open the file dhcpd.conf:
$ sudo nano /etc/dhcp/dhcpd.conf
Next I want the DCHP server to attribute ip from 10.0.1.10
to 10.0.1.254
with a 255.255.255.0
netmask. To do so you can simply replace the content of the file with the following one:
ddns-update-style none;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-facility local7;
subnet 10.0.1.0 netmask 255.255.255.0 {
range 10.0.1.10 10.0.1.254;
option broadcast-address 10.0.1.255;
option routers 10.0.1.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;
}
Press CTRL+O
then CTRL+X
to save the file.
Next file to edit is isc-dhcp-server:
$ sudo nano /etc/default/isc-dhcp-server
Find the line with the key INTERFACES
and add the wlan0
value:
INTERFACES="wlan0"
This will make the DHCP server hand out network addresses on the wireless interface. Save the file and exit nano.
In order to make the DHCP server effective we need to configure a static ip address for the wireless network adapter. Start by putting down the WLAN interface:
$ sudo ifdown wlan0
Then open the file /etc/network/interfaces:
$ sudo nano /etc/network/interfaces
Replace its content by the following one:
auto lo
iface lo inet loopback
auto eth0
allow-hotplug eth0
iface eth0 inet manual
allow-hotplug wlan0
iface wlan0 inet static
address 10.0.1.2
netmask 255.255.255.0
This configuration will make the wireless adapter take the address 10.0.1.2
in our new local network.
Now let’s configure the access point thanks to the hostapd daemon.
The access point
To configure the hostapd daemon, open the file called hostapd.conf:
$ sudo nano /etc/hostapd/hostapd.conf
Update the ssid
and wpa_passphrase
values to define the SSID Wifi name and WPA password of your WiFi:
ssid=My Wifi Hotspot Name
wpa_passphrase=TheP4ssword$
That’s all for the access point configuration. Just think to test the configuration and reboot your Raspberry Pi:
$ sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf
$ sudo reboot
Starting your WiFi router
You are now ready to start the DHCP server and the access point application. To test everything is ok just run these commands:
$ sudo /etc/init.d/isc-dhcp-server start
$ sudo /etc/init.d/hostapd start
If everything is ok you should be able to find your wireless network in the list of your smart phone or laptop!
So this is the time to start these services when the Raspberry Pi boot. It’s very simple, just enter the following commands:
$ sudo update-rc.d hostapd defaults
$ sudo update-rc.d isc-dhcp-server defaults
Conclusion
I tried to make it as simple as possible in order to make your (and my next) installations as simple as possible.
In the next chapter I’ll do the same this in order to install mongodb and nodejs on a Raspberry Pi 2.
If you have any question, let a comment below!
J’ai suivi pas à pas le tuto. Je vois bien mon SSID, lorsque je veux me connecter il accepte bien mon password mais ensuite me rejete. J’ai essayer depuis un PC android et un PC windows. Mon rapsberry est connecté en ETH0 sur la freebos (192.168) et mon WLAN0 est en (10.0). Une piste pour résoudre mon souci ?
Hello, many thanks for this tutorial!
I set up everything, the hotspot is found by my devices but I can’t connect them to it.
How can I move?
Just a quick question: Is the Raspberry Pi suitable for running continuously, 24/7, as a wifi router? I mean..what is the average sd card lifespam in here? Supposing i only use wifi for 2 hours a day (no videos/mp3’s/images – only internet browsing and some apps that use data). Thanks!
Can I use a wlan1 instead of the eth0 ?
what I need is a wifi to wifi router, have my raspberry connect to a wifi network (using wlan1) that i can manually specify, and then become an wifi AP (using wlan0) for many other raspberry pi’s.
thanks in advance.
Mine says that the driver isn’t recognized or something like that any help
Thank for information!
i have succeeded with connection my raspberry Pi 3 and my laptop.
Although Raspberry Pi is connected with my ethernet cable, I cannot access the internet with Pi. Is there any way to resolve this problem?
adding one more question,
Although my laptop is connected to “Pi_AP”(ssid that i setted up).
I can’t access to RPi through putty.
I write 10.0.1.2 in the box, but no response.
I can see my inet address is “10.0.1.2” as a result of ‘ifconfig’ command