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

interface=wlan0
driver=nl80211
ssid=YOUR-NEW-WIFI-NAME-HERE
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=YOUR-NEW-WIFI-PASSWORD-HERE
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

and udhcpd

start           192.168.0.20    #default: 192.168.0.20
end             192.168.0.254   #default: 192.168.0.254
interface       wlan0           #default: eth0
opt     router  192.168.0.1
option  dns     8.8.8.8 # 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

#!/bin/sh
killall hostapd -9
killall udhcpd -9
hostapd -B /etc/hostapd/hostapd.conf
ifconfig wlan0 192.168.0.1
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 192.168.42.206
route add default gw 192.168.42.129

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

#!/bin/bash
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

/home/pi/internet.sh
/home/pi/router.sh
/home/pi/speed-limit.sh