Setup as Wireless Host

See also this alternate info:

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

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


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 "";
#option domain-name-servers,;

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.

At the bottom of the file add the following lines:

subnet netmask {
option broadcast-address;
option routers;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local";
option domain-name-servers,;

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:


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

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:


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

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.


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

sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.ORIG 
sudo mv hostapd /usr/sbin
sudo chmod 755 /usr/sbin/hostapd

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>