raspberrypirouter

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

Materials

  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.

Downloads

  1. Older hostapd (only needed if the driver via apt  does not work)
  2. Copies of config files on Github (optional or for reference)

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

Resources

  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 (if necessary)
  7. Configure DHCP to allow clients to connect
  8. Enable IPv4 forwarding to reach the Internet

pi-router

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.

You should see a listing mentioning the adapter you have plugged in.

Verify that the Wi-Fi dongle supports AP mode

If the output of the following command does not 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 (highlighted lines are the additions):

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

If using a different adapter than the one I used, just modify the file by adjusting any unique items (i.e. country_code , ssid , etc).  If the iw list  command mentioned previously reported the error nl80211 not found , then modify the file as follows:

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

and then modify the highlighted line below (be sure to uncomment the line):

If you are using the same dongle I did, you will need to take another additiona step and replace   /usr/sbin/hostapd  with a newer version, which you can get here (but we will use a command to download it next).  Following our best practice, make a backup first and then edit it:

Delete the original file, which will be replaced by the one downloaded via the command below:

Set the proper permissions on the new file and make sure it is executable:

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 .

raspberrypi-router

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:

After saving the file, you can easily verify all the settings were correct with the following command (it just parses out the comments and blank lines):

Restart the dnsmasq  service to apply the settings:

Attempt to connect to the network using another device.  You should be able to connect, but you won’t be able to get out the the Internet yet.

Enable Forwarding to Reach the Internet

Enable IPv4 forwarding

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.

Appendix

Here are all the commands from the walkthrough needed to set up the router (excluding the information needing to be added to the files):

 Troubleshooting

I recently followed my own walkthrough and ran into an issue on the last command for iptables .  It returned the error:

I was using the image of raspian from back in January 2014.  After running

I was able to execute the command and get out to the Internet via the Raspberry Pi Router!

  • tuxun

    the last line is double :) I’m trying it!

  • Pingback: Tab Dump | mattezell.info()

  • blue architect

    Any thoughts on turning a Raspberry Pi into an aircard router for one device (IP Cam) via Lan? I’d like to plug a USB Aircard into the Pi and connect the cam via ethernet.

    • http://www.jacobsalmela.com Jacob Salmela

      If you are just looking to get an IP webcam, take a look at my other post on turning the Pi into a live-steam Webcam over the Internet. If you have already set up the Pi as a router, you could just plug in the USB Webcam, bypassing the aircard all together.

      If you are trying to use the celluar data service for the Webam, you would probably need the driver for the aircard first. Since an aircard is basically another network interface, you might be able to modify the steps in this walkthrough for that interface instead of the ethernet/wi-fi interfaces.

      • https://www.facebook.com/Blue.Architect Christian Silva

        Thanks for the info J. I am following your design since I just have to swap the interfaces and make a couple of changes. I’m new to linux so it is taking me a bit longer to learn my way around. Once I get the wwan0-eth0 bridge working I’ll post the results.

  • https://www.facebook.com/prokectx ProJectX
    • tuxun

      Do you know I got mail for this useless ad?

  • David

    How would one go about running this on start up? I can get it to work no problem after booting, Thank you!

    • http://www.jacobsalmela.com Jacob Salmela

      I tried this setup again and the router started automatically when I booted the Pi…

      • Ambitious

        Dear Jacob;
        good job for this post, i want to ask you a question, i need to make a small project like router,i.e i need to forward any data comes from local network to outside the local network through a GPRS / USB modem,
        and also i need to access a device connected to the local network remotly through the same GPRS or USB modem.
        I successfully did it using a TP-Link router and usb modem and using the port forward, but now i want to use my own Embedded system to add some control to my system,
        Could i know if the rabry Pi or any platform is suitable for this project or not. also i need to buy thousands of this system so i need to compile all command to the kernel instead of write all these command for each device.

        hope that i explained my problem successfully and i appreciate your support.

        Best regards
        Mohamed Hamed

  • http://www.jacobsalmela.com Jacob Salmela

    My *nix skills seem to fade if I don’t use them for a while, so I cannot answer this upfront. I am planning to follow my own tutorial and set this up on the faster B+ model and maybe make some sort of RPi mesh network or something…

  • http://gravatar.com/pmurad25 Pablo

    Hey bud, a simple 5v power supply can afford a usb wireless adapter?

  • Pingback: 設定 Raspberry Pi 成為 3G Router 教學 | Focus()

  • Pingback: Raspberry Pi – Turn your Pi into a Wireless Router | theezitguy()

  • leuk_he

    This tutorial makes a fine access point from the wlan interface, but it does not function as a nat router for me.

    The windows pc still is connecting to the upstream dhcp and gets an address in the range of the upstream (tplink) router.

    e.g. i am missing the place where a ip number is assigned to the wlan interface.

    • http://www.jacobsalmela.com Jacob Salmela

      What do you have set in your dnsmasq.conf file? You should have the dhcp-range= option set in order to distribute DHCP addresses.

      /etc/network/interfaces is the file where you can assign how an interface gets its IP address. In this walkthrough, I had my Pi connected directly to my router via eth0, so I had this in the file: iface eth0 inet dhcp. This says that eth0 should have its IP set via DHCP (i.e. from my existing router). I set it this way so I could configure the entire thing via SSH.

      You will probably want to look at adjusting both of the files mentioned above.

  • iskender

    thank you for tutorial but I can connect raspberry but there is no internet?

    • iskender

      I am sorry. Everything works well. Thank you again

  • iskender

    Is there a way to learn ip of devices connect to raspberry ?

    • http://www.jacobsalmela.com Jacob Salmela

      I think the easiest way would be to run this command:
      cat /var/lib/misc/dnsmasq.leases
      Client requets also show up in the main log file along with other information:
      cat /var/log/daemon.log