How to setup a backup internet connection from a second ISP?

I have a Linux server running Debian which acts as a router/gateway for a network.
The server has 3 NICs on eth0 is the main ISP, on eth1 the backup ISP and on eth2 the network.
Is it possible to setup a script or something that will check if the connection with the main ISP is up and if not to switch(and setup the routes properly) to the backup ISP?


Please see the Linux Advanced Routing & Traffic Control documentation for "Routing for multiple uplinks/providers" here:

https://web.archive.org/web/20180304202946/http://lartc.org/howto/lartc.rpdb.multiple-links.html


You may read Gateway status checking methods on linux/bsd it's similar to what you are asking.
To answer you question precisely. You have normally 2 public IP address, one on eth0 and one on eth1.
You could write a script that try to ping the default gateway of isp1 (on eth0) and switch to eth1 if ping fail. But this will only switch when your link at your side is down (like DSL line down) but not from an outage within the ISP network because his gateway will still ping.
So you probably what to :

1.Ping ISP1 default gateway
2. Ping www.google.com or something else that is reliable
3. Check for paquet on eth0 with tcpdump 3.

If 1 is down, you should change the defaut gatway to the ISP2 gatway IP.
If 1 is Ok but 2 is Ko, may be it's juste google, may be an outage on ISP side, you may try other well know service yahoo.com, microsoft.com if all are Ko you probably want to switch to ISP2. You could use 3 here to see if you still receive packet from internet.

Depending on your distribution you have certainly some ifup/ifdown script somewhere in /etc they are called when an interface come up and down. This can also be used to switch the gateway. They will be called only when you lost the ethernet link.


In fact, true internet connection redundancy is only achieved by using BPG as an Autonomous System. This way you may have any number of uplinks from any number of ISPs, which will all point to your IP-address(es), which will be truly yours, not leased or rented, but an subnet you'll be able to use anywhere...

While the topic of acquiring ASN & setting up BGP is out of the scope of original question, I think it's important to mention nevertheless. If you feel interested, here is a guide you can start with:

https://www.auvik.com/media/blog/redundant-internet-connection-bgp/


How about doing this in the router?

The Draytek 2910 (ethernet) and 2820 (ADSL) routers support two WAN ports with load balancing and failover. The bit more expensive 3300 router supports 4 WAN ports.

JR


We have a linux server (a Raspberry Pi running Debian) that acts as a gateway. The linux server has 2 internet connections and runs DHCP services and NAT for the rest of the network.

Its primary Internet connection is via gateway 192.168.3.1 (a 4G USB stick) and there is a backup ADSL connection on 192.168.1.1

This script tests the Internet connection on gateway 192.168.3.1 to see if its working (by pinging a Level 3 Public DNS Server). If the Internet check fails, it sends all traffic to the backup gateway 192.168.1.1

It also logs when the primary connection fails to /var/log/4glog.txt

#!/bin/bash

/sbin/route add 209.244.0.3 gw 192.168.3.1

if ping -c1 209.244.0.3 ; then
        echo 4G Up
        /sbin/route del default gw 192.168.1.1
        /sbin/route add default gw 192.168.3.1
else
        echo 4G Down
        echo `date` 4G Down >> /var/log/4glog.txt
        /sbin/route del default gw 192.168.3.1
        /sbin/route add default gw 192.168.1.1
fi

/sbin/route del 209.244.0.3

Run this script from cron to automatically keep the gateway accessing the Internet.