There are a number of useful Raspberry Pi (RPi) projects out there. One interesting use case is to turn Raspberry Pi into a WiFi access point. The advantage of having a RPi-powered WiFi access point is that you will have ultimate control and customization of the access point, thanks to the flexibility of the mainline Linux which powers the RPi board.
In this tutorial, I will demonstrate how to build a wireless access point using Raspberry Pi. The configured wireless access point will have its own built-in DHCP service. I am going to use Raspbian image for Raspberry Pi in this project.
Necessary Hardware Components
You will need the following three items to build a RPi-based WiFi access point.
- Raspberry Pi model B: You need an Ethernet port on Raspberry Pi to connect it to wired Ethernet.
- USB wireless Ethernet adapter: I used Belkin F5D7050 Wireless 802.11G.
- Micro USB charger: I used EasyAcc USB charger which offers a proper voltage and enough amps for Raspberry Pi and USB Ethernet adapter.
Step One: Check the Status of USB Ethernet Adaptor
Plug the USB wireless Ethernet adapter into Raspberry Pi's USB port, connect Raspberry Pi to wired Ethernet through its Ethernet port, and finally power it on. Then remotely connect to Raspberry Pi over SSH.
Use lsusb command to check if the USB Ethernet adapter is successfully detected by Raspberry Pi.
Your Ethernet adapter should appear in the USB device list as shown above.
Step Two: Install Necessary Packages
Next, install the following packages with apt-get.
hostapd is an access-point server which supports IEEE 802.11 and IEEE 802.1X/WPA/WPA2/EAP authentication. udhcpd is a lightweight DHCP server which is typically used on embedded systems. Finally, zd1211 is a firmware driver used by hostapd.
Step Three: Configure DHCP Server
The next step is to configure udhcpd DHCP server.
Edit /etc/udhcpd.conf to configure DHCP settings. The following is a sample configuration file. With this configuration, a DHCP server will manage a separate 192.168.0.0/24 subnet. Customize the configuration as required.
# The start and end of the DHCP lease block start 192.168.0.20 end 192.168.0.254 # The wireless interface used by udhcpd interface wlan0 # If remaining is true (default), udhcpd will store the time # remaining for each lease in the udhcpd leases file. This is # for embedded systems that cannot keep time between reboots. remaining yes # The location of DHCP lease file lease_file /var/lib/misc/udhcpd.leases # The location of the pid file pidfile /var/run/udhcpd.pid # DNS servers that connected devices will use. Use Google DNS. opt dns 22.214.171.124 126.96.36.199 # The IP address of the access point opt router 192.168.0.1 opt subnet 255.255.255.0 opt domain local # 10 days of lease period opt lease 864000 # Optionally specify static lease(s) #static_lease 00:51:AF:05:B0:05 192.168.0.100 #static_lease 00:51:AF:00:E1:02 192.168.0.110
Create an empty DHCP lease file which will automatically be populated by udhcpd later.
Enable udhcpd permanently by commenting out the following line in /etc/default/udhcpd.
Set udhcpd to auto-start upon boot.
Now, run ifconfig to obtain the name of a wireless interface. Let's assume that the wireless interface name is wlan0.
Edit /etc/network/interfaces to assign a static IP address to wlan0. This IP address should be the same as the one that you defined in /etc/udhcpd.conf for the access point (e.g., 192.168.0.1). If /etc/network/interfaces already contains any configuration info for wlan0, you should comment it out first.
auto lo iface lo inet loopback iface eth0 inet dhcp iface wlan0 inet static address 192.168.0.1 netmask 255.255.255.0 #allow-hotplug wlan0 #iface wlan0 inet manual #wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf #iface default inet dhcp
Step Four: Configure HostAPD Server
To configure HostAPD server, create a following configuration file.
# interface used by access point interface=wlan0 # firmware driver driver=nl80211 # access point SSID ssid=rpiap # operation mode (a = IEEE 802.11a, b = IEEE 802.11b, g = IEEE 802.11g) hw_mode=g # access point channel channel=6 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 # key management algorithm wpa_key_mgmt=WPA-PSK wpa_passphrase=mypasscode wpa=2 # set ciphers wpa_pairwise=TKIP rsn_pairwise=CCMP
Edit /etc/default/hostapd to point to the above configuration file.
Set hostapd server to auto-start upon boot.
Step Five: Configure NAT Forwarding
The last stage is to set up NAT forwarding rules for the access point, so that the access point can communicates with external networks on behalf of all connected devices.
Enable IP forwarding.
Set up necessary iptables rules.
$ 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
Save the updated iptables rules to a file.
Edit /etc/network/interfaces to append the following line to the end of the file, so that the iptables rules will automatically be applied upon start.
up iptables-restore < /etc/iptables.ipv4.nat
In the end, /etc/network/interfaces will look like the following.
auto lo iface lo inet loopback iface eth0 inet dhcp iface wlan0 inet static address 192.168.0.1 netmask 255.255.255.0 #allow-hotplug wlan0 #iface wlan0 inet manual #wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf #iface default inet dhcp up iptables-restore < /etc/iptables.ap
Step Six: Reboot
Finally, reboot Raspberry Pi.
After rebooting, verify that both hostapd and udhcpd are running in the background.
If everything works okay, you should be able to connect to the wireless access point from any device.
1. If you encounter the following error while starting hostapd, make sure that you have installed the zd1211 firmware driver.
raspberrypi kernel: [ 802.760824] usb 1-1.2: Could not load firmware file zd1211/zd1211b_ub. Error number -2
2. If you encounter the following error while starting udhcpd, create an empty lease file as follows.
Mar 31 23:06:29 raspberrypi udhcpd: udhcpd (v1.20.2) started Mar 31 23:06:29 raspberrypi udhcpd: can't open '/var/lib/misc/udhcpd.leases': No such file or directory
Subscribe to Xmodulo
Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.