4

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?
5
  • You should define default gateway.Kernel didnt choose default gateway.
    – supriady
    Jan 14, 2017 at 11:33
  • destination 0.0.0.0 is default gw, no?
    – lash
    Jan 14, 2017 at 11:34
  • 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?
    – supriady
    Jan 14, 2017 at 13:56
  • 1
    @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.
    – xhienne
    Jan 15, 2017 at 14:04
  • Related: unix.stackexchange.com/questions/200188/… Oct 10, 2019 at 13:23

3 Answers 3

10

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.

8
  • 3
    What is the "administrative cost" in OP's output?
    – xhienne
    Jan 14, 2017 at 12:17
  • 1
    @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). Jan 14, 2017 at 13:38
  • Ah ok, so it implied, not actually shown. Thanks for the explanation. But a default GW is necessarily on a connected interface, right?
    – xhienne
    Jan 14, 2017 at 14:16
  • @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). Jan 15, 2017 at 13:59
  • 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). Jan 16, 2017 at 0:38
3

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.

1

From experimenting it seems to me that Linux uses the (equal cost) default gateway you have added last.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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