How Does Routing Work? The Routing Table Explained
Routing is how a router moves data between separate networks by reading each packet's destination IP address. When a packet arrives, the router looks that destination up in its routing table, picks the best matching entry, and forwards the packet toward the next network that gets it closer to the goal — a decision made at Layer 3 of the OSI model, one hop at a time.
Part of the Static & Default Routing learning hub
Think of a router as a postal sorting office: it doesn't care what's inside the envelope, only where it's addressed. It reads the address, hands the envelope to the next office along the route, and trusts that office to do the same until the letter arrives.
What Routing Actually Is
Routing is the job of forwarding packets between different networks based on their destination IP address. This happens at Layer 3 (the Network layer) of the seven-layer OSI model — the layer that deals with logical addressing. An IP address is a Layer 3 address: 32 bits in IPv4, 128 bits in IPv6.
It helps to contrast a router with a switch. A switch works at Layer 2 and forwards frames using MAC addresses (48-bit hardware addresses burned into each network card). A switch delivers traffic locally, within a single network. A router sits between networks and decides which way a packet should leave to reach a network it is not directly part of. Switching is local delivery on one street; routing is getting a package across town to a different street entirely.
Why Routing Exists: The Problem It Solves
Networks are broken up into smaller pieces called subnets. Devices on the same subnet can talk to each other directly. But the internet is made of an enormous number of separate networks, and no single device could ever store the physical location of every other network on Earth.
Routing solves this by letting each router know just enough. A router doesn't need the whole map — it only needs to know, for each destination network it has heard of, which direction to send the packet next. Packets then hop from router to router, each one nudging the packet closer, until it arrives. It's like driving to a distant city by following one road sign at a time rather than memorizing the entire journey in advance.
How a Router Builds Its Routing Table
The routing table is the router's map of known destination networks, and for each one it records the next hop and the interface to send traffic out of. A router fills this table from three kinds of sources.
Connected routes appear automatically for the networks a router is directly attached to — as soon as an interface is up and has an IP address, the router knows that network firsthand. Static routes are entered by hand by an administrator who says, in effect, 'to reach that network, go this way.' Dynamic routes are learned automatically from routing protocols such as OSPF, a link-state protocol where routers exchange information with their neighbors and calculate paths for themselves. (The exact commands for each live in our step-by-step configuration guides — here we're focused on the concept.)
Reading the Table: Longest-Prefix Match
A single packet can match more than one entry in the routing table. When that happens, the router always chooses the most specific match — the one with the longest prefix, meaning the most network bits, or the biggest subnet mask. This rule is called longest-prefix match.
The subnet mask is what defines that specificity: it splits an address into network bits and host bits, and more network bits means a more precise route. For a packet headed to 10.1.1.5, a route to 10.1.1.0/24 wins over a broader route to 10.0.0.0/8, because /24 describes a smaller, more exact destination. It's the difference between an address that says '123 Main Street, Apartment 4B' and one that just says 'the state of Texas' — the more detailed one is used.
Breaking Ties: Administrative Distance and Metric
Sometimes a router learns two routes to the exact same network from two different sources. The first tiebreaker is administrative distance (AD), which ranks how trustworthy each source is. Connected routes are trusted most, static routes next, and dynamically learned routes after that. The route with the lower administrative distance wins.
If two routes to the same network come from the same source — for example, two paths both learned through OSPF — administrative distance can't separate them, so the router uses the metric instead. The metric measures how 'good' a path is according to that protocol; OSPF uses cost, calculated by the SPF (Dijkstra) algorithm. In short: administrative distance chooses between different sources, and the metric chooses between paths from the same source.
The Gateway of Last Resort and Hop-by-Hop Forwarding
What if a packet matches nothing in the table? Rather than drop it, a router can fall back to a default route, written as 0.0.0.0/0 — the catch-all that matches any destination. This is the gateway of last resort: 'if you don't know where it goes, send it here,' usually toward the internet-facing router or ISP. Your own computer's default gateway is exactly this idea — the next hop for any traffic that isn't on your local subnet.
Finally, remember that forwarding is hop-by-hop. Each router makes its own independent decision, choosing only the next hop, not the whole route. No single router knows the entire path from source to destination. It works like a relay race: each runner carries the baton just one leg and hands it off, and no runner needs to run the whole track.
Frequently asked questions
What's the difference between routing and switching?
They operate at different layers and use different addresses. A switch works at Layer 2 and forwards frames within a single network using 48-bit MAC addresses. A router works at Layer 3 and forwards packets between different networks using IP addresses. Put simply, switching handles local delivery inside one network, while routing moves traffic from one network to another.
Why does my computer need a default gateway?
Your computer can reach other devices on its own subnet directly, but it has no idea how to reach networks beyond it. The default gateway is the router it hands off-subnet traffic to — the device that does know how to forward packets toward other networks. It corresponds to the default route 0.0.0.0/0, the 'send anything I don't recognize here' entry. Without it, your device could talk locally but couldn't reach the wider internet.
Does a router know the entire path to a destination?
No. Routing is hop-by-hop, so each router only decides the next hop and trusts the following routers to keep the packet moving. A router's table tells it which direction to send traffic for each known network, not the full end-to-end route. The complete path emerges from many routers each making their own local decision, like a chain of relay runners who each carry the baton one leg.
Now build it on real Cisco IOS
Concepts stick when you configure them. Try a free graded lab, then grade your own config against the answer key.