Raspberry Pi as a Router. Internet Access Point Configuration

Da My-Lab.

Your Raspberry PI can be used as a router for your domestic or business network. In our example we connected the PI to the internet through a usb 3G android cellphone, and shared the connection over a WiFi stick, enabled with WPA2 security. The reasons why you'd like to set up such a solution, instead of relying on a pre-built access point, are many, and include the possibility to hijack and limit internet traffic going through the PI, in case, for example, you are providing internet to a community or to paying clients, such as in a hotel, and provide extra services and features such as enhanced firewall functionality, and shared file server. This guide is intended for linux debian systems, such as raspberry pi (raspian), debian, ubuntu and so on,

Raspberry Pi Router

First of all, install all which is necessary.

sudo apt-get install hostapd udhcpd

Set the configuration of udhcpd


and udhcpd

start     #default:
end      #default:
interface       wlan0           #default: eth0
opt     router
option  dns # appened to above DNS servers for a total of 3

then with a script you start everything up: create a file named router.sh in your home /home/pi/router.sh

killall hostapd -9
killall udhcpd -9
hostapd -B /etc/hostapd/hostapd.conf
ifconfig wlan0
udhcpd /etc/udhcpd.conf
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE

you can now run your script and start the networking.

sudo bash /home/pi/router.sh

To enable communications towards the internet towards my galaxy s, I had to add a few other lines, which set up the pi to the internet connection, which I placed in a file named /home/pi/internet.sh

ifconfig usb0
route add default gw

You can now access the internet with

sudo bash /home/pi/internet.sh

Beware, these lines will vary depending on the device you use to access the internet, i.e. if you connect through a LAN, an ADSL modem and so on.

That's it. To explore more options, I provide an example on how to limit the bandwidth given in the shared internet connection. We will use wondershaper

sudo apt-get install wondershaper

We can now limit the speed with a script in /home/pi/speed-limit.sh

wondershaper usb0 100000 256

and run

sudo bash /home/pi/speed-limit.sh

To wrap everything up, we can make everything start up at raspberry pi bootup time. Just edit /etc/rc.local and add