2

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
Aminos
  • 131
  • 1
  • 9
  • 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 09:17
  • @Ned64, I noticed that the first interface that has the cable plugged is the one that fixes the default gateway (eth0 or enxd037458b96e3). How can I achieve what you said ? – Aminos Dec 15 '20 at 09:43
  • I found a similar question https://askubuntu.com/questions/948453/internet-connection-stops-after-one-interface-is-down-in-ubuntu-server but the author didn't explain how he managed to fix this problem with network manager :( – Aminos Dec 15 '20 at 11:22
  • Could it be, that the `allow-hotplug` option is the problem? Since the USB is always connected, there is no plugging event (on the device itself, independent of unpluggin eht0). Why this line? – FelixJN Dec 15 '20 at 11:56
  • @Fiximan I tried in the other way : USB Ethernet fixing the default gateway, then I plug the cable in the ethernet port of eth0, I unplug the cable from the USB Ethernet, but the default gateway is not updated. – Aminos Dec 15 '20 at 13:05
  • I think I'm going to test Ubuntu's Network Manager, otherwise, I think the solution is to save the network configurations sent by the DHCP servers somewhere (of the interfaces) and then, use those configurations when a link is down (cable/interface down or internet down). I don't know if this is feasible in /etc/network/interfaces or maybe I will need to launch a service. – Aminos Dec 15 '20 at 14:35
  • 1
    I can't find the question, but the last time I saw this ended up with them using [this](https://www.computerhope.com/unix/ifup.htm) 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: https://www.thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System Work with `metric` as detailed here https://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 https://unix.stackexchange.com/questions/345862/is-it-possible-to-have-multiple-default-gateways-for-outbound-connections - see whether it works. – Ned64 Dec 15 '20 at 18:21
  • @Ned64 The issue is that the IP address is assigned by a DHCP server, it's not static. In the link you have shared, there's an answer talking about "Bonding", I think that's what I need, I will test and give you a feedback. – Aminos Dec 16 '20 at 16:33
  • 1
    @KGIII are the IP addresses dynamically assigned ? Otherwise, I'm going to test the "bonding" technique. – Aminos Dec 16 '20 at 16:34
  • They *should* be assigned by DHCP, unless you've specifically set them to be static. Should... I've yet to actually try it out and merely found the answer interesting/informative. – KGIII Dec 16 '20 at 16:57
  • @KGIII I can't use interface bonding for now, as the Ubuntu I have (designed for an embedded system) doesn't have the kernel module (ls /lib/modules/`uname -r`/kernel/drivers/net/bonding/bonding.ko), can you please make an effort to find the question you are talking about. Ned64's link to a question contains a solution only if the interfaces configuration is static :( – Aminos Dec 17 '20 at 08:41

1 Answers1

0

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.

Aminos
  • 131
  • 1
  • 9