1

I have a RaspberryPi Zero with WIFI, that is connected to a USB modem (emulating ethernet, which produces eth0). Both work. I want to be able to select which one to use, and which one to be fallback. What is the correct way of doing this?

Thanks!

Ilans
  • 19
  • 1
  • 1
    Where did you get a USB modem that presents itself as a USB ethernet device rather than a dialup modem/tty? I want! – R.. GitHub STOP HELPING ICE Sep 22 '19 at 02:29
  • This is more of a generic Linux networking question than a Raspberry Pi specific question. You might have better luck at Server Fault or Super User. – Fred Sep 22 '19 at 10:31
  • Possible duplicate of [Howto migrate from networking to systemd-networkd with dynamic failover](https://raspberrypi.stackexchange.com/questions/78787/howto-migrate-from-networking-to-systemd-networkd-with-dynamic-failover) – Dmitry Grigoryev Sep 23 '19 at 12:47

2 Answers2

3

This is a typical use case for dynamic failover. This will configure both interfaces eth0 and wlan0 and use one primary interface that you can define. if this connection fails it will automatically use the other interface as fallback. How to setup this you can look at Howto migrate from networking to systemd-networkd with dynamic failover.

Ingo
  • 40,606
  • 15
  • 76
  • 189
1

Preferred routing is determined by the metric. Lower metrics are preferred.

This can be shown by ip r e.g.

default via 10.1.2.1 dev eth0 proto dhcp src 10.1.2.74 metric 202 
default via 10.1.2.1 dev wlan0 proto dhcp src 10.1.2.84 metric 303 
10.1.2.0/24 dev eth0 proto dhcp scope link src 10.1.2.74 metric 202 
10.1.2.0/24 dev wlan0 proto dhcp scope link src 10.1.2.84 metric 303 

dhcpcd sets these by default, assigning a higher priority to Ethernet (which is usually desirable), but you can change this by setting the metric for an interface in dhcpcd.conf.

metric

Metrics are used to prefer an interface over another one, lowest wins.
dhcpcd will supply a default metric of 200 + if_nametoindex(3).
An extra 100 will be added for wireless interfaces.

Milliways
  • 54,718
  • 26
  • 92
  • 182
  • 1
    Fiddling with metric doesn't help if an interface fails (e.g. unplugged). This will break all stateful connections (e.g. TCP, ssh, streams, etc.) and you have to reboot to get it again on the other interface. New connections may work without reboot. – Ingo Sep 22 '19 at 08:46
  • @Ingo I don't understand the comment. If I disconnect `eth0` `dhcpcd` releases the IP Address and it is removed from the routing table. – Milliways Sep 23 '19 at 12:05
  • 1
    You are right. The kernel will to that and it will use the route with the next higher metric. It will also use the new **source** ip address of the (switched) default route. But this will break all established stateful TCP connections like **ssh** or others. The other end don't know the new source ip address and get stuck. To avoid this you need an interim interface like **bond0** that will not switch its ip address. – Ingo Sep 23 '19 at 12:34