Sub topic 3
Static routes: configuring paths by hand
A static route is a routing table entry that an administrator types in. The router does not learn it from anyone and does not change it when the network changes. This chapter covers what a static route contains, how to configure one on Linux and Cisco IOS, and which mistakes cause most static routing problems.
This chapter assumes you know:
- IPv4 addressing and CIDR prefix notation.
- The difference between a router, a switch and a host.
- Basic command-line use on a Linux host or a router.
- That a router forwards a packet using the most specific matching route (longest-prefix match).
What a static route contains
Every static route answers one question: "For packets destined to this prefix, where do I send them next?" It has these parts:
- Destination prefix. The network the route covers, such as
10.2.0.0/16. - Next hop. The IP address of the neighboring router that should receive the packet.
- Exit interface. The local interface the packet leaves through. Often the router works this out for you.
- Administrative distance. A trust value for the route source. Cisco IOS gives a static route a default distance of 1, so it beats routes from any dynamic protocol unless you change it.
- Metric. A cost used to rank routes from the same source. Static routes usually leave it at its default.
The next hop must be reachable through a network the router is directly connected to. A router cannot send a frame to 192.168.1.2 unless it has an interface on 192.168.1.0/24.
A worked topology
The example network has two routers joined by a link. Each router knows only its own connected networks until you tell it more.
For Host A to reach Host B, R1 needs a route to 10.2.0.0/16 through 192.168.1.2. For the reply to come back, R2 needs a route to 10.1.0.0/16 through 192.168.1.1. Routing is not symmetric by default, so you must configure both directions.
Configuring a static route on Linux
On a Linux host or a Linux router, the ip route command edits the kernel routing table.
# On R1: send traffic for 10.2.0.0/16 to R2
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 output of ip route get looks like this:
10.2.5.7 via 192.168.1.2 dev eth0 src 192.168.1.1 uid 1000
cacheIt names the next hop, the exit interface and the source address the kernel would use. This is the fastest way to check a forwarding decision.
A Linux machine only forwards packets for other hosts if forwarding is turned on:
sudo sysctl -w net.ipv4.ip_forward=1Making the route survive a reboot
A route added with ip route add lives only in memory. It disappears at reboot or when the interface restarts. Put persistent routes in your distribution's network configuration. On systems that use Netplan, the route goes in the interface definition:
network:
version: 2
ethernets:
eth0:
addresses: [192.168.1.1/24]
routes:
- to: 10.2.0.0/16
via: 192.168.1.2Apply it with sudo netplan apply. Other systems use NetworkManager (nmcli) or files in /etc/network. The mechanism differs, but the rule is the same: runtime commands are for testing, and configuration files are for the real route.
Configuring a static route on Cisco IOS
IOS uses a network address and a subnet mask instead of a prefix length.
! On R1: 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
! On R2: the return route
ip route 10.1.0.0 255.255.0.0 192.168.1.1IOS stores the route in the running configuration. Save it with copy running-config startup-config, or it is lost at the next reload.
Next hop, exit interface, or both
IOS accepts three forms of the same command. They behave differently.
| Form | Example | Behavior |
|---|---|---|
| Next hop only | `ip route 10.2.0.0 255.255.0.0 192.168.1.2` | The router does a second lookup to find the interface that reaches `192.168.1.2`. This is called a recursive lookup. |
| Exit interface only | `ip route 10.2.0.0 255.255.0.0 Serial0/0/0` | The router treats the destination as directly attached to that interface. This works on point-to-point links. |
| Both (fully specified) | `ip route 10.2.0.0 255.255.0.0 GigabitEthernet0/1 192.168.1.2` | The router uses the named interface and the named next hop. No extra lookup is needed. |
The exit-interface-only form is a trap on Ethernet. The router sends an ARP request for every destination address in the prefix, because it believes each one is on the local segment. That fills the ARP table and works only if the neighbor answers on behalf of other hosts (proxy ARP). On Ethernet, use the next-hop form or the fully specified form.
Reading the result
show ip route staticA working entry looks like this:
S 10.2.0.0/16 [1/0] via 192.168.1.2S marks a static route. In [1/0], the first number is the administrative distance (1) and the second is the metric (0).
When a static route enters the table
A router installs a static route only if its next hop is reachable. If the next hop is not covered by a connected network or another route in the table, the route stays in the configuration but does not appear in the routing table.
The router removes the route if the interface toward the next hop goes down. Linux does the same when the link goes down. That is the only reaction a static route has to failure.
A static route does not test whether the next hop is healthy. If the next-hop router is powered off but the switch port is still up, the route stays installed. Packets then go to a device that cannot forward them. Detecting that needs tracking features such as IP SLA on Cisco devices, or a dynamic protocol.
Common errors
These problems account for most failed static route setups:
- Missing return route. Host A's packets arrive, but Host B's replies have no path back.
pingtimes out even though the forward route is correct. Check the routing table on every router in the path, in both directions. - Wrong mask or prefix length.
10.2.0.0 255.255.255.0covers only a/24, not the whole/16. Traffic to10.2.5.7then misses the route. A mask that is too specific is a quiet failure. - Next hop not on a connected network. The route is accepted in the configuration and never installed. Compare
show running-configwithshow ip route. - Exit-interface-only on Ethernet. This causes ARP floods and unreliable behavior, as described above.
- A more specific route overrides yours. Longest-prefix match prefers a
/24that leads elsewhere over your/16. Useip route getorshow ip route 10.2.5.7to see which entry is chosen. - Route loops. R1 points a prefix at R2, and R2 points the same prefix back at R1. Packets circle until the TTL (time to live) reaches zero, and the sender gets "TTL exceeded" errors.
tracerouteshows the same two addresses alternating. - Forgetting persistence. The route worked yesterday and is gone after the reboot.
Where static routes fit
Static routes cost nothing in CPU, bandwidth or protocol traffic, and they are predictable: the path is exactly what you typed. They suit small networks, stub networks with a single exit, and paths that must never change on their own. Their cost is administrative. Each new network means editing every router that needs to reach it, and every mistake is yours to find. The later chapter on choosing an approach compares this with dynamic routing in detail.
Exercise
R1 has the interface 192.168.1.1/24 toward R2, and R2 has the LAN 10.2.0.0/16 behind it. You enter ip route 10.2.0.0 255.255.0.0 192.168.1.2 on R1. Host A (10.1.0.10) can send packets to Host B (10.2.5.7), but pings never receive a reply. Hosts on R2's LAN can reach each other. What is the most likely cause, and what command would you enter to fix it?
Show answer
R2 has no route back to 10.1.0.0/16, so the replies are dropped. Add the return route on R2: ip route 10.1.0.0 255.255.0.0 192.168.1.1. Verify with show ip route static on R2 and with ip route get or show ip route 10.1.0.10.
Exercise
On a Linux router you run sudo ip route add 10.3.0.0/16 via 192.168.1.9 dev eth0. The command fails with "Nexthop has invalid gateway". What does this tell you about 192.168.1.9, and what should you check?
Show answer
The kernel does not consider 192.168.1.9 reachable on a directly connected network through eth0. Check that eth0 has an address in the same subnet as 192.168.1.9 (for example 192.168.1.1/24), that the interface is up, and that the mask on eth0 actually includes 192.168.1.9.