When several routes could match a destination, how does a router choose which one to use?
The router uses the longest match — the route whose subnet mask / prefix matches the most bits of the destination address wins, even over a more general route.
A routing table often holds several routes that all "cover" the same destination — for example both 10.0.0.0/8 and 10.1.1.0/24 contain the address 10.1.1.10. The router resolves this with the longest prefix match rule: it picks the route whose network prefix (the number after the slash, the count of leading bits fixed by the subnet mask) matches the most bits of the destination. A longer prefix means a more specific, narrower route, so /24 (a single subnet) beats /8 (a whole range of millions of addresses). This rule applies regardless of how the route was learned or its position in the table — specificity alone decides. It is what makes the default route (0.0.0.0/0, matching zero bits) the absolute last resort, used only when nothing more specific matches, and it lets you keep one broad "everything else" route while still steering particular networks down particular paths.
* Several routes can cover the same address; the one matching the most prefix bits (/24 here) wins, leaving 0.0.0.0/0 as the last resort. *
Best-match (longest prefix match) lookup:
- The router compares the packet's destination IP address against every route in the table.
- It selects the entry with the longest matching prefix (the most specific route).
- Example: for a packet to
10.1.1.10, a10.1.1.0/24entry is chosen over a less specific10.0.0.0/8entry.
Where the default route fits in:
- A default route (0.0.0.0/0) matches zero bits, so it is the least specific possible route.
- It is therefore only used when no more specific route matches — making it the "gateway of last resort."
Why it matters: Longest-match means you can install a broad default route for "everything else" while still steering specific networks down specific paths; the most specific entry always wins regardless of route type or order in the table.
Go deeper:
Wikipedia — Longest prefix match — the algorithm with a worked /28-beats-/16 example.
Wikipedia — IP routing — how longest-match fits into the full forwarding decision.