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   U     0      0        0 uap0   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         UG    0      0        0 eth0   U     0      0        0 uap0   U     0      0        0 eth0   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
      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


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         UG    0      0        0 enxd037458b96e3   U     0      0        0 uap0   U     0      0        0 eth0   U     0      0        0 enxd037458b96e3
  • This is often done using a different metric for the backup route in the kernel IP routing table. Both interfaces would be up and UG but one would be preferred by its metric. BTW Gateways are missing for some of your connections. – Ned64 Dec 15 '20 at 9:17
  • 1
    I can't find the question, but the last time I saw this ended up with them using this and writing a script that they loaded as a service. I remember because I made a note of the URL I linked. Sadly, I didn't note the URL for the question. – KGIII Dec 15 '20 at 16:38
  • 1
    This could help: thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System Work with metric as detailed here manpages.debian.org/jessie/ifupdown/interfaces.5.en.html – Ned64 Dec 15 '20 at 18:19
  • 1
    In fact, this could be a duplicate of unix.stackexchange.com/questions/345862/… - see whether it works. – Ned64 Dec 15 '20 at 18:21
  • 1
    @KGIII are the IP addresses dynamically assigned ? Otherwise, I'm going to test the "bonding" technique. – Aminos Dec 16 '20 at 16:34

Similar to this question : https://askubuntu.com/questions/948453/internet-connection-stops-after-one-interface-is-down-in-ubuntu-server I will use NetworkManager to manage my connections (3G too).

However, when the internet access is lost on the interface that has the lowest metric, the system cannot access to internet anymore whereas an interface having a higher metric but connected to internet exist (e.g. a 3G modem), in that case we do need to manually increase the metric of the interface not connected to internet.

I will see if I can request a new system image with the interface bonding built in it.

Otherwise, one can write a service to monitor if the default route with the lowest metric is connected to internet and can control the NetworkManager.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.