Using two ISP connections simultaneously to add stability in Linux
I have two internet connection from two different ISP. ADSL on eth0 and 4G on wlan0. Is there any way that if one ISP fails, the other take over automatically without a physical router and by Host OS (Ubuntu 18.04)?
I have never personally tried to do this from the host OS. But after a back and forth, I decided to dig around on the internet to see if I could find what you are looking for.
I found an example script for gateway fail-over at gist.github.com created by the user "Apsu". you can download the bash script here.
You will have to modify this to your specific setup, add your interface names, gateway addresses & you should be good to go.
Script for the Host OS:
#!/bin/bash
# Set defaults if not provided by environment
CHECK_DELAY=${CHECK_DELAY:-5}
CHECK_IP=${CHECK_IP:-8.8.8.8}
PRIMARY_IF=${PRIMARY_IF:-eth0}
PRIMARY_GW=${PRIMARY_GW:-1.2.3.4}
BACKUP_IF=${BACKUP_IF:-eth1}
BACKUP_GW=${BACKUP_GW:-2.3.4.5}
# Compare arg with current default gateway interface for route to healthcheck IP
gateway_if() {
[[ "$1" = "$(ip r g "$CHECK_IP" | sed -rn 's/^.*dev ([^ ]*).*$/\1/p')" ]]
}
# Cycle healthcheck continuously with specified delay
while sleep "$CHECK_DELAY"
do
# If healthcheck succeeds from primary interface
if ping -I "$PRIMARY_IF" -c1 "$CHECK_IP" &>/dev/null
then
# Are we using the backup?
if gateway_if "$BACKUP_IF"
then # Switch to primary
ip r d default via "$BACKUP_GW" dev "$BACKUP_IF"
ip r a default via "$PRIMARY_GW" dev "$PRIMARY_IF"
fi
else
# Are we using the primary?
if gateway_if "$PRIMARY_IF"
then # Switch to backup
ip r d default via "$PRIMARY_GW" dev "$PRIMARY_IF"
ip r a default via "$BACKUP_GW" dev "$BACKUP_IF"
fi
fi
done
With network hardware:
There are considerable advantages to using a router for this function, as opposed to just fail-over from the host OS. What immediately comes to mind is using both connections bandwidth simultaneously. (Fully utilizing both ISP connections)
You can use PFsense / OpenSense as a dual WAN router. (Most old PC hardware will work.) I believe DD-WRT supported routers are also capable of WAN fail-over with some tweaking. Or you could go with an out of the box solution, like Sophos or Netgear prosafe, I am sure there are others, But most out of the box solutions are more expensive than home users are willing to pay for. A dual wan router setup allows you to increase your internet bandwidth. You can load balance traffic for your specific needs. You can get internet connection redundancy and fail-over.
Regards,