Sub topic 3
Static Routes: Configuring Paths by Hand
This chapter assumes you know IPv4 addresses and CIDR prefixes, the difference between a router, a switch and a host, and basic command-line use on a router or Linux host. It also assumes you have seen a routing table and longest-prefix match.
What a static route is
A static route is an entry in the routing table that an administrator types in. The router does not learn it from anyone, and it does not change unless someone edits it. A static route has three parts:
- The destination prefix, such as
10.2.0.0/16. - The next hop, which is the IP address of the neighboring router that should receive the packet. You can also name an exit interface instead.
- Optionally, a cost value such as administrative distance (covered in a later chapter).
The next hop must be reachable on a directly connected network. If the router has no route to the next-hop address, it cannot install the static route.
Worked example on Linux
The ip command manages routes on a Linux host or a Linux-based router. This example sends traffic for 10.2.0.0/16 to the router at 192.168.1.2.
# Linux host or router: add a static route to 10.2.0.0/16 via a next hop
sudo ip route add 10.2.0.0/16 via 192.168.1.2 dev eth0
# Show the routing table
ip route show
# Ask the kernel which route it would use for one destination
ip route get 10.2.5.7The first command adds the route. The second lists the table, where the new line appears as 10.2.0.0/16 via 192.168.1.2 dev eth0. The third shows the route the kernel picks for 10.2.5.7. Because 10.2.5.7 falls inside 10.2.0.0/16, the kernel picks your new route unless a more specific prefix also matches.
Routes added with ip route add are lost on reboot. To keep them, put them in your distribution's network configuration.
Worked example on Cisco IOS
On Cisco IOS, the same route uses a network address and a subnet mask instead of a prefix length.
! Cisco IOS: static route to 10.2.0.0/16 via next hop 192.168.1.2
ip route 10.2.0.0 255.255.0.0 192.168.1.2
! Default route toward the upstream router
ip route 0.0.0.0 0.0.0.0 192.168.1.1The first line sends traffic for 10.2.0.0/16 to a fixed next hop. The second line uses 0.0.0.0/0, which matches any destination that no more specific route covers.
Common errors
Most static route problems come from a small set of mistakes:
- Missing return route. Router B must also know how to reach
10.1.0.0/16. Without it, packets arrive but replies are lost. - Wrong or unreachable next hop. If the next-hop address is not on a connected network, the route is rejected or never installed.
- Wrong mask. Writing
255.255.255.0instead of255.255.0.0covers a much smaller range than you intended. - A more specific route overrides yours. Longest-prefix match picks the most specific entry, so a
/24elsewhere in the table wins over your/16. - Lost after reboot. On Linux, runtime routes are not persistent by default.
Where static routes fit
Static routes cost no CPU or bandwidth for route exchange, behave predictably, and are easy to read. They work well for small networks, stub sites with a single exit, and default routes. They become hard to maintain as the number of routers and prefixes grows, because every change must be made by hand on every affected device.
Exercise
Router A has the route ip route 10.2.0.0 255.255.0.0 192.168.1.2. A host behind Router A pings 10.2.5.7, and the request reaches the destination, but no reply comes back. What is the most likely cause, and where do you fix it?
Show answer
The return path is missing. The router on the 10.2.0.0/16 side (or any device between it and the sender) has no route back to the sender's network, such as 10.1.0.0/16. Add a static route for the sender's network on that router, pointing to the correct next hop.