Understanding Static Routing Basics
A static route is a manually configured entry in the kernel routing table that tells the operating system which path to take for a specific destination network. Unlike dynamic routing protocols such as OSPF or BGP, static routes do not change unless an administrator modifies them. This predictability makes them ideal for simple topologies or when specific traffic needs to be forced through a particular gateway or interface.
In Linux, the routing decision is made by the ip route subsystem. When a packet arrives, the kernel checks its destination IP against the routing table. If a match is found for a specific subnet, the packet is forwarded out of the designated interface toward the next hop. Without a proper static route, traffic destined for non-local subnets may default to the main gateway, potentially causing connectivity issues or security leaks in multi-homed setups.
Configuring Routes with Netplan (Ubuntu 18.04+)
Modern Ubuntu releases use Netplan as the primary configuration tool, which generates backend configurations for NetworkManager or systemd-networkd. To add a static route, you must edit the YAML configuration file located in /etc/netplan/. The exact filename varies, often something like 50-cloud-init.yaml or 01-netcfg.yaml.
Open your preferred text editor to modify the file. You need to identify the network interface that will handle the traffic, such as eth0 or wlan0, and add a routes section under that interface's definition. Here is an example structure:
network:
version: 2
ethernets:
eth0:
addresses:
- 192.168.1.10/24
routes:
- to: 10.0.0.0/8
via: 192.168.1.1
metric: 100
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
search:
- example.com
In this example, any traffic destined for the 10.0.0.0/8 network will be sent to the gateway at 192.168.1.1 via the eth0 interface. The metric value determines the priority; lower numbers indicate higher preference. After saving the file, apply the changes using the command sudo netplan apply. If errors occur, run sudo netplan try to revert automatically after a timeout.
For more complex scenarios involving multiple tables or specialized backends, refer to Routing Traffic Between Different Table/Backend Interfaces: wlan0 and wg0 for insights into managing diverse network stacks.
Legacy Configuration with ifupdown
Older Linux distributions or minimal server installations may still rely on ifupdown, managed by files in /etc/network/interfaces. In this environment, static routes are defined using the up directive within the interface stanza. This method ensures the route is added every time the interface comes up.
Edit the /etc/network/interfaces file and append the route command to the desired interface block. For instance, to route traffic for 172.16.0.0/16 through gateway 192.168.1.254 on eth1:
auto eth1
iface eth1 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
up ip route add 172.16.0.0/16 via 192.168.1.254 dev eth1
The up command executes the specified ip route command during the interface activation phase. Remember that these routes are transient until the interface restarts or the system reboots. To make changes effective immediately without rebooting, you can either restart the networking service with sudo systemctl restart networking or bring the interface down and up again using sudo ifdown eth1 && sudo ifup eth1.
Ensuring Persistence and Verification
Regardless of the method used, verifying the route is crucial. Use ip route show to display the current routing table. Look for your new entry to ensure it appears correctly. Additionally, check cat /proc/sys/net/ipv4/ip_forward to confirm that IP forwarding is enabled if you are acting as a router between interfaces. A value of 1 indicates forwarding is active.
If you encounter issues where traffic does not flow as expected, consider checking firewall rules. Sometimes, static routes are correct, but iptables or nftables blocks the actual packet transmission. For troubleshooting internet access issues in similar gateway setups, see Linux Server 2: No Internet Access - Troubleshooting with Linux Gateway.
Advanced Considerations and Policy Routing
Simple static routes work well for single-path routing. However, if you need to route based on source IP rather than just destination, you must implement policy routing using separate routing tables. This involves defining a custom table in /etc/iproute2/rt_tables and adding rules with ip rule.
This approach is essential for split-horizon DNS or when different internal networks require different external gateways. While this article focuses on basic static routes, understanding the foundation is key before moving to advanced policy-based routing. Always test changes in a controlled environment to avoid locking yourself out of remote management connections.
Frequently Asked Questions
How do I verify if a static route was successfully added?
You can verify the route by running ip route show in the terminal. Look for the specific destination network and gateway you configured. If the route is present, it will appear in the output list alongside other active routes.
What happens if the gateway for a static route becomes unreachable?
Linux will not automatically remove the route if the gateway becomes unreachable, but packets sent to that destination will fail to transmit. You may see "Destination Host Unreachable" or timeouts. Monitoring tools or scripts are needed to detect this state and trigger corrective actions.
Can I have multiple static routes for the same destination network?
Yes, you can add multiple routes for the same destination, but they must point to different gateways or interfaces. The kernel uses the metric value to determine priority. Lower metric values are preferred. If metrics are equal, load balancing or fallback behavior depends on the kernel version and configuration.
Why is my static route disappearing after a reboot?
This usually means the route was added only temporarily using the ip route command without modifying the persistent configuration files. Ensure you update your Netplan YAML or /etc/network/interfaces file so the route is recreated during boot.
Do I need to enable IP forwarding for static routes to work?
No, IP forwarding is only required if you are routing traffic *between* two interfaces on the same machine (acting as a router). If the machine itself is sending traffic to a remote network via a static route, forwarding is not necessary; standard routing suffices.