I have an embedded system running Ubuntu 18.04 LTS that has a 3G modem and an Ethernet interface (eth0).
Both of these interfaces have access to Internet.
When the internet is unavailable on the Ethernet interface (cable is unplugged), I want to set the default gateway to the one of the 3G modem automatically, so the system can always access the internet.
For now, for test purposes and for the sake of simplicity, I'm substituting the 3G modem with another ethernet interface (a USB Ethernet adapter connected to another network - interface enxd037458b96e3) and I noticed that when the Ethernet connection is lost on eth0 (its cable is connected to a 4 port gigabit router) the default gateway disappears and I don't have access to internet whereas the USB Ethernet interface is active (and its IP address is automatically assigned with DHCP just like eth0).
In that case, this is the output of the route command :
$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 uap0
192.168.137.0 0.0.0.0 255.255.255.0 U 0 0 0 enxd037458b96e3
Whereas when the eth0 is up or recoverd :
$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.30.102 0.0.0.0 UG 0 0 0 eth0
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 uap0
192.168.30.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.137.0 0.0.0.0 255.255.255.0 U 0 0 0 enxd037458b96e3
Below, the content of this /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
iface eth0 inet dhcp
# post-up route add default via [gateway-ip-address] dev eth0
# interface usb eth
allow-hotplug enxd037458b96e3
iface enxd037458b96e3 inet dhcp
# auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
auto uap0
# allow-hotplug uap0
iface uap0 inet static
address 192.168.2.10
netmask 255.255.255.0
broadcast 192.168.2.255
post-up /etc/rc.apstart || true
# post-up /bin/uaputl.exe sys_cfg_80211d country DE || true
# post-up /bin/uaputl.exe sys_config /etc/uapTest.conf || true
# post-up /bin/uaputl.exe bss_start || true
post-down /bin/uaputl.exe bss_stop
post-down /bin/uaputl.exe sys_reset
# post-up /sbin/ifconfig uap0 192.168.0.2
UPDATE: Unlike the eth0 interface, when I unplug the cable from USB Ethernet adapter, route always shows the default gateway of this last. With the eth0 interface, the default gateway disappears and will be back only if the cable is plugged on it.
$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.137.1 0.0.0.0 UG 0 0 0 enxd037458b96e3
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 uap0
192.168.30.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.137.0 0.0.0.0 255.255.255.0 U 0 0 0 enxd037458b96e3
metric
as detailed here manpages.debian.org/jessie/ifupdown/interfaces.5.en.html