How Routing Works: Routing Tables, Static Routes, RIP, OSPF and BGP
Learn how routers forward packets: routing tables, longest prefix match, static vs dynamic routing, and how RIP, OSPF, EIGRP and BGP choose the best path.
Routing is the process of choosing a path for packets to travel from one network to another. Every router looks at a packet's destination IP, checks its routing table and forwards the packet one hop closer to its destination.
Inside a routing table#
A routing table lists destination networks and how to reach them:
Destination Next hop Interface Metric
0.0.0.0/0 203.0.113.1 wan0 1 (default route)
10.0.0.0/8 10.255.0.1 eth1 20
10.20.0.0/16 10.255.0.2 eth2 10
192.168.1.0/24 directly eth0 0Longest prefix match#
When several routes match, the router picks the most specific one – the longest prefix. A packet to 10.20.5.9 matches both 10.0.0.0/8 and 10.20.0.0/16; the router uses the /16. If nothing else matches, the default route 0.0.0.0/0 catches it.
Choosing the best path#
Routers often have several ways to reach a destination. Routing protocols assign a metric (hop count, bandwidth-based cost, delay) and choose the lowest. In the diagram, A → B → D → F costs 1 + 2 + 1 = 4, cheaper than A → C → E → F (4 + 1 + 3 = 8).
When two protocols offer a route to the same network, the router trusts the one with the lower administrative distance (e.g. connected 0, static 1, OSPF 110, RIP 120 on Cisco devices).
Static vs dynamic routing#
| Static | Dynamic | |
|---|---|---|
| Configured by | An administrator, manually | Routers sharing information automatically |
| Adapts to failures | No | Yes – reroutes around broken links |
| Overhead | None | CPU, memory and protocol traffic |
| Best for | Small networks, default routes, stubs | Medium to large networks |
Routing protocols#
Interior gateway protocols (inside one organisation)#
- RIP – a distance-vector protocol. Routers share their tables with neighbours periodically and use hop count as the metric (max 15). Simple but slow to converge; mostly legacy today.
- OSPF – a link-state protocol. Every router learns the full topology of its area and runs Dijkstra's shortest-path algorithm. Converges fast, scales with areas, and uses a bandwidth-based cost. The most common open IGP.
- EIGRP – an advanced distance-vector protocol, originally Cisco-proprietary, using bandwidth and delay with fast failover.
- IS-IS – another link-state protocol, popular with large ISPs.
Exterior gateway protocol: BGP#
BGP (Border Gateway Protocol) is the routing protocol of the internet. It connects Autonomous Systems (AS) – networks run by ISPs, cloud providers and large companies, each with an AS number.
- BGP is a path-vector protocol: routes carry the list of ASes they pass through, which prevents loops.
- Decisions are driven by policy (business relationships, preferences) rather than pure speed.
- Mistakes in BGP announcements can – and occasionally do – take large parts of the internet offline.
Run traceroute (or mtr for a live view) to see every router hop your packets take and where latency jumps.
# Linux: show routes and add a static route
ip route show
sudo ip route add 10.50.0.0/16 via 192.168.1.254
# Which route would be used for this destination?
ip route get 8.8.8.8Key takeaways#
- Routers forward packets using a routing table and longest prefix match.
- Static routes are simple; dynamic protocols adapt to failures.
- RIP uses hop count, OSPF uses link-state with Dijkstra, BGP uses path vectors and policy.
- BGP connects autonomous systems and holds the internet together.