Tuesday, September 8, 2015

Raspberry Pi2 as a Router with Network Failover

This experiment is all about using Pi2 as a Home router with network failover. Assume you’ve both wired internet (Dialup Modem), and a mobile 3G internet (eg. 3G USB Dongle). Your wired internet can be considered as the primary and mobile 3G internet as backup or secondary.

The requirement is to, provide internet through your primary by default. As soon as your primary network is down, switch to your secondary. Your network clients (i.e the PC, smartphones, laptop which are connected to your local network through Ethernet and WIFI), should not be reconfigured, they should be automatically routed to the secondary network for accessing the internet, when the primary goes down.

Here Pi2 will be configured as a single gateway for all the network clients. By default it will route packets designated to internet to the primary network. Whenever primary is not available, it will dial in secondary network and route packets through secondary. When Primary is back online, Pi2 will disconnect the secondary, and route packets back to Primary. All packets from the attached clients will be routed to Pi2 first, where the actual routing will happen. Pi2 has been running with Ubuntu.14.04.

The conceptual diagram is shown below.


The configuration steps has been detailed below.

1.  Primary internet will be available through the ADSL modem, which is connected to a Wireless router (

2.  DHCP Server is disabled in the Wireless router, since DHCP requests will be served by the PI2

3.  Pi2 ( is connected to the Wireless Router through Ethernet through ‘eth0’ interface. It is the default gateway for the LAN connected devices

4.  DNSMASQ utility has been configured to run on Pi2, as a DHCP service, which will serve IP Address for the LAN connected devices.

Range: –, Gateway: (The Pi2 itself)

So that clients will route the packets to the Pi2.

5.  Pi2 has set the default route to the Wireless Router ( for internet, in the IP Routing table)

i.e. route add default gw

6.  Pi2 has also connected with a 3G USB Modem, which has been configured with ‘Wvdial’ utility, this will be secondary network. It will be recognized as ‘ppp0’ interface in Pi2.

7.  Pi2 is configured to Forward packets from eth0 to ppp0 through NAT (Using IP Tables)

8.  When internet is not available through primary (i.e through eth0), Pi2 has to dial ppp0

9.  Pi2 has to delete the default route through Wireless Router to internet, so that from now on packets will be routed to ppp0, to reach internet

Note: You can check internet connectivity through any interface using ‘ping’ command, where you can optionally give the interface name as well. In this case eth0

10.  Once the Primary network is back, ppp0 will be brought down. The default route will be added back to Wireless router, so that packets will be routed through it from now on.

11.  Steps 8-9, will be done through a shell script running in the background.

No comments:

Post a Comment