Routing
Routing is the process of determining the path that data packets take from source to destination across a network. On Linux, the kernel maintains a routing table that directs packets to the appropriate network interface or next hop.
Routing Tables
The kernel routing table maps destination networks to next hops and interfaces.
View the routing table with ip:
ip route show
A typical routing table contains:
default via 192.168.1.1 dev eth0 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10 172.16.0.0/16 via 10.0.0.1 dev eth1 10.0.0.0/8 via 10.0.0.1 dev eth1 metric 100
- default: The default route (gateway of last resort).
- 192.168.1.0/24: Directly connected network (no gateway needed).
- 172.16.0.0/16: Route via next hop 10.0.0.1.
Route Types
- unicast: Standard point-to-point route.
- broadcast: Deliver to all hosts on a network.
- unreachable: Destination is unreachable (ICMP unreachable returned).
- prohibit: Destination is forbidden (ICMP prohibited returned).
- local: Destination is assigned to a local interface.
- blackhole: Packets are silently dropped (used for filtering).
- throw: Route lookup continues without finding a match (policy routing).
Static Routes
Add a static route:
ip route add 10.0.0.0/8 via 192.168.1.254
Remove a static route:
ip route del 10.0.0.0/8
Dynamic Routing
For complex networks with redundant paths, dynamic routing protocols automatically compute routes:
- RIP (Routing Information Protocol): Distance-vector protocol, simple but limited to 15 hops.
- OSPF (Open Shortest Path First): Link-state protocol, fast convergence, suitable for large networks.
- BGP (Border Gateway Protocol): Path-vector protocol, used for internet routing between autonomous systems.
Linux implementations include quagga (FRR), bird, and bgpd.
Policy Routing
Linux supports policy-based routing, where different routing tables are selected based on source address, firewall mark, or incoming interface.
Create an additional routing table in /etc/iproute2/rt_tables:
200 custom
Add rules:
ip rule add from 192.168.1.10 table custom ip route add default via 10.0.0.1 table custom
Metrics and Multipath
Routes can have metrics (administrative distance) to prioritise paths:
ip route add default via 192.168.1.1 metric 100 ip route add default via 10.0.0.1 metric 200
ECMP (Equal-Cost Multi-Path) allows multiple next hops for the same destination:
ip route add 192.168.0.0/16 nexthop via 192.168.1.1 weight 1 nexthop via 10.0.0.1 weight 1
Related Articles
- OSI model: Article - OSI network model
- Protocols: Article - Main protocols: ARP, ICMP, UDP, TCP
- Interfaces: Article - Interfaces/bridge, IPv4/IPv6 addressing
- Firewall: Article - netfilter, firewall, conntrack, masquerade