Routing & Gateways¶
Addressing tells you where something is; routing is the separate question of how a packet actually gets there. A host doesn't know a path to every address on the internet — it knows a small routing table and one important shortcut: its default gateway.
TL;DR — in 30 seconds:
- Same network → the host delivers directly; different network → it hands the packet to its default gateway, which is always hop 1, never the last hop.
- The routing table picks the most specific matching entry, regardless of insertion order; the default
route (
0.0.0.0/0) is the catch-all least-specific entry. - Routing and addressing only get a packet to the right host — the port number picks which service on that host receives it.
1. Local vs remote delivery¶
Every host decides, for every outbound packet, whether the destination is on its own local network or somewhere else:
- Same network (same network portion, from the addressing page) → the host delivers the packet directly, no router involved.
- Different network → the host can't reach it directly, so it hands the packet to its default gateway — a router that knows the next step of the path.
flowchart LR
H["Host<br/>10.0.1.42/24"]
L["10.0.1.99<br/>(same network)"]
G["Default gateway<br/>10.0.1.1"]
R["8.8.8.8<br/>(remote network)"]
H -->|"local — direct delivery"| L
H -->|"remote — via gateway"| G
G --> R
2. The routing table¶
A routing table is the list every host and router consults to make that local-vs-remote call — and,
for a router, to decide which next hop gets a packet closer to a remote destination. Each entry is a
(destination network → next hop) pair. When more than one entry could match, the most specific match
wins — a narrower destination range (e.g. /32, a single address) beats a broader one (e.g. the /0
default route), regardless of which order the entries were added in.
The default route (0.0.0.0/0) is the table's catch-all: "anything not matched by a more specific entry,
send here" — almost always the default gateway.
3. Gateways and hops¶
A gateway (or router) is a device that forwards packets between networks it's connected to. A packet crossing from your laptop to a server on the other side of the internet crosses many routers in sequence — each one is a hop. Your default gateway is always hop 1: the first router your traffic reaches leaving your own network.
flowchart LR
You["Your host"] -->|hop 1| GW["Default gateway"]
GW -->|hop 2| R1["ISP router"]
R1 -->|hop 3| R2["…more routers…"]
R2 -->|hop N| Dest["Destination server"]
Tools like traceroute/tracepath make this chain visible by sending probes that expire one hop farther
each time, forcing every router along the path to reveal itself.
4. Ports — routing gets you to the host, not the service¶
Routing and addressing get a packet to the right host — they say nothing about which program on that host should receive it. That's the job of the port number: a 16-bit number (0–65535) that, combined with the IP address and the transport protocol (TCP or UDP), uniquely identifies one conversation. A single host can run many services at once (a web server on port 443, an SSH daemon on port 22) because each one listens on its own port.
| Port | Common service |
|---|---|
| 22 | SSH |
| 53 | DNS |
| 80 | HTTP |
| 443 | HTTPS |
5. How this connects¶
- This page is the general routing/gateway mechanic behind the AWS module's Internet-Gateway-vs-NAT-Gateway material — AWS's VPC networking page (module 05, AWS, §3) is this same idea, specialized for one cloud's managed routing constructs.
- Next: DNS — how a name gets turned into the address that routing actually operates on.
Common gotchas
- Assuming routing-table entries are checked in the order they were added. Fix: they're not — the most specific match wins, regardless of insertion order. A narrower entry always beats a broader one for the same destination.
- Treating the default route as just another entry that might "win." Fix:
0.0.0.0/0is the least specific entry possible — it only ever matches when nothing more specific does, which is exactly what makes it a safe catch-all. - Thinking your default gateway is the last stop before the destination. Fix: it's always hop 1 — the first router your traffic reaches leaving your own network — not the final one; a packet crossing the internet passes through many more hops after it.
- Assuming reaching the right host means reaching the right service. Fix: routing and addressing only get a packet to the right host; the port number is what picks which program on that host receives it.
Check yourself — your routing table has both a route to 10.0.1.0/24 and the default route 0.0.0.0/0. A packet is headed to 10.0.1.50. Which entry wins, and why?
The 10.0.1.0/24 route wins. The most specific matching entry always wins regardless of when it was
added, and the default route is the least specific entry in the table — it only catches traffic nothing
more specific matches.
Check yourself — why is your default gateway always called 'hop 1,' even when the destination is many networks away?
Because a hop count is relative to your path, not the destination's distance: your default gateway is defined as the first router your traffic reaches after leaving your own local network. Every router the packet crosses after that is hop 2, hop 3, and so on — the gateway isn't the last stop, it's the first.
You can defend this when you can explain why a packet to a local address never touches your default gateway, why the default route is the least specific entry in the table (and therefore loses to anything more specific), and what a hop actually is.