What is a routing table and what information does it contain?
The routing table maps destination networks to "send it out interface X (directly, or via gateway Y)"; the most specific match wins, and 0.0.0.0/0 is the catch-all default.
* The kernel picks the route whose prefix most specifically contains the destination (longest-prefix match); 0.0.0.0/0 is only the catch-all default. *
For every outgoing packet the kernel consults this table to answer "where next?". It looks for the entry whose network most specifically contains the destination (longest-prefix match). If the destination is on a directly-connected network, it's delivered straight out that interface; otherwise it's handed to a gateway (a router) to forward onward.
| Destination | Interface | Gateway |
|---|---|---|
| 192.168.2.0/24 | wlo1 | — (direct) |
| 192.168.5.0/24 | enp0s3 | — (direct) |
| 0.0.0.0/0 (default) | enp0s3 | 192.168.2.1 |
Three concepts to hold:
- Direct route — destination is on a locally-connected subnet, no gateway needed
- Gateway route — destination is elsewhere, so forward to a router's IP
- Default route (
0.0.0.0/0) — matches anything not matched above; this is "send everything else to the internet via my router"
View it:
ip route # modern (iproute2)
route -n # legacy
Tip: the default route's gateway is your router's LAN IP — lose that entry and you can reach your local subnet but nothing on the wider internet.
Go deeper:
ip-route(8) man page (man7.org) — the iproute2 reference:
default(0/0),viagateway, and longest-prefix routing.