Sub topic 12
Static routes
This chapter assumes you can read a routing table, apply longest prefix match, and understand next hops and connected networks from the earlier chapters. A static route is an entry that an administrator adds to the routing table by hand. The router does not learn it from other routers.
Why use static routes
A router automatically knows its connected networks. For anything farther away, it needs a route. Static routes are a good fit in three cases:
- A small network with one path out, such as a branch office with a single uplink.
- A default route toward an internet provider.
- A deliberate override, such as sending one prefix through a firewall.
Static routes use no CPU or bandwidth for route exchange, and their behavior is predictable. The cost is that they never adapt. If a next hop fails, the route stays in the table until you remove it, and traffic is dropped.
Anatomy of a static route
Every static route has a destination prefix and a way to reach it. The way is usually a next-hop address, and sometimes an outgoing interface.
# Add a static route to 10.2.0.0/16 through the next hop 192.0.2.2
sudo ip route add 10.2.0.0/16 via 192.0.2.2 dev eth0
# Confirm it is installed
ip route show
# Ask which route a destination would use
ip route get 10.2.5.9The via address must be reachable through a connected network. Here, 192.0.2.2 must sit on the same subnet as eth0. If it does not, the kernel rejects the command, because it cannot resolve the next hop with ARP.
A worked example
Router R1 connects to the 10.1.0.0/16 network on one interface and to a link 192.0.2.0/24 on another. R2 is at 192.0.2.2 and sits in front of 10.2.0.0/16. R1 knows nothing about 10.2.0.0/16 until you add the route.
After you add the route on R1, its table gains an entry for 10.2.0.0/16. R1 forwards a packet for 10.2.5.9 to 192.0.2.2. The packet only returns if R2 also has a route back to 10.1.0.0/16. Static routes are one-way, so you must configure both directions. A missing return route is the most common static routing mistake.
Forwarding must be enabled
A Linux machine only routes between interfaces if forwarding is on:
sudo sysctl -w net.ipv4.ip_forward=1Without it, the machine accepts packets for itself but drops transit traffic.
Static routes and route selection
Static routes take part in longest prefix match like any other route. A static /24 beats a dynamic /16 for addresses inside it, whatever the protocol. When two sources offer the same prefix length, the router compares administrative distance. On many platforms a static route has a low distance (1 on Cisco IOS), so it wins over most dynamic routes. A static route with a higher distance, called a floating static route, acts as a backup. It appears in the table only when the preferred route disappears.
Common errors
| Symptom | Likely cause |
|---|---|
| Command rejected | Next hop is not on a connected subnet |
| Ping reaches the far router but replies never return | No return route on the far side |
| Traffic goes the wrong way | A more specific route elsewhere in the table wins |
| Traffic is dropped after a link failure | The static route still points at a dead next hop |
Exercise
Router R1 has a static route 10.2.0.0/16 via 192.0.2.2. Hosts on 10.1.0.0/16 can send packets to 10.2.5.9, but get no replies. R2 forwards packets correctly. What is the most likely cause, and how do you fix it?
Show answer
R2 probably has no route back to 10.1.0.0/16, so replies are dropped or sent elsewhere. Add a static route on R2 for 10.1.0.0/16 via R1's address on the shared link.
Exercise
A router has a static route 10.0.0.0/8 via 192.0.2.2 and a dynamic route 10.1.0.0/16 via 192.0.2.3. Which next hop does a packet for 10.1.2.3 use, and why?
Show answer
It uses 192.0.2.3. Longest prefix match is applied first, and /16 is more specific than /8. Administrative distance is compared only between routes with the same prefix.