Routing is the job of moving a packet from one network to another, hop by hop, until it reaches the destination’s network. No router knows the whole path. Each one only answers: “for this destination, which neighbor do I hand the packet to next?” The answer comes from its routing table.
What is in a routing table
Every entry says: “to reach this prefix, send to this next hop out of this interface“, plus some numbers to choose between options.
$ ip route
default via 192.168.1.1 dev enp3s0 proto dhcp metric 100
10.8.0.0/24 dev wg0 proto kernel scope link src 10.8.0.2
10.20.0.0/16 via 10.8.0.1 dev wg0
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
192.168.1.0/24 dev enp3s0 proto kernel scope link src 192.168.1.42 metric 100
| Route | Kind | Meaning |
|---|---|---|
192.168.1.0/24 dev enp3s0 | Connected | My own LAN; deliver directly with ARP |
10.20.0.0/16 via 10.8.0.1 | Static | Office networks are behind the VPN peer |
default via 192.168.1.1 | Default (0.0.0.0/0) | Everything else goes to the home router |
Windows shows the same with route print or Get-NetRoute.
Longest prefix match
When several routes match a destination, the router picks the most specific one: the longest prefix wins, regardless of the order of the table.
For destination 10.20.5.9 on the table above:
0.0.0.0/0matches (everything does) — 0 bits.10.20.0.0/16matches — 16 bits. Winner.
Ask the kernel which route it would use:
$ ip route get 10.20.5.9
10.20.5.9 via 10.8.0.1 dev wg0 src 10.8.0.2 uid 1000
$ ip route get 1.1.1.1
1.1.1.1 via 192.168.1.1 dev enp3s0 src 192.168.1.42 uid 1000
ip route get is the single most useful routing command: it removes all guessing.
Choosing between equal prefixes
If two sources offer the same prefix, the router uses two tie-breakers:
- Administrative distance (trust in the source, lower is better). Typical Cisco values: connected 0, static 1, eBGP 20, EIGRP 90, OSPF 110, IS-IS 115, RIP 120, iBGP 200.
- Metric (cost within one protocol, lower is better): hop count for RIP, a bandwidth-based cost for OSPF, path attributes for BGP. On Linux, the
metricfield plays this role between interfaces, which is why a wired link (metric 100) beats Wi-Fi (metric 600).
If two routes tie on everything, many routers use both: ECMP (equal-cost multipath) spreads flows across them.
Static routing
You write routes by hand.
$ sudo ip route add 10.30.0.0/16 via 192.168.1.254
$ sudo ip route replace default via 192.168.1.1 dev enp3s0
$ sudo ip route del 10.30.0.0/16
These disappear at reboot; make them permanent in netplan, NetworkManager (nmcli connection modify … +ipv4.routes "10.30.0.0/16 192.168.1.254") or systemd-networkd.
| Static routing | Dynamic routing |
|---|---|
| Configured by hand | Routers learn routes from each other |
| No protocol overhead, fully predictable | Uses some CPU, memory and bandwidth |
| Does not react to failures (unless tracked) | Reroutes around failures automatically |
| Fine for small or stub networks, default routes | Needed for anything with redundancy or many networks |
| Easy to make mistakes at scale | Scales to the whole internet |
A stub network (a branch with one way out) only needs a default route. A floating static route with a higher administrative distance makes a cheap backup: it only appears if the dynamic route vanishes.
Dynamic routing: two families
Distance vector
Each router tells its neighbors “here are the networks I can reach and how far away they are”. Neighbors add their own cost and pass it on. Every router knows the distance and direction (vector) to each network, but not the topology. This is the Bellman-Ford approach.
- Simple and light.
- Slow to converge after failures, and prone to routing loops and the “count to infinity” problem.
- Fixes: split horizon (do not advertise a route back where you learned it), route poisoning (advertise a dead route with an infinite metric), hold-down timers, and a maximum hop count (RIP gives up at 16).
- Example: RIP. EIGRP is an advanced distance vector protocol that fixed most of these problems.
Link state
Each router floods a description of its own links to every router in the area. Every router builds the same full map of the network (the link-state database) and runs Dijkstra’s shortest path first algorithm to compute the best path to every destination.
- Fast convergence, no loops by design, every router sees the whole picture.
- Needs more memory and CPU, and careful design (areas or levels) on big networks.
- Examples: OSPF and IS-IS.
| Distance vector | Link state | |
|---|---|---|
| Shares | Its routing table, with neighbors | Its links, with everyone in the area |
| Knows | Distance and direction | The full topology |
| Algorithm | Bellman-Ford | Dijkstra (SPF) |
| Convergence | Slow | Fast |
| Examples | RIP (EIGRP is an advanced variant) | OSPF, IS-IS |
BGP, the protocol between internet providers, is a third kind: path vector. It shares the full list of networks (autonomous systems) a route crosses and chooses paths by policy rather than shortest distance. Lesson 12 covers all of these protocols.
Interior and exterior
- IGP (interior gateway protocol): routing inside one organization: OSPF, IS-IS, EIGRP, RIP.
- EGP (exterior gateway protocol): routing between organizations: BGP.
Watch routing happen
traceroute and mtr show the routers along a path. ip route get tells you the first hop. On a Linux router running FRRouting, vtysh -c 'show ip route' adds the source protocol of each route:
$ sudo vtysh -c 'show ip route'
Codes: K - kernel route, C - connected, S - static, R - RIP,
O - OSPF, B - BGP, > - selected route, * - FIB route
S>* 0.0.0.0/0 [1/0] via 203.0.113.1, eth0, 2d01h
C>* 10.0.12.0/30 is directly connected, eth1, 2d01h
O>* 10.0.34.0/30 [110/20] via 10.0.12.2, eth1, 01:12:09
B>* 198.51.100.0/24 [20/0] via 203.0.113.1, eth0, 2d00h
The numbers in brackets are [administrative distance/metric]: [110/20] is an OSPF route with cost 20.
Linux as a router
A Linux machine only forwards packets between interfaces if you allow it:
$ sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0
$ sudo sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
Make it permanent in /etc/sysctl.d/. Docker and Kubernetes turn this on for you, which is how containers reach the outside world.
Exercise
A router has routes for 10.0.0.0/8, 10.1.0.0/16 and 10.1.2.0/24. Which one does it use for 10.1.2.77?
Show answer
Answer: C. The longest prefix match wins. Order of entries does not matter, and ECMP only applies to equal prefixes with equal cost.
Exercise
A router learns 172.16.0.0/16 from both OSPF and RIP. With default administrative distances, which route goes into the table?
Show answer
Answer: A. For the same prefix, the source with the lower administrative distance wins. Metrics are only compared within one protocol.
Exercise
Which statement describes link-state routing?
Show answer
Answer: D. OSPF and IS-IS flood link-state information so every router has the same topology. Sharing tables with neighbors is distance vector; policy-based is BGP.
Key takeaways
- A routing table maps prefixes to next hops; the default route catches everything else.
- The longest matching prefix wins; then administrative distance; then metric.
ip route get <address>tells you exactly which route the kernel will use.- Static routes are simple but do not react to failures; dynamic protocols reroute automatically.
- Distance vector shares tables with neighbors; link state shares links with everyone; BGP is path vector.
FAQ
What is the difference between routing and forwarding?
Routing is building the table (by hand or with protocols). Forwarding is the per-packet act of looking up the destination and sending it out. Routers do routing in software and forwarding in hardware, as fast as possible.
Why does my laptop have two default routes?
One for each connected interface, such as Ethernet and Wi-Fi. The lower metric wins, which is why plugging in a cable usually takes over from Wi-Fi.
What is policy-based routing?
Choosing a route by more than the destination: source address, port or a firewall mark. Linux does it with ip rule and multiple routing tables, for example to send one application through a VPN.
What is a route “flap”?
A route that keeps appearing and disappearing, usually because of an unstable link. Each change makes routers recompute, so protocols dampen flapping routes and summarization hides them from the rest of the network.