Octet

How do you read the Linux routing table, and how does Linux choose between routes?

Show the answer

Say this first

ip route shows the main table. Linux picks the longest prefix match first. Between routes with the same prefix it picks the lowest metric. There is no administrative distance as on Cisco: whoever installs the route sets the metric. Each line shows the prefix, the next hop (via), the interface (dev), who installed it (proto), the scope and the preferred source address (src).

Go deeper

A real table

Output
$ ip route
default via 10.0.10.1 dev ens192 proto static
10.0.10.0/24 dev ens192 proto kernel scope link src 10.0.10.21
172.16.40.0/22 via 10.0.10.254 dev ens192 proto static
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown
  • default via 10.0.10.1: the default route, written as default instead of 0.0.0.0/0.
  • proto kernel scope link: the connected route the kernel added when the address was configured. scope link means the destination is directly on the wire, no gateway.
  • proto static: installed by the network manager from config. proto dhcp comes from a lease. Routes added by hand with ip route add get proto boot, which ip route does not print.
  • src 10.0.10.21: the source address the kernel prefers for traffic using this route.
  • linkdown: the interface has no carrier, here libvirt's bridge with no VMs running.

Changing it

Shell
ip route add 172.16.40.0/22 via 10.0.10.254 dev ens192
ip route replace 172.16.40.0/22 via 10.0.10.253   # add or overwrite in one step
ip route del 172.16.40.0/22
ip route add default via 10.0.10.1 metric 100

Metrics in practice

NetworkManager gives Ethernet default routes metric 100 and Wi-Fi 600, which is why a laptop prefers the cable when both are connected. The lower metric wins.

Other tables

ip route only shows table main. The local table holds the host's own addresses and broadcast addresses. ip route show table all shows everything.

Traps

  • Talking about administrative distance in the Linux kernel. Longest match, then metric. Routing daemons such as FRR apply their own distances before they install a route.
  • Reading only ip route when policy routing rules send traffic to another table.
How did you do?Book 3.3 Internet (IP)