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
$ 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 linkdowndefault via 10.0.10.1: the default route, written asdefaultinstead of 0.0.0.0/0.proto kernel scope link: the connected route the kernel added when the address was configured.scope linkmeans the destination is directly on the wire, no gateway.proto static: installed by the network manager from config.proto dhcpcomes from a lease. Routes added by hand withip route addgetproto boot, whichip routedoes 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
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 100Metrics 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.
They will ask next
Where are the routes for the host's own addresses?
In the
localtable (ID 255), consulted first by rule priority 0.ip route show table locallists entries likelocal 10.0.10.21 dev ens192 proto kernel scope host src 10.0.10.21.What happens if you add a second route to the same prefix with the same metric?
ip route addfails withRTNETLINK answers: File exists. You either give it a different metric (a floating backup), useip route append, or build one ECMP route with severalnexthop viaentries.How do you add an ECMP default route?
ip route add default nexthop via 10.0.10.1 dev ens192 weight 1 nexthop via 10.0.20.1 dev ens224 weight 1. IPv4 multipath hashes on source and destination address by default;net.ipv4.fib_multipath_hash_policy=1adds ports (layer 4) so different flows between the same two hosts can spread.
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 routewhen policy routing rules send traffic to another table.