How to Set up a Raspberry Pi as a Wireless Access Point

The Raspberry Pi can connect to a Wi-Fi network using a USB dongle but using that same dongle you can also turn your Raspberry Pi into a wireless access point. Once set up correctly, this will allow other wireless devices to connect to your Pi and optionally you can route any traffic out through the Ethernet port and on to the internet (via the router from your ISP).

However, before looking at the steps needed to get this working, a word of warning. The configuration needed can be a little complex and if things don’t work as they should then troubleshooting the problem can be difficult. Also for this to work correctly, you need a WiFi USB dongle that can work as an access point. The best place to find information about your particular dongle and the Raspberry Pi is on the embedded Linux Raspberry Pi Wi-Fi adapters page.

To configure a hotspot requires several steps:

  • Configure the wireless adapter with a static IP address
  • Install and configure a DHCP server
  • Install and configure the access point daemon
  • Configure IP routing between the wireless and Ethernet

In this example, the wireless network will use the address range 192.168.42.n and the wired Ethernet will use the address range 192.168.1.n.

Edit “/etc/network/interfaces” and add the static IP address information for wlan0. You can learn about static IP addresses in our SSH and static IP address tutorial.

 sudo nano /etc/network/interfaces

Place a “#” sign in front of all the lines which mention wlan0 and wpa, except for “allow hotplug wlan0“. Then add the following lines to the file:

iface wlan0 inet static
address 192.168.42.1
netmask 255.255.255.0

The bottom half of the file will now look something like this:

raspberry-pi-hotspot-interfaces

Now reboot.

Install the DHCP server:

sudo apt-get install isc-dhcp-server

You can safely ignore any errors about not being able to start the DHCP server at this point. Now edit its configuration file:

sudo nano /etc/dhcp/dhcpd.conf

Add a “#” character in front of the “option domain-name” lines like this:

#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;

Remove the “#” sign in front of the “authoritative;” statement like this:

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;

At the bottom of the file add the following lines:

subnet 192.168.42.0 netmask 255.255.255.0 {
range 192.168.42.10 192.168.42.50;
option broadcast-address 192.168.42.255;
option routers 192.168.42.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local";
option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Exit from nano with “Ctrl + X”.

Make the wireless adapter the default for the DHCP request:

sudo nano /etc/default/isc-dhcp-server

Change “INTERFACES=""” to “INTERFACES="wlan0"

Exit from nano with “Ctrl + X”.

Restart the DHCP server:

sudo service isc-dhcp-server restart

Install hostapd:

sudo apt-get install hostapd

Edit the hostapd configuration file and create a wireless network:

sudo nano /etc/hostapd/hostapd.conf

Add the following lines:

interface=wlan0
driver=nl80211
#driver=rtl871xdrv
ssid=MyPi
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

This will create a password protected network called MyPi on channel 6 with the password raspberry.

Tell hostapd where to find its configuration file by setting the default location:

sudo nano /etc/default/hostapd

Remove the “#” in front of “DAEMON_CONF” and alter the line to read:

DAEMON_CONF="/etc/hostapd/hostapd.conf"

Edit “/etc/sysctl.conf” to enable IP forwarding:

sudo nano /etc/sysctl.conf

Find the line which reads “Uncomment the next line to enable packet forwarding for IPv4” and uncomment the next line like this:

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

Run the following command to activate forwarding now:

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

Now turn the Pi into a router with the follow 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

And save the routing tables into the file “/etc/iptables.ipv4.nat

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Edit “/etc/network/interfaces“:

sudo nano /etc/network/interfaces

And add the following line to the end of the file. This line will restore the routing table whenever the Pi is booted:

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

You should now reboot your Pi and test the wireless access using a laptop, smartphone, tablet or other Wi-Fi enabled device.

raspberry-pi-hotspot-connected-700px

Since this configuration is quite complex things can easily go wrong. If you run into trouble, double check all the configuration files. It is also worth power cycling the Pi as it is possible for the USB dongle to get into an undefined state.

You may have noticed that the “hostapd.conf” file had two “driver=” lines and one of them was commented out. If your USB Wi-Fi dongle uses the nl80211 driver then the above configuration should work OK. However if your dongle uses the rtl871xdrv which it does for adapters based on the Realtek RTL8188CUS chipset then there are some extra steps needed.

First install the iw package:

sudo apt-get install iw

Now run the following command:

iw list

If the output of the command is “nl80211 not found” then you need to use the rtl871xdrv driver. However you will also need a special version of hostapd.

Adafruit has a pre-compiled version of hostapd for the rtl871xdrv driver. To install it use the following commands:

wget http://www.adafruit.com/downloads/adafruit_hostapd.zip 
unzip adafruit_hostapd.zip 
sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.ORIG 
sudo mv hostapd /usr/sbin
sudo chmod 755 /usr/sbin/hostapd

If you have any questions about turning your Pi into a wireless access point, please ask below and we will try our best to help!