Routing Table
When you send a packet to 8.8.8.8, how does Linux know which network interface to use and where to send it next? The routing table. It's a list of rules that maps destination addresses to next-hops and interfaces. Understanding it explains everything from default gateways to VPN routing to Kubernetes pod networking.
Reading the Routing Table
# Modern tool: ip route (replaces 'route -n')
ip route show
# default via 192.168.1.1 dev eth0 proto dhcp src 192.168.1.100 metric 100
# 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
# 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100
# Reading each field:
# default = 0.0.0.0/0 — matches everything not matched elsewhere
# via 192.168.1.1 = next hop (gateway) — send to this IP first
# dev eth0 = outbound interface
# proto dhcp = how this route was learned (dhcp/kernel/static/ospf/bgp)
# src 192.168.1.100 = preferred source IP when sending on this route
# metric 100 = preference (lower = preferred when multiple routes match)
# Show route for specific destination (which route would be used?):
ip route get 8.8.8.8
# 8.8.8.8 via 192.168.1.1 dev eth0 src 192.168.1.100 uid 0
Longest Prefix Match — How Routes Are Chosen
If multiple routes match a destination, which one wins?
The most specific route — the one with the longest prefix (most bits matching). A /24 route beats a /16 route beats a /0 (default) route. This is why VPNs work: the VPN client adds specific routes (e.g., 10.0.0.0/8) that are more specific than the default route, so VPN-bound traffic takes those routes while internet traffic uses the default.
Example routing table:
10.0.0.0/8 via 10.255.0.1 ← corporate VPN
10.1.2.0/24 dev eth1 ← direct local subnet
192.168.1.0/24 dev eth0 ← home network
default via 192.168.1.1 ← internet
Destination: 10.1.2.50
Matches: 10.0.0.0/8 (8-bit prefix)
Matches: 10.1.2.0/24 (24-bit prefix) ← WINNER (most specific)
Result: sent via eth1 directly
Destination: 10.5.0.1
Matches: 10.0.0.0/8 (8-bit prefix) ← WINNER
Result: sent via VPN gateway
Destination: 8.8.8.8
Matches: default (0-bit prefix) ← only match
Result: sent via 192.168.1.1
Managing Routes
# Add a static route
ip route add 10.10.0.0/24 via 192.168.1.254
ip route add 10.10.0.0/24 dev eth1 # direct (no gateway needed)
# Delete a route
ip route del 10.10.0.0/24
# Change default gateway
ip route del default
ip route add default via 192.168.1.1
# Add route with metric (backup route — higher metric = lower priority)
ip route add default via 192.168.1.1 metric 100 # primary
ip route add default via 10.0.0.1 metric 200 # backup
# Persistent routes — different per distro:
# Debian/Ubuntu: /etc/network/interfaces or Netplan
# RHEL/Fedora: nmcli or /etc/sysconfig/network-scripts/route-eth0
# Netplan (Ubuntu): add 'routes:' section in /etc/netplan/01-netcfg.yaml
Multiple Routing Tables and Policy Routing
Can you have different routing rules for different processes or source IPs?
Yes — Linux supports multiple routing tables (up to 255) and "ip rules" that select which table to use based on source IP, destination IP, firewall marks, or interface. This is how multi-homed servers work: traffic from one public IP uses routing table 100, traffic from another public IP uses table 101. Used heavily in Kubernetes (kube-proxy) and VPNs.
# Show routing policy rules (which table to use for which traffic):
ip rule show
# 0: from all lookup local ← highest priority: local addresses
# 32766: from all lookup main ← the normal routing table
# 32767: from all lookup default ← fallback (usually empty)
# Add a rule: traffic from 192.168.2.0/24 use routing table 200
ip rule add from 192.168.2.0/24 table 200
# Configure routing table 200
ip route add default via 10.0.0.1 table 200
ip route add 192.168.2.0/24 dev eth1 table 200
# Named tables: /etc/iproute2/rt_tables
# 253 default
# 254 main
# 255 local
# Add custom: echo "200 isp2" >> /etc/iproute2/rt_tables
Frequently Asked Questions
What will I learn here?
This page covers the core concepts and techniques you need to understand the topic and progress confidently to the next lesson.
How should I use this page?
Start with the overview, then follow the section links to deepen your understanding. Use the table of contents on the right to jump to specific sections.
What should I read next?
Use the navigation below to continue to the next lesson or explore related topics.