Router and the routing table
A router connects networks and decides the next hop of every packet by looking up the destination IP in its routing table. Knowing how that lookup works (connected, static, default routes, longest prefix match, metric) explains most 'why does traffic go there?' questions.
49.1 Why is it used?
- A PC can only reach its own subnet directly; everything else goes to the default gateway (the router).
- PC 192.168.69.10 (/26) sees that 192.168.69.70 is not in its LAN, so it sends the packet to R1.
- R1 matches the destination IP in its table and forwards the packet out of the right interface.
49.2 How it works: forwarding
- Step 1
1.PC 192.168.69.10 -> dst 192.168.69.70: not in my /26, send to gateway R1
- Step 2
2.R1 looks up 192.168.69.70: match 192.168.69.64/26 -> eth1
- Step 3
3.R1 decrements TTL 64 -> 63
- Step 4
4.R1 builds a new Ethernet header (new MACs, same IPs)
- Step 5
5.Packet reaches the server with ttl=63
- Step 6
6.No match and no default: drop + ICMP destination unreachable to the sender
49.3 Routing table columns
| Column | Meaning | Example |
|---|---|---|
| Destination / prefix | Which network | 192.168.69.64/26 |
| Gateway (next hop) | IP of the next router, or direct | 192.168.69.67 or 0.0.0.0 |
| Interface | Port the packet leaves from | eth1 |
| Metric | Cost of the route, lower = better | 10 |
Windows route print shows the same columns with a separate Netmask.
49.4 Types of routes
| Route type | Who adds it | Example |
|---|---|---|
| Connected | Automatically when an interface gets an IP (proto kernel, no gateway) | 192.168.69.64/26 dev eth1 |
| Static | Admin, by hand | 192.168.69.128/26 via 192.168.69.67 |
| Default | Admin / DHCP; matches everything else | 0.0.0.0/0 via 192.168.69.1 |
| Host route | A /32 for one address | 192.168.69.140/32 via 192.168.69.68 |
49.5 Practical: the PC's routing table
ip routeroute -n$ ip routedefault via 192.168.69.1 dev eth0192.168.69.0/26 dev eth0 proto kernel scope link src 192.168.69.10$ route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.69.1 0.0.0.0 UG 0 0 0 eth0192.168.69.0 0.0.0.0 255.255.255.192 U 0 0 0 eth0- proto kernel = the system added the connected route itself.
- Flags: U = up, G = via a gateway; gateway 0.0.0.0 = directly connected.
49.6 Practical: remove a route, add a route
ip route del defaultping -c 2 -W 1 192.168.69.70ip route add 192.168.69.64/26 via 192.168.69.2ping -c 2 192.168.69.70ip route del 192.168.69.64/26ip route add default via 192.168.69.2ping -c 2 192.168.69.70$ ip route del default$ ping -c 2 -W 1 192.168.69.70ping: connect: Network unreachable$ ip route add 192.168.69.64/26 via 192.168.69.2$ ping -c 2 192.168.69.7064 bytes from 192.168.69.70: icmp_seq=1 ttl=63 time=0.456 ms64 bytes from 192.168.69.70: icmp_seq=2 ttl=63 time=0.283 ms2 packets transmitted, 2 received, 0% packet loss, time 1002ms$ ip route del 192.168.69.64/26$ ip route add default via 192.168.69.2$ ping -c 2 192.168.69.7064 bytes from 192.168.69.70: icmp_seq=1 ttl=63 time=0.160 msNo matching route gives 'Network unreachable' immediately; ttl=63 shows one router in between.
49.7 Static routes and metric on R1
- ip route get <dst> shows which route will be used.
- Before adding routes, 192.168.69.140 matched only the default route (towards the Docker gateway).
- Two routes to the same prefix: via R2 metric 10 and via R3 metric 20; the lower metric wins.
- Delete the metric-10 route and the backup via R3 is used at once, with no change on the PC.
ip route get 192.168.69.140ip route add 192.168.69.128/26 via 192.168.69.67 metric 10ip route add 192.168.69.128/26 via 192.168.69.68 metric 20route -nip route del 192.168.69.128/26 via 192.168.69.67 metric 10ip route get 192.168.69.140$ ip route get 192.168.69.140192.168.69.140 via 192.168.69.1 dev eth0 src 192.168.69.2 uid 0$ route -nDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.69.1 0.0.0.0 UG 0 0 0 eth0192.168.69.0 0.0.0.0 255.255.255.192 U 0 0 0 eth0192.168.69.64 0.0.0.0 255.255.255.192 U 0 0 0 eth1192.168.69.128 192.168.69.67 255.255.255.192 UG 10 0 0 eth1192.168.69.128 192.168.69.68 255.255.255.192 UG 20 0 0 eth1pc$ traceroute -n -q 1 -w 1 192.168.69.140 1 192.168.69.2 0.015 ms 2 192.168.69.67 0.008 ms 3 192.168.69.140 0.010 ms# after deleting the metric 10 router1$ ip route get 192.168.69.140192.168.69.140 via 192.168.69.68 dev eth1 src 192.168.69.66 uid 0pc$ traceroute -n -q 1 -w 1 192.168.69.140 1 192.168.69.2 0.011 ms 2 192.168.69.68 0.010 ms 3 192.168.69.140 0.008 ms49.8 Longest prefix match
- Step 1
1.Table: 0.0.0.0/0 via .1, 192.168.69.128/26 via .67, 192.168.69.140/32 via .68
- Step 2
2.dst 192.168.69.140 matches /0, /26 and /32: longest (/32) wins -> R3 (.68)
- Step 3
3.dst 192.168.69.150 matches /0 and /26: /26 wins -> R2 (.67)
- Step 4
4.dst 8.8.8.8 matches only /0: default -> Docker gateway (.1)
- Step 5
5.Same prefix, several routes: lower metric (after AD) wins
ip route add 192.168.69.140/32 via 192.168.69.68ip route get 192.168.69.140ip route get 192.168.69.150ip route get 8.8.8.8$ ip route get 192.168.69.140192.168.69.140 via 192.168.69.68 dev eth1 src 192.168.69.66 uid 0$ ip route get 192.168.69.150192.168.69.150 via 192.168.69.67 dev eth1 src 192.168.69.66 uid 0$ ip route get 8.8.8.88.8.8.8 via 192.168.69.1 dev eth0 src 192.168.69.2 uid 049.9 Common mistakes
- Thinking the router knows the full path: it only decides the next hop; every hop does a fresh lookup.
- A wrong route in the middle can loop packets until TTL reaches 0.
- Comparing metrics across different prefixes: prefix length is checked first, metric only breaks ties on the same prefix.
- Deleting the default route on a host and wondering why everything outside the LAN is 'Network unreachable'.
49.10 Interview tips
- Route selection order: longest prefix match, then administrative distance, then metric.
- No match: default route; no default either: drop + ICMP destination unreachable.
- Example: 10.0.0.0/8 via A, 10.1.0.0/16 via B, default via C: 10.1.5.9 -> B, 10.2.3.4 -> A.
- IPs stay the same end to end, MACs change every hop, TTL drops by 1 per router.