Router

Setting up Raspberry Pi as a network router.

Check the routing table with "route -n" or "ip route".

In my setup, it forwards both LAN and Wi-Fi traffic to the 3G modem.

Static IP Address

When used as a router, RPi should have a static IP address on the interface. This is configured in "/etc/dhcpcd.conf". I.e.

interface wlan0
    static ip_address=192.168.4.1/24

List the interface and the address with the IP segment (netmask).

DHCP Server

Configure DHCP, the dynamic address allocation for the devices that connect to RPi. Edit "/etc/dnsmasq.conf".

interface=wlan0
  dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h

List the interface name. The range is set up with the address range (start and end address), the net mask and the lease time (24 hours in this case).

IP Forwarding

Enable ipv4 forwarding in "/etc/sysctl.conf".

net.ipv4.ip_forward=1

DNS Server

dnsmasq is also used as a caching DNS server. Keep resolv.conf pointing the nameserver to 127.0.0.1. This will make all the clients use dnsmasq transparently.

Use fast DNS servers (like Google). Edit "/etc/dnsmasq.conf" and add

server=8.8.8.8

and/or other DNS servers. Restart the dnsmasq service "sudo service dnsmasq restart". DNS updates are very quick If you change a domain name on the DNS host, it will be updated on nameservers in a matter of seconds.

Use "dig +trace www.example.com" to see the full trace of the DNS query.

Also, disable resolving from the default nameservers on ppp interface. Edit "/etc/resolvconf.conf" and add

resolvconf=NO

This will prevent using the service provider's nameservers.

Note that, when using 3G modem, ppp interface will run scripts in "/etc/ppp/ip-up.d/" and set the nameservers accordingly.

See which DNS servers are used

When using dnsmasq, run "sudo service dnsmasq status" to see which nameservers are being used.

On Windows, use "nslookup <domain>" to see the info.

Links

    • DNS server with dnsmasq (link)
    • Changing DNS (link)

3G Router

Enable the ppp connection as described in the 3G section of the Network page.

To connect the Wi-Fi AP clients to the internet on USB 3G modem, set up the Access Point first. This is the minumum configuration that works. See above to configure the static ip address, DHCP, and access point.

sakis3g is a convenient script for establishing a network connection using a USB stick modem.

NAT

NAT forwards all traffic to the default route. After the ppp connection is started with sakis3g, enable nat with iptables:

sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

to persist the settings install the "iptables-persistent" package after the rule has been set. During installation, this will be made persistent. The rules can later be saved by

sudo netfilter-persistent save

Default Route

Sometimes the default route is not set as connecting takes a long time. It can be set manually:

sudo ip route add default via 10.64.64.64

where the IP address is the address of the interface. It can be seen with "route -n".

Links