How to set up Raspberry Pi as a WiFi access point

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.

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

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

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

$ 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
#DHCPD_ENABLED="no"

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., 192.168.0.1). 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
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.

$ 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
DAEMON_CONF="/etc/hostapd/hostapd.conf"

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
net.ipv4.ip_forward=1

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

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

If everything works okay, you should be able to connect to the wireless access point from any device.

Troubleshooting

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

Solution:

$ 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

Solution:

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

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.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.
Your name can also be listed here. Write for us as a freelancer.

14 thoughts on “How to set up Raspberry Pi as a WiFi access point

  1. I have problems with the WiFi signal in hotels to my Android being poor. Can I use this but a second WiFi adaptor to boost/repeat the signal further into the room?

    • You can have one WiFi adapter connected to hotel WiFi, and the other adapter as an AP. But if hotel's WiFi signal is bad, having a separate AP wouldn't help because that AP is again connected to the bad hotel WiFi. It's not a signal booster.

      Instead, if you have access to wired Ethernet in a hotel room, you can hook up the RPi AP to the wired Ethernet to get good quality WiFi signal.

  2. Great article. Can these exact steps be taken on Ubuntu or other Linux distros to establish an access point?

  3. The hostapd, according to the verified peripherals page doesn't work for all usb modules. Though i've always wondered why.

  4. Hi, nice tutorial. I've been looking at various sites on this topic for a few days now and can't seem to find the exact configuration. I'm looking for which is slightly different than the one you have here. Basically I would like to use wlan0 as both the receiver and the AP (i.e. not use lan at all). Reason is I'm using a USB Wifi Antenna to pull signal from public hotspot and idea is to use the raspberry pi in a similar way to something like the Alfa R36 (router/repeater). With a Windows 7 PC, this is easily done with sharing the internet out using the Microsoft Virtual WiFi adapter, so it can be done. I'm just struggling to find anything that covers off how to do it with a raspberry pi :)
    Any suggestions? Thanks

  5. This has been both informative and helpful. I can do this; however, I would like for the Raspberry Pi Access Point use the DHCP Server in my router. I have a Linksys Access Point that works in this manner. Is there some reason this Access Point is operating on a different Subnet? Thanks...

  6. Thanks for this great tutorial!
    Anyway I still have some small problems. I want to use an embedded PC with Ubuntu 12.04 as AP; after struggling a little bit with disabling NetworkManager and setting up the /etc/network/interfaces file (I have 3 network interfaces, 2 wired and 1 wireless), I ended up having the network interfaces up when booting but udhcpd and hostapd daemon do not appear in the ps aux list (checked with " ps aux | grep 'hostapd|udhcpd' " ) and all the interfaces don't have an IP assigned! If I manually assign an IP the WiFi interface keeps its configuration, but then if I manually start hostapd and udhcpd (and I verify they are running with ps aux), I still cannot connect to the wifi network.
    Any clue about possible check to do or possible problem?
    Thanks,
    Andrea

  7. I can't seem to get this to work. Using a fresh install of Raspbian. I am using an Alfa AWUS051NH wireless device which is listed as having AP mode. The only thing I did differently is my hostapd.conf looks like this.
    interface=wlan0
    ssid=Pi
    hw_mode=g
    channel=6
    auth_algs=1
    wmm_enabled=0

    and I had to add "allow-hotplug eth0" to my /etc/network/interfaces

    Anyway, I can see the AP but cannot connect. I get an automatically assigned 169 address and if i set a static IP in the 192.168.0.x range it still does not work.
    Any Ideas?

  8. I have setup as described above and get VERRRY slow internet connections.
    Using speedtest.net, I get (on average):
    ping: 45 ms
    upload: 1.0 Mbits per sec (on average)
    download: 0.5 Mbits per sec (on average)

    using driver=rtl871xdrv

    any way to speed this up?
    I tried installing haveged, but no luck.

    Pat

Leave a comment

Your email address will not be published. Required fields are marked *