Lesson 4 of 9 · 27m
Routing Tables, Policy Routing, and the Longest Match
Use routes, rules, and route lookup to explain forwarding decisions instead of treating the main table as the whole routing system.
Routing on Linux is not one table consulted once in an obvious way. It is a longest-prefix-match database, multiplied by tables, selected by rules, constrained by nexthop reachability, and sometimes bypassed by policy. The shortest path to confusion is running ip route and assuming you saw the entire system.
Read the main table as a decision list
ip route show
ip -6 route showTypical output looks simple:
default via 192.0.2.1 dev eth0 proto dhcp metric 100
10.8.0.0/24 dev wg0 proto kernel scope link src 10.8.0.2
192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.14The kernel chooses the most specific prefix. 192.0.2.55 matches the connected /24 before default. 10.8.0.20 matches the WireGuard interface route. Metrics only break ties among otherwise similar routes; they do not override prefix length.
Ask the kernel, do not guess
Route lookup is one of the highest-value commands in network debugging:
ip route get 203.0.113.40
ip route get 203.0.113.40 from 192.0.2.14 iif eth0The answer includes the selected source address, output interface, nexthop, and sometimes a failure such as unreachable or prohibit. When an application binds to an unexpected source address, ip route get usually explains it before anyone opens the service configuration.
Rules select tables before tables select routes
Inspect policy routing with both commands:
ip rule show
ip route show table allA realistic policy setup might be:
0: from all lookup local
100: from 192.0.2.14 lookup isp1
200: fwmark 0x1 lookup vpn
32766: from all lookup main
32767: from all lookup defaultRules are evaluated in priority order. A packet can match a rule and then fail because the selected table has no valid route, even though the main table would have worked. That is why policy-routing incidents often look intermittent or application-specific.
Multihomed hosts are asymmetric until proven otherwise
Two uplinks with one main-table default route mean replies may leave through the wrong provider. Source-based routing, connection marking, or reverse-path filtering then becomes relevant:
sysctl net.ipv4.conf.all.rp_filter net.ipv4.conf.eth0.rp_filterStrict reverse-path filtering can drop legitimate asymmetric traffic. Loose mode is more forgiving but still not magic. If incoming packets arrive on one interface and the route lookup says replies leave another, write that down before changing anything.
Connected routes carry operational consequences
When you add an address with a prefix, Linux normally installs a connected route. Deleting the address removes it. Moving an address to a different interface silently changes forwarding and source-selection behavior. Routing changes are often side effects of address changes, not explicit route edits.
Field notes
localtable entries control addresses owned by the host; do not treat them as cosmetic.proto kernel,proto dhcp,proto static, andproto birdreveal who installed a route.- A route can exist but be unusable because its nexthop is unresolved or unreachable.
- Network namespaces have independent rules and tables; the host's correct route proves nothing inside a container.
- After any change, test with
ip route getusing the same source and destination shape as the failing traffic.
Was this lesson helpful?