Configure Raspberry Pi 3 as a WiFi Access Point (AP)

The tutorial below is for Raspberry Pi 3 ONLY.

One of my thoughts was, can I use it as a SoftAP for some ESP8266 sensor nodes? As it turns out, you can, and it’s not that difficult, as the BCM43438 chip is supported by the open-source brcmfmac driver!


The first step is to install the required packages: sudo apt-get install dnsmasq hostapd

I’ll go into a little detail about the two:

  • hostapd – This is the package that allows you to use the built in WiFi as an access point
  • dnsmasq – This is a combined DHCP and DNS server that’s very easy to configure

If you want something a little more ‘heavyweight’, you can use the isc-dhcp-server and bind9 packages for DHCP and DNS respectively, but for our purposes, dnsmasq works just fine.

Configure your interfaces

The first thing you’ll need to do is to configure your wlan0 interface with a static IP.

If you’re connected to the Pi via WiFi, connect via ethernet/serial/keyboard first.

In newer Raspbian versions, interface configuration is handled by dhcpcd by default. We need to tell it to ignore wlan0, as we will be configuring it with a static IP address elsewhere. So open up the dhcpcd configuration file with sudo nano /etc/dhcpcd.conf and add the following line to the very TOP of the file:

denyinterfaces wlan0  

Note: This must be ABOVE any interface lines you may have added!

Now we need to configure our static IP. To do this open up the interface configuration file with sudo nano /etc/network/interfaces and edit the wlan0 section so that it looks like this:

allow-hotplug wlan0  
iface wlan0 inet static  
#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

#this is what the old version looks like
#source-directory /etc/network/interfaces.d

#auto lo
#iface lo inet loopback
#iface eth0 inet manual

#allow-hotplug wlan0
#iface wlan0 inet manual
#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Restart dhcpcd with sudo service dhcpcd restart and then reload the configuration for wlan0 with sudo ifdown wlan0; sudo ifup wlan0.

Configure hostapd

Next, we need to configure hostapd. Create a new configuration file with sudo nano /etc/hostapd/hostapd.conf with the following contents:

# This is the name of the WiFi interface we configured above

# Use the nl80211 driver with the brcmfmac driver

# This is the name of the network

# Use the 2.4GHz band

# Use channel 6

# Enable 802.11n

# Enable WMM

# Enable 40MHz channels with 20ns guard interval

# Accept all MAC addresses

# Use WPA authentication

# Require clients to know the network name

# Use WPA2

# Use a pre-shared key

# The network passphrase

# Use AES, instead of TKIP

We can check if it’s working at this stage by running sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf. If it’s all gone well thus far, you should be able to see to the network Pi3-AP! If you try connecting to it, you will see some output from the Pi, but you won’t receive and IP address until we set up dnsmasq in the next step. Use Ctrl+C to stop it.

We aren’t quite done yet, because we also need to tell hostapd where to look for the config file when it starts up on boot. Open up the default configuration file with sudo nano /etc/default/hostapd and find the line #DAEMON_CONF="" and replace it with DAEMON_CONF="/etc/hostapd/hostapd.conf".

Configure dnsmasq

The shipped dnsmasq config file contains a wealth of information on how to use it, but the majority of it is largely redundant for our purposes. I’d advise moving it (rather than deleting it), and creating a new one with

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig  
sudo nano /etc/dnsmasq.conf  

Paste the following into the new file:

interface=wlan0      # Use interface wlan0  
listen-address= # Explicitly specify the address to listen on  
bind-interfaces      # Bind to the interface to make sure we aren't sending things elsewhere  
server=       # Forward DNS requests to Google DNS  
domain-needed        # Don't forward short names  
bogus-priv           # Never forward addresses in the non-routed address spaces.  
dhcp-range=,,12h # Assign IP addresses between and with a 12 hour lease time  


Note: If you restart the dnsmasq service here (i.e. without doing the port forwarding below),  you will turn this into a mobile AP ONLY. That means Raspberry Pi could be used as a router or web server and listen to some port…

$sudo service dnsmasq restart

To restore the previous wifi setting (o disable the AP mode reliably), you’ll need to work on the interface and hostapd ONLY.

In particular,

1. Comment out the line denyinterface wlan0 in /etc/ahcpcd.conf, such that interfaces will evaluate wlan0

2. Restore the original interface file, that is /etc/network/interfaces. 

3.  Comment out the line DAEMON_CONF=”/etc/hostapd/hostapd.conf” in /etc/default/hostapd, such that hostapd configuration will be missed.

Missing anyone of these three (e.g., the last one) will lead to unreliable network connection.


Set up IPv4 forwarding

One of the last things that we need to do before we send traffic anywhere is to enable packet forwarding.

To do this, open up the sysctl.conf file with sudo nano /etc/sysctl.conf, and remove the # from the beginning of the line containing net.ipv4.ip_forward=1. This will enable it on the next reboot, but because we are impatient, activate it immediately with :
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

We also need to share our Pi’s internet connection to our devices connected over WiFi by the configuring a NAT between our wlan0 interface and our eth0 interface. We can do this using 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  

However, we need these rules to be applied every time we reboot the Pi, so run sudo sh -c "iptables-save > /etc/iptables.ipv4.nat" to save the rules to the file /etc/iptables.ipv4.nat. Now we need to run this after each reboot, so open the rc.local file with sudo nano /etc/rc.local and just above the line exit 0, add the following line:

iptables-restore < /etc/iptables.ipv4.nat  

We’re almost there!

Now we just need to start our services:

sudo service hostapd start  
sudo service dnsmasq start  

And that’s it! You should now be able to connect to the internet through your Pi, via the on-board WiFi!

To double check we have got everything configured correctly, reboot with sudo reboot.


Some background knowledge, if needed.

dhcpcd is a mature and stable standards compliant DHCP client. It is used to obtain an IP address and other information from a dhcp server, renew the IP address lease time, and automatically configure the network interface. The program performs a similar function as dhclient. See dhcpcd.conf Ubunt/Debian /etc/dhcpcd.conf

HOSTAPD is a user space daemon for access point and authentication servers. In simple words, hostapd allows you to create software wifi access points allowing decent amount of configuration options. In rest of this post, I will show how to create a software access point in Linux using hostapd and share your internet to the devices through it. I have used my Lenovo Z560 with ath9k wifi driver under Arch Linux and have also tested it under Ubuntu 11.10. But the method is also applicable for other Linux distros and supported hardware.

Dnsmasq is a lightweight, easy to configure, DNS forwarder and DHCP server. It is designed to provide DNS and optionally, DHCP, to a small network. It can serve the names of local machines which are not in the global DNS

isc-dhcp-server. Dynamic Host Configuration Protocol (DHCP) The Dynamic Host Configuration Protocol (DHCP) is a network service that enables host computers to be automatically assigned settings from a server as opposed to manually configuring each network host.