How to set up Raspberry Pi as a WiFi access point

Last updated on September 15, 2020 by Dan Nanni

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.

I assume that you have already flashed Raspberry Pi with Raspbian image, and configured it for remote SSH access.

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.

$ lsusb

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.

$ sudo apt-get install hostapd udhcpd zd1211-firmware

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 subnet. Customize the configuration as required.

$ sudo vi /etc/udhcpd.conf
# The start and end of the DHCP lease block

# 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/

# DNS servers that connected devices will use.  Use Google DNS.
opt    dns

# The IP address of the access point
opt    router
opt    subnet
opt    domain   local

# 10 days of lease period
opt     lease   864000

# Optionally specify static lease(s)
#static_lease   00:51:AF:05:B0:05
#static_lease   00:51:AF:00:E1:02

Create an empty DHCP lease file which will automatically be populated by udhcpd later.

$ sudo touch /var/lib/misc/udhcpd.leases

Enable udhcpd permanently by commenting out the following line in /etc/default/udhcpd.

$ sudo vi /etc/default/udhcpd

Set udhcpd to auto-start upon boot.

$ sudo update-rc.d udhcpd enable

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., If /etc/network/interfaces already contains any configuration info for wlan0, you should comment it out first.

$ sudo vi /etc/network/interfaces
auto lo

iface lo inet loopback
iface eth0 inet dhcp

iface wlan0 inet static

#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.

$ sudo vi /etc/hostapd/hostapd.conf
# 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.

$ sudo vi /etc/default/hostapd

Set hostapd server to auto-start upon boot.

$ sudo update-rc.d hostapd enable

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.

$ sudo vi /etc/sysctl.conf

Set up necessary iptables rules.

$ 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

Save the updated iptables rules to a file.

$ sudo sh -c "iptables-save > /etc/iptables.ap"

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

#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.

$ ps aux | grep -E 'hostapd|udhcpd'

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


$ sudo apt-get install zd1211-firmware

2. If you encounter the following error while starting udhcpd, create an empty lease file as follows.

Mar 31 23:06:29 raspberrypi udhcpd[16965]: udhcpd (v1.20.2) started
Mar 31 23:06:29 raspberrypi udhcpd[16965]: can't open '/var/lib/misc/udhcpd.leases': No such file or directory


$ sudo touch /var/lib/misc/udhcpd.leases

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean