SkillPilotAI

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

One router hop
  1. 1.PC 192.168.69.10 -> dst 192.168.69.70: not in my /26, send to gateway R1

  2. 2.R1 looks up 192.168.69.70: match 192.168.69.64/26 -> eth1

  3. 3.R1 decrements TTL 64 -> 63

  4. 4.R1 builds a new Ethernet header (new MACs, same IPs)

  5. 5.Packet reaches the server with ttl=63

  6. 6.No match and no default: drop + ICMP destination unreachable to the sender

49.3 Routing table columns

ColumnMeaningExample
Destination / prefixWhich network192.168.69.64/26
Gateway (next hop)IP of the next router, or direct192.168.69.67 or 0.0.0.0
InterfacePort the packet leaves frometh1
MetricCost of the route, lower = better10

Windows route print shows the same columns with a separate Netmask.

49.4 Types of routes

Route typeWho adds itExample
ConnectedAutomatically when an interface gets an IP (proto kernel, no gateway)192.168.69.64/26 dev eth1
StaticAdmin, by hand192.168.69.128/26 via 192.168.69.67
DefaultAdmin / DHCP; matches everything else0.0.0.0/0 via 192.168.69.1
Host routeA /32 for one address192.168.69.140/32 via 192.168.69.68

49.5 Practical: the PC's routing table

Terminal
ip routeroute -n
Output
$ 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

Terminal
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
Output
$ 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 ms

No 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.
Terminal
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
Output
$ 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 ms

49.8 Longest prefix match

Which route wins on R1?
  1. 1.Table: 0.0.0.0/0 via .1, 192.168.69.128/26 via .67, 192.168.69.140/32 via .68

  2. 2.dst 192.168.69.140 matches /0, /26 and /32: longest (/32) wins -> R3 (.68)

  3. 3.dst 192.168.69.150 matches /0 and /26: /26 wins -> R2 (.67)

  4. 4.dst 8.8.8.8 matches only /0: default -> Docker gateway (.1)

  5. 5.Same prefix, several routes: lower metric (after AD) wins

Terminal
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
Output
$ 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 0

49.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.