How does Linux choose between default gateways?

5,941

Solution 1

In this case the kernel chooses based on the metric: the lower metric wins. (Route selection is based on route specificity, administrative cost, and metric in that order. Both your default gateways have the same specificity and administrative cost.)

To change the selection, the best approach is to change the route metric.

Solution 2

I came to this post because I had two different PCs, each with dual network cards, each one configured something like this:

auto enp6s0 
iface enp6s0 inet dhcp
    address 192.168.20.36
    netmask 255.255.255.0
    gateway 192.168.20.1


auto enp7s0 
iface enp7s0 inet static
    address 10.10.10.3
    netmask 255.255.255.0
    gateway 192.168.20.1

They were both able to connect to the other machines on the 192.168.* LAN and the IOT gadgets on 10.10.*, but one of them could not get out to the Internet.

No policies had been defined, and the route command showed that Metrics were equal... except that for the one with no Internet connectivity the default gateway was using the network card associated with the 10.10.* network.

Apparently (empirically, not guaranteed!) if the Kernel has nothing better to go by it will use the first one it finds. Editing /etc/network/interfaces so that the reference to 192.168.* was listed before the reference to 10.10.* in /etc/network/interfaces appears to have solved the problem.

Share:
5,941

Related videos on Youtube

lash
Author by

lash

Updated on September 18, 2022

Comments

  • lash
    lash over 1 year

    I have a computer with two NICS, one eth one wlan.

    • wlan is on 10.0.0.0/24
    • eth is on 192.168.0.0/16

    Kernel routing table is:

     $ route -n
     Kernel IP routing table
     Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
     0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 enp4s0f0
     0.0.0.0         10.0.0.1        0.0.0.0         UG    600    0        0 wlp3s0
     10.0.0.0        0.0.0.0         255.255.255.0   U     600    0        0 wlp3s0
     169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 enp4s0f0
     192.168.0.0     0.0.0.0         255.255.0.0     U     100    0        0 enp4s0f0
    

    Questions:

    1. Does the kernel choose which default gw use, or does it send to both?
    2. How does it choose if it chooses?
    3. What is the most efficient way to influence the choice, or to make it make one?

    using 4.4.0-45-generic

    • supriady
      supriady over 7 years
      You should define default gateway.Kernel didnt choose default gateway.
    • lash
      lash over 7 years
      destination 0.0.0.0 is default gw, no?
    • supriady
      supriady over 7 years
      Default Gateway is Router IP address to connect to Internet.Can you connect to internet using 0.0.0.0 as Default gateway?Can you access www.xxxx.com using 0.0.0.0 as name server?
    • xhienne
      xhienne over 7 years
      @lash Yes, destination 0.0.0.0 means 'default' and the getaway associated to this destination is the default GW. Either you define it manually, or automatically with DHCP. If there are several default GWs, the kernel choose the one to use according to many parameters (policy, metrics, etc). See Stephen's answer.
    • Christopher
      Christopher over 4 years
  • xhienne
    xhienne over 7 years
    What is the "administrative cost" in OP's output?
  • Stephen Kitt
    Stephen Kitt over 7 years
    @xhienne the administrative cost is 0 for both default routes because they correspond to connected interfaces. A route's administrative cost depends on the source of its definition: 0 if it's a connected interface, 1 if it's a static route, varying amounts for other route sources (depending on the protocol, e.g. RIP v. OSPF).
  • xhienne
    xhienne over 7 years
    Ah ok, so it implied, not actually shown. Thanks for the explanation. But a default GW is necessarily on a connected interface, right?
  • Stephen Kitt
    Stephen Kitt over 7 years
    @xhienne I guess it is — I'm trying to think of scenarios where it wouldn't be but I can't think of one (tunnels etc. appear as new interfaces).
  • benishky
    benishky over 7 years
    In newer kernels you can use Policy-based routing (you also need the new iproute2 package) ... you then put the default routes into different tables and create rules which determine when to use each table (and hence, which default route applies).
  • Sander Steffann
    Sander Steffann over 7 years
    Policy based routing is often necessary to prevent traffic originating from for example 192.168.0.1 to be sent through the default gateway belonging to 10.0.0.1. Source address filtering and NAT require more symmetric routing.