Longest Prefix Match
Cheat sheet · https://pcornel.dk/topic.php?topic=2603
Answer
When several routes match a destination, the router uses the one with the longest prefix — the most specific. A /32 beats a /24, which beats a default route /0. Administrative distance and metric only decide between routes for the same prefix.
Remember
Specificity first, then administrative distance, then metric. A more specific route from any source wins over a less specific one from a "better" protocol. Summary and default routes catch only what nothing more specific matches.
First checks
show ip route <dst>— the route actually chosen for that address.show ip route <prefix> <mask> longer-prefixes— every more specific route under a block.show ip cef <dst>— the forwarding entry used.show ipv6 route <dst>— the same for IPv6.
Verify in this order
destination address → all routes that contain it → the longest prefix selected → its next hop resolved → CEF entry matches
Where it usually breaks
underlay/interface reachability → protocol adjacency/session → received/originated state → policy → best path/RIB → FIB/adjacency → traffic path
Route lookup decision order
| Stage | Decision | Example |
|---|---|---|
| 1 | Longest matching prefix | /32 beats /24 beats /0 |
| 2 | Route-source preference for same prefix | lower Cisco AD wins between sources |
| 3 | Protocol-specific path choice | OSPF cost, EIGRP DUAL/metric, BGP best-path logic |
| 4 | Multipath/install constraints | equal/eligible paths may coexist |
| 5 | FIB/CEF programming | recursive next hop and adjacency become forwarding state |
Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=2603