Raspberry Pi and Routing: Turning a Pi into A Router

The Raspberry Pi can be used as a router–great if you need a pocket-sized device to share an Internet connection.  It can also be used in conjunction with a cantenna.

Requirements For This Walkthrough


  1. Local network
  2. Mac or PC
  3. Raspberry Pi  running Raspbian “wheezy”
  4. Wireless USB Adapter
  5. HDMI Cable (*optional)
  6. Keyboard (*optional)
  7. Mouse (*optional)
  8. Monitor with HDMI input (*optional)

*If the Raspberry Pi is set up as a headless machine, you will not need a monitor, keyboard, or mouse–just another computer, which would be used to access it remotely over the network via SSH.


  1. Older hostapd (only needed if the driver via apt  does not work)

Knowledge, Skills, and Abilities

  1. Ability to navigate throughout a computer OS
  2. Knowledge of basic computer terminology
  3. Ability and confidence to enter commands in the Terminal, adjusting them to suit your environment, if necessary
  4. Familiarity with core networking concepts


  1. http://magpi.techjeeper.com/The-MagPi-issue-11-en.pdf
  2. http://www.pi-point.co.uk/documentation/
  3. http://blog.sip2serve.com/post/48420162196/howto-setup-rtl8188cus-on-rpi-as-an-access-point
  4. https://docs.google.com/file/d/0B3nsVzbJuBHVOWRJRDJacVd0S2s/preview?pli=1
  5. http://sirlagz.net/2012/08/11/how-to-use-the-raspberry-pi-as-a-wireless-access-pointrouter-part-3/
  6. http://www.raspberrypi.org/phpBB3/viewtopic.php?t=39096&p=393810
  7. http://unix.stackexchange.com/questions/119209/hostapd-will-not-start-via-service-but-will-start-directly
  8. http://www.daveconroy.com/turn-your-raspberry-pi-into-a-wifi-hotspot-with-edimax-nano-usb-ew-7811un-rtl8188cus-chipset/

Conceptual Overview

  1. Install Required Components via  apt-get
  2. Verify Wi-Fi Adapter dongle functionality
  3. Configure  /etc/network/interfaces 
  4. Create /etc/hostapd/hostapd.conf  with desired AP settings
  5. Point hostapd  to the configuration file in step 4 by modifying  /etc/default/hostapd
  6. Replace /sbin/hostapd  with the older version
  7. Configure DNS
  8. Enable IPv4 forwarding to reach the Internet

Prepare the RPi

Make certain that the RPi is up-to-date and all the newest packages are available for download/installation.

Install Required Components

The following components will be required for setting this up as an access point:

  • rfkill: a wireless utility
  • zd1211-firmware: common firmware that works with many Wi-Fi dongles
  • hostapd: the hostap wireless access point daemon
  • hostap-utils: supplemental hostap tools
  • iw: wireless configuration utility
  • dnsmasq: DHCP and DNS utility
  • bridge-utils:  used for connecting multiple Ethernet devices together

Install these components using the command:

Configure the Wireless Dongle

Verify The Dongle is Recognized by the RPi

Check that the RPi recognizes the Wi-Fi dongle.

Verify that the Wi-Fi dongle supports AP mode

If the output of the following command does now show anything about AP mode, then the dongle will not work for this project. But if it does, continue following the step-by-step directions.

Unfortunately, my dongle did not work and produced the error  nl80211 not found , which is OK.  Run this command:

if the output is similar to:

then it will probably still work.  This will be fixed in a later step.

Configure the Wireless Interface

Make a backup of /etc/network/interfaces  and then open it in a text editor:

Modify the file as below:

Restart the wlan0  interface:

Configure hostapd Settings

Next, make a backup of the  /etc/hostapd/hostap.conf  file and open it in a text editor to make some changes (ignore the first command if the file does not exist yet).

Modify the file to have the following content adjusting any unique items (i.e. country_code , ssid , etc)

If the iw list  command mentioned previously reported the error nl80211 not found , then create the file to have the following:

If the iw list  command reported the error, you will also need to make a backup of the  /etc/default/hostapd  file (if it exists),

and then add the following (be sure to uncomment):

It is also necessary to install a different version of   /usr/sbin/hostapd , which you can get here. Following our best practice, make a backup first and then edit it:

Delete the original file, which will be replaced by the one available in the Downloads section

Set the proper permissions on the new file:

Restart the hostapd  service to activate the settings.

If the hostapd restart did not work.  Try running this to view the output and see if you can find where the problem is in the config file and fix it:

The wireless network should show up if you were to scan for it from another device.  However, a few other settings need to be configured before anyone can connect to it.  In particular, dnsmasq .

Configure dnsmasq Settings

Following best practices, make a backup of the config file before editing it and then open the file in a text editor.

Uncomment the following lines and adjust them to your environment:

Restart the dnsmasq  service to apply the settings:

Attempt to connect to the network using another device.  You still won’t be able to get out the the Internet yet.

Enable Forwarding to Reach the Internet

Enable IPv4 forwarding

If the above command returns a permission denied , try this instead:

Enable NAT

Other Dongles and Other Uses

The main reason I chose the Wi-Fi dongle that I did was so that I could connect my cantenna to it and blast the signal a long distance.  It would have certainly been easier to use an adapter that worked right out of the box, but now I have a lot more flexibility with antenna types.

2 thoughts on “Raspberry Pi and Routing: Turning a Pi into A Router”

Leave a Reply