Sub topic 12
Static routes
This chapter assumes you can read a routing table, apply longest prefix match, and tell a connected network from a next hop. It also assumes you know what a default route is and are comfortable with a Linux shell. Static routes are the simplest way to put an entry into the routing table. They are also a good way to see what the table assumes about the rest of the network.
What a static route is
A static route is a routing table entry that an administrator writes by hand. No protocol discovers it, and no protocol removes it. It stays until someone deletes it, or until the kernel drops it because the outgoing interface went away.
Every static route says the same thing: "To reach this prefix, send the packet to this next hop, or out of this interface." The route enters the table like any other. Forwarding still uses longest prefix match, so a static /24 beats a dynamic /16 that covers the same addresses.
The next table compares static routes with the other two sources of routes.
| Source | Who creates it | Reacts to failures | Typical use |
|---|---|---|---|
| Connected | The kernel, when an interface gets an address | Yes, when the link or address goes away | Directly attached subnets |
| Static | An administrator | Only if the interface goes down | Stub networks, default routes, small sites |
| Dynamic | A routing protocol | Yes, through protocol timers and updates | Larger or changing networks |
When static routes are the right tool
Static routes fit when the topology is small and does not change often. They have no protocol overhead, no neighbor sessions, and no convergence behavior to debug. The router's behavior is exactly what you typed.
Common cases:
- Stub networks. A site with one uplink needs only a default route toward its provider.
- Default routes on hosts and edge routers. Almost every host has one static default route.
- Pointing a few prefixes at a specific device, such as a firewall or VPN gateway.
- Discarding traffic on purpose with a blackhole route.
- Backup paths that should be used only when the primary is gone.
They fit poorly when there are many routers or several paths. Each change must be repeated by hand on every router that needs it. A typo on one router can create a loop that nobody notices until traffic fails.
Anatomy of a static route
A static route has three parts: the destination prefix, the way to reach it (next hop, interface, or both), and optionally a preference value.
The next hop must be reachable through a connected network. The router can only rewrite the Layer 2 frame toward an address on a link it is attached to. If you give a next hop that is not on any connected subnet, the Linux kernel rejects the command with Nexthop has invalid gateway. Some router operating systems accept the route and resolve the next hop recursively through other routes. That behavior is useful, but it makes mistakes harder to spot.
The following script checks the same rule before you apply a route. It uses the same ipaddress module as the earlier lookup example.
import ipaddress
connected = [
ipaddress.ip_network("192.0.2.0/24"),
ipaddress.ip_network("198.51.100.0/24"),
ipaddress.ip_network("10.0.0.0/24"),
]
def validate(prefix, next_hop):
"""Check that the prefix is valid and the next hop is on a connected network."""
ipaddress.ip_network(prefix) # raises ValueError for a malformed prefix
hop = ipaddress.ip_address(next_hop)
for net in connected:
if hop in net:
return f"ok: {next_hop} is reachable through {net}"
return f"reject: {next_hop} is not on any connected network"
print(validate("10.2.0.0/16", "192.0.2.2"))
print(validate("10.3.0.0/16", "203.0.113.9"))The first call prints an ok line naming 192.0.2.0/24. The second prints a reject line, because 203.0.113.9 is not on a connected subnet.
Configuring static routes on Linux
The topology below is used for the rest of the chapter. Router R1 reaches the remote network 10.2.0.0/16 through R2 on one link. A second path goes through R3 on another link.
On R1 the commands look like this:
# Enable forwarding so R1 routes between its interfaces
sudo sysctl -w net.ipv4.ip_forward=1
# Primary static route through R2
sudo ip route add 10.2.0.0/16 via 192.0.2.2 dev eth0 metric 1
# Floating backup route through R3 with a worse (higher) metric
sudo ip route add 10.2.0.0/16 via 198.51.100.3 dev eth1 metric 200
# Default route toward the provider
sudo ip route add default via 203.0.113.1 dev eth2
# Discard traffic to a prefix you never want to forward
sudo ip route add blackhole 10.99.0.0/16
# Verify
ip route show
ip route get 10.2.5.5ip route get 10.2.5.5 should report the route through 192.0.2.2 while the primary is present. If you delete the primary with sudo ip route del 10.2.0.0/16 via 192.0.2.2 dev eth0 metric 1, the same command reports 198.51.100.3.
Routes added with ip route add live only in the running kernel. A reboot removes them. To make them persistent, put them in your distribution's network configuration, such as netplan, NetworkManager, or systemd-networkd. Router operating systems keep them in the saved startup configuration.
Next hop versus interface
You can name the next hop (via 192.0.2.2), the interface (dev eth0), or both.
- Next hop address. The router ARPs for that one address and sends all matching traffic to it. This is the right choice on Ethernet. It is also the safe one, because the router resolves only one neighbor.
- Interface only. The router treats the prefix as directly attached to that link. It sends an ARP request for every distinct destination address inside the prefix. This works only if a device on the link answers for those addresses, for example through proxy ARP. On a broadcast Ethernet link it is usually a mistake, and it causes a heavy ARP load.
- Interface only on point-to-point links, such as tunnels, is fine. There is only one device at the other end, so no address resolution is needed.
Floating static routes
A floating static route is a backup route with a deliberately worse preference. Both routes to 10.2.0.0/16 have the same prefix length, so longest prefix match cannot choose between them. The preference value decides. On Linux this is the metric, and lower wins. Many router operating systems use an administrative distance for the same purpose. The chapter on metrics and administrative distance covers how those values compare across route sources.
The backup stays in the configuration but does not carry traffic while the primary is present. If the primary route disappears, the backup becomes the best remaining route and traffic shifts to it.
Failure modes
The main weakness of static routes is that they do not know whether the next hop is working.
Other common problems:
- Routing loops. If R1 sends
10.2.0.0/16to R2 and R2 has a static route sending it back to R1, packets bounce until the TTL reaches zero. Each router sends an ICMP Time Exceeded message when it drops one. Traceroute shows the same two addresses alternating. - Missing return route. Traffic reaches the destination, but the reply has no route back to the source. The symptom is "ping sends but gets no answer." Check that the destination network can route back to the source prefix.
- Wrong prefix length. A
/16typed as/24leaves most of the remote network unreachable. A/16typed as/8can capture traffic that belongs elsewhere, because a broader route only loses to more specific ones. - Forwarding disabled. On Linux,
net.ipv4.ip_forwarddefaults to 0. The routes look correct andip route getgives the right answer, but the machine drops transit packets. - Overlapping blackholes. A blackhole
/16silently discards everything inside it that has no more specific route. This is the intended use, but a forgotten blackhole is hard to find.
Debugging static routes
Work from the routing decision outward.
- Run
ip route get <destination>on the router. It shows which route wins, the next hop, and the source address chosen. - Run
ip route showand look for a more specific prefix that overrides your route. - Run
ip neigh showto confirm the next hop has a resolved MAC address. AFAILEDorINCOMPLETEentry means the next hop is not answering ARP. - Run
traceroute <destination>. The last responding hop shows where the path breaks, and repeating hops show a loop. - Run the same checks on the next router and on the return path.
Exercise
R1 has two routes to 10.2.0.0/16: one via 192.0.2.2 with metric 1, and one via 198.51.100.3 with metric 200. R2 loses power, but the link between R1 and R2 stays up because a switch sits between them and its port stays up. Which route does R1 use, and why? What would you add or change so that R1 notices the failure?
Show answer
R1 keeps using the metric 1 route through R2, because the route is still in the table. Nothing removed it, since the interface is up. Packets to 10.2.0.0/16 are lost. To detect the failure, tie the route to a liveness check, such as BFD or an IP SLA style probe on a router operating system, or replace the static routes with a dynamic routing protocol that detects a dead neighbor. On a plain Linux host, a script that pings R2 and deletes the primary route when pings fail is a manual alternative.
Exercise
You run sudo ip route add 10.5.0.0/16 via 203.0.113.9. The router's connected networks are 192.0.2.0/24 and 198.51.100.0/24. What happens, and why?
Show answer
The kernel rejects the command with an error such as "Nexthop has invalid gateway". The next hop 203.0.113.9 is not on any connected network, so the router has no link on which to send an ARP request for it. You must use a next hop that falls inside a connected subnet, or first add a route that makes 203.0.113.9 reachable.