PracticeLabs
Week 4Lesson 5Required32 min estimated0% progress

Longest-Prefix Match, AD, and Metrics

Apply the router's real selection order — longest-prefix match, then administrative distance, then metric — to predict exactly which route forwards a packet.

Lesson orientation

What you'll learn (4 objectives)~32 min: video → lesson → check → apply → lab prep

Learning objectives

  • State the three-step order a router uses to choose a route
  • Use longest-prefix match to pick the most specific matching prefix regardless of source or AD
  • Use administrative distance to choose between two sources offering the same prefix, from the standard values
  • Explain that metric only breaks ties within a single protocol, and diagnose an unexpected path caused by AD or LPM

Terms you will see

Longest-prefix matchAdministrative distanceMetricRoute sourceSpecificityShadowed route

Time breakdown

  • Read the notes16 min
  • Routing Table Trainer — hard mode10 min
  • Decision walkthrough6 min

Assigned video

Opens on YouTube
Optional video

Dynamic Routing, AD & Floating Static (Day 24)

By Jeremy's IT Lab · Opens externally on YouTube

Assigned watch
~6 min

The link opens in a new browser tab. Return here when you finish watching.

Watch for these concepts

  • How AD ranks different route sources
  • Longest-prefix match being applied before AD is even considered

Go beyond the video

The assigned video introduces the idea. CCNA Practice Labs completes the learning path: clarify core concepts, explore how each device behaves, visualize the communication path, check your understanding, and prepare for hands-on lab work.

Watch the concept → understand it → visualize it → practice it → apply it.

Three tests, in a fixed order

When several routes could carry a packet, the router applies three tests in order and stops at the first one that decides. Get the order right and every route-selection question becomes mechanical.

  1. Longest-prefix match — of all routes whose prefix contains the destination, the one with the most specific (longest) prefix wins. Decided first, and decided on the destination address alone.
  2. Administrative distance — only if two or more routes match at the same longest prefix length but come from different sources, the source with the lower AD is installed.
  3. Metric — only if the tie is still unresolved because two routes to the same prefix come from the same protocol, the lower metric wins.
These are not three equal factors
Longest-prefix match is not one consideration among several — it is the gate everything else passes through. A more specific prefix wins even with a worse AD and a worse metric, because AD and metric are never consulted for prefixes of different lengths. The order is a filter, not a weighted score.

There is a reason this order makes sense rather than being arbitrary. A more specific route reflects more precise knowledge about a destination: 10.1.1.0/24 says I know specifically about this /24, while 10.1.0.0/16 says I know about this larger block generally. Following the more specific route sends the packet toward the router with the finer-grained information, which is almost always the more correct path. The default route 0.0.0.0/0 is the logical endpoint of that idea — the least specific route, used only when nothing else matches.

Longest-prefix match, worked

A table where prefixes overlap
R2# show ip route
Codes: C - connected, L - local, S - static, O - OSPF
       * - candidate default

Gateway of last resort is 203.0.113.1 to network 0.0.0.0

S*   0.0.0.0/0 [1/0] via 203.0.113.1
      10.0.0.0/8 is variably subnetted, 6 subnets, 4 masks
O        10.1.0.0/16 [110/20] via 10.9.9.1, 00:12:04, GigabitEthernet0/2
S        10.1.1.0/24 [1/0] via 10.9.9.5
C        10.1.1.0/26 is directly connected, GigabitEthernet0/0
L        10.1.1.1/32 is directly connected, GigabitEthernet0/0
Same table, three destinations, three different winners
DestinationWinnerWhy
10.1.1.50C 10.1.1.0/26The /26 covers .0 through .63, so .50 is inside it. It is the most specific match, so it wins outright — the OSPF /16 and static /24 also match but are less specific.
10.1.1.200S 10.1.1.0/24Outside the /26's range of .0–.63, so the /26 no longer matches at all. The next most specific is the static /24.
10.2.5.5S* 0.0.0.0/0None of the 10.1 prefixes match — the OSPF /16 covers 10.1.0.0 through 10.1.255.255, and 10.2 is outside it. Only the default remains.

Note that none of those three decisions consulted AD or metric. Prefix length settled all of them, which is what makes the first test so much more important than its position in a list suggests.

Metric is the within-protocol tiebreaker

If the surviving candidates are two routes to the same prefix from the same protocol — two OSPF paths, for instance — the router compares their metric and installs the lower. Each protocol computes its metric differently: OSPF uses a cost derived from bandwidth, EIGRP a composite of bandwidth and delay, RIP a hop count.

Why metrics never cross protocol lines
Because the units are incompatible. A router never compares an OSPF cost of 20 against an EIGRP metric of 3072 — the comparison would be meaningless. That incomparability is precisely why administrative distance exists: something has to arbitrate between protocols before metric is ever reached.
Why a better metric can never rescue a losing route
Each test is only reached if the previous one tied. If a competitor has a longer prefix, LPM ends the contest immediately and neither AD nor metric is examined — so no metric could help. If prefixes tie but a competitor has a lower AD, the AD test ends it, and again metric is never examined. Metric is only consulted when prefix length and source protocol are both identical. A brilliant OSPF metric is irrelevant if a static route covers the same prefix, because the static wins on AD long before OSPF's metric is looked at.

When a healthy protocol is quietly ignored

Troubleshooting — OSPF is up but traffic ignores it
Symptom: R2 runs OSPF, learns 10.1.5.0/24 from a neighbour, and you expect traffic to follow the OSPF path. Instead it leaves via an old link. The adjacency is full and the route is being advertised. Diagnosis: run show ip route 10.1.5.20 and read the Known via line — it says static, not OSPF. Both sources offer the identical prefix, so LPM ties and AD decides: static AD 1 beats OSPF AD 110, so the OSPF route sits in the database and never reaches the RIB. A leftover ip route command is silently shadowing the dynamic route. Fix: decide which path you actually want. Remove the stale static and OSPF installs, or, if you want the static as a backup, give it an AD above 110 so it becomes a floating static and OSPF wins while healthy. Root cause: when a dynamic route is not being used, look for a lower-AD static covering the same prefix before suspecting the protocol.

What you should retain

  • Fixed order: longest-prefix match, then administrative distance, then metric — stop at the first test that decides.
  • LPM first: the most specific matching prefix wins regardless of AD or metric.
  • The default 0.0.0.0/0 is the least specific route and therefore the last resort.
  • AD applies only to the same prefix from different sources: connected 0, static 1, EIGRP 90, OSPF 110, RIP 120, external EIGRP 170.
  • Metric applies only within one protocol; comparing an OSPF cost to an EIGRP metric is meaningless.
  • show ip route <destination> reveals the single entry that survived all three tests.
Pause and predictNot scored — nothing is recorded

Before you read on

Given: S* 0.0.0.0/0 [1/0]; O 10.1.0.0/16 [110/20]; S 10.1.5.0/24 [1/0]; O 10.1.5.0/24 [110/30]; D 172.16.0.0/16 [90/2816]. Name the winner and the deciding test for each destination. (1) 10.1.5.40 (2) 10.1.9.20 (3) 172.16.4.4 and 8.8.8.8.

Interactive tool
Required~10 min

Routing Table Trainer — hard mode

For each destination apply LPM, then AD, then metric, and check your winner against the tool. Hard mode deliberately includes overlapping prefixes from different sources, which is where the order stops being obvious and starts being a skill.

Study deeper

Topic guides extend this lesson — they do not replace the first-party walkthrough above.

Routing Protocols

For the full AD table and the LPM-versus-AD distinction, which returns as soon as OSPF arrives in Week 5