In multi-homed Linux servers, virtualised cloud environments, and enterprise networks spanning multiple local subnets and VPN tunnels, relying exclusively on a single default gateway can cause internal network traffic to be misrouted. When a system needs to communicate with private subnets across dedicated internal routers, network administrators must define static routing entries within the Linux kernel routing table. The modern ip route command (provided by the standard iproute2 package, which supersedes legacy route and netstat commands) offers complete, real-time control over routing policies.
Understanding how to add, inspect, modify, and delete static routes allows you to direct traffic across secondary network interfaces, VPN links, and internal gateway hops reliably.
Displaying and Interpreting the Linux Routing Table
Before modifying routes, inspect the existing kernel routing configuration:
- Open a terminal shell and display the active IPv4 routing table:
ip route show
- The command returns all active routes recognized by the kernel. Key entries include:
- Default Route: Rendered as
default via 192.168.1.1 dev eth0 proto dhcp metric 100. This specifies the fallback gateway and egress interface for all traffic not explicitly matched by more specific subnet rules. - Local Subnet Route: Displayed as
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.50, which directs local subnet communications directly across physical link wires without passing through the gateway.
- Default Route: Rendered as
- To view routing decisions for a specific destination IP address to determine which interface the kernel will select:
ip route get 8.8.8.8
Adding a Static Route to an Internal Subnet
To direct traffic targeting a remote internal subnet (for example, a private lab network at 10.50.0.0/16) through a secondary internal router located at 192.168.1.254:
- Execute the route addition command with elevated privileges:
sudo ip route add 10.50.0.0/16 via 192.168.1.254 dev eth0
- If you only need to route traffic targeting a single specific host IP (such as an internal database server at
192.168.10.45):
sudo ip route add 192.168.10.45 via 192.168.1.254 dev eth0
- To assign a custom routing metric (priority score) when redundant network paths exist, append the
metricparameter (lower values indicate higher preference):
sudo ip route add 10.50.0.0/16 via 192.168.1.254 dev eth0 metric 50
Modifying and Replacing Active Routes
If an internal gateway changes its IP address or a primary link fails, update the routing entry seamlessly without tearing down network interfaces:
- Use the
replacedirective to substitute new routing parameters:
sudo ip route replace 10.50.0.0/16 via 192.168.1.200 dev eth0
- Verify that the replacement updated cleanly:
ip route show 10.50.0.0/16
Deleting Static Routes
When temporary maintenance concludes or testing subnets are decommissioned, remove static routes to restore standard default gateway forwarding:
- Delete a specific route by declaring its destination prefix:
sudo ip route del 10.50.0.0/16 via 192.168.1.200 dev eth0
- Alternatively, omit interface flags to purge all matching entries for that subnet:
sudo ip route del 10.50.0.0/16
Making Static Routes Persistent Across Reboots
Direct commands executed with ip route apply immediately to the active kernel state, but disappear upon system reboot. To make static routes permanent:
- Ubuntu / Debian (Netplan): Add a
routes:section under your network interface in/etc/netplan/01-netcfg.yaml:
Apply the configuration by runningnetwork: version: 2 ethernets: eth0: routes: - to: 10.50.0.0/16 via: 192.168.1.254sudo netplan apply. - RHEL / Rocky Linux (NetworkManager): Add persistent routes using
nmcli:sudo nmcli connection modify eth0 +ipv4.routes "10.50.0.0/16 192.168.1.254" sudo nmcli connection up eth0