Configure Linux Static Routes in Minutes: A Practical Step-by-Step Guide
Get hands-on with Linux static routes in minutes—this practical step-by-step guide walks you through clear commands, core concepts, and real-world scenarios so you can set precise, persistent routes for VPS, VPN, and multi-homed setups.
Static routing is a foundational skill for system administrators, devops engineers, and anyone managing VPS networks. While dynamic routing protocols automate route discovery, static routes provide control, predictability, and simplicity — especially in small deployments, VPNs, and multi-homed servers. This article gives a practical, step-by-step walkthrough to configure Linux static routes in minutes, explains underlying principles, shows common application scenarios, compares static vs dynamic approaches, and offers buying guidance for VPS deployments.
Why use static routes?
Static routes are manual entries in the kernel routing table that define how network traffic should be forwarded. They are ideal when you need:
- Precise control over traffic paths (e.g., send management traffic through a dedicated gateway).
- Simple, predictable routing in small topologies or point-to-point links.
- Fallback or policy routes for specific subnets without running a routing daemon.
- Stable routes in environments where dynamic routing may be overkill or unsupported (common on many VPS platforms).
Key routing concepts to understand
Destination network — the subnet or host you want to reach (CIDR notation, e.g., 10.10.1.0/24).
Next-hop (gateway) — the IP address where packets are sent to reach the destination (often a router on the same broadcast domain).
Interface — the local network device the kernel uses to send the packets (e.g., eth0, ens3).
Metric — preference value; lower is preferred when multiple routes to the same destination exist.
Quick checklist before you begin
- Confirm the server’s current IP configuration with the command: ip addr show.
- Inspect current routing table: ip route show or route -n.
- Ensure you have administrative privileges (root or sudo).
- Know the destination subnet and next-hop IP reachable on a directly connected interface.
Step-by-step: Adding temporary static routes (effective immediately)
The fastest way to add a route is with the ip utility. These commands are immediate but not persistent across reboots.
To add a route to a subnet via a next-hop:
Example – route traffic for 10.10.20.0/24 via gateway 192.0.2.1 on eth0:
ip route add 10.10.20.0/24 via 192.0.2.1 dev eth0
To add a host route:
Example – route traffic specifically to host 203.0.113.45:
ip route add 203.0.113.45/32 via 192.0.2.1 dev eth0
To add a route with a metric:
Example – prefer another default route by setting a higher metric:
ip route add default via 192.0.2.254 dev eth0 metric 200
Verify the route
After adding a route, verify with: ip route show. You can also test reachability using ping and trace with traceroute (or tracepath).
Making routes persistent across reboots
Different Linux distributions and init systems store persistent network configuration in different places. Below are the most common methods used on VPS instances.
Debian/Ubuntu (ifupdown, /etc/network/interfaces)
Add static routes under the interface stanza in /etc/network/interfaces:
Example:
auto eth0
iface eth0 inet static
address 198.51.100.10
netmask 255.255.255.0
gateway 198.51.100.1
up ip route add 10.10.20.0/24 via 198.51.100.2 dev eth0
The up directive runs after the interface is up. Use down ip route del … to remove on shutdown if you want cleanup.
Ubuntu (netplan)
Modern Ubuntu versions use netplan with YAML files in /etc/netplan/. Example snippet:
network:
version: 2
ethernets:
eth0:
addresses: [198.51.100.10/24]
gateway4: 198.51.100.1
routes:
– to: 10.10.20.0/24
via: 198.51.100.2
metric: 100
Apply changes with sudo netplan apply.
CentOS/RHEL (NetworkManager or ifcfg files)
For classic ifcfg files, create a route file like /etc/sysconfig/network-scripts/route-eth0 with entries:
10.10.20.0/24 via 198.51.100.2 dev eth0
Or use nmcli/NetworkManager configuration for persistent settings.
systemd-networkd
If your system uses systemd-networkd, define routes in the .network file:
[Match]
Name=eth0
[Network]
Address=198.51.100.10/24
Gateway=198.51.100.1
[Route]
Destination=10.10.20.0/24
Gateway=198.51.100.2
Troubleshooting common issues
Static routes may fail for several reasons. Use the following checks:
- Is the next-hop reachable? Test with ping to the gateway IP.
- Correct interface? Ensure the specified interface is up (ip link show) and matches the subnet used to reach the next-hop.
- Source IP selection — outgoing packets choose a source address based on the outgoing interface. If reverse path checks are enabled (rp_filter), the kernel may drop packets if source addresses seem incorrect. Check /proc/sys/net/ipv4/conf/*/rp_filter.
- Firewall or iptables rules — ensure policies allow forwarding if this machine routes between networks (iptables -L -n, check FORWARD chain).
- Asymmetric routing — replies may take a different path, causing connection problems. Use policy routing or adjust metrics to control return paths.
Use ip rule and ip route for policy routing
When you need to route traffic based on source address, use policy routing:
Example – create a routing table in /etc/iproute2/rt_tables by adding “200 secondary”, then:
ip route add default via 198.51.100.254 dev eth1 table secondary
ip rule add from 10.0.0.0/24 table secondary
This instructs the kernel to use the “secondary” table for packets originating from the 10.0.0.0/24 source, solving multi-homed return path issues.
Application scenarios and practical examples
Static routes are commonly used in these contexts:
- VPN gateways — route specific remote subnets through a tunnel interface (tun0 or wg0) using persistent routes so traffic destined for partner networks goes through the VPN.
- Multi-homed VPS — when a VPS has interfaces in different networks you may route outbound traffic for certain destinations through a specific provider interface for compliance or cost reasons.
- DMZ and management networks — ensure management traffic to admin subnets goes via the secure path.
- Peering with on-prem networks — small office environments connected to cloud VPS often use static routes on the VPS as a simple alternative to running BGP.
Advantages and limitations: static vs dynamic routing
Advantages of static routes:
- Low resource usage and minimal configuration overhead.
- Predictable behavior — traffic always follows the configured path.
- No additional daemons or protocols required, which is suitable for constrained VPS environments.
Limitations:
- No automatic adaptation to link failures — routes must be updated manually or by scripts.
- Scales poorly in large or frequently changing topologies.
- No inherent load balancing or route discovery like BGP/OSPF.
Purchase and deployment considerations for VPS environments
When deploying static routes on VPS instances, consider:
- Does the VPS control panel or provider support custom routes or advanced networking (VLANs, private networks)? For example, providers that offer multiple network interfaces or private networks simplify multi-homed designs.
- Does the provider block or NAT some traffic paths that could interfere with routing (e.g., private gateway requirements)? Check documentation and provider support.
- Performance and latency for specific geolocations — choose a VPS region near your users or peer networks to reduce RTT.
- Operational features like snapshots and backups are useful when experimenting with network changes.
Summary and recommended next steps
Static routes are a powerful tool for giving you deterministic control over packet flows on Linux servers. With simple commands like ip route add you can achieve immediate effects, and with distro-specific persistence mechanisms (netplan, ifcfg, systemd-networkd) you can make configurations survive reboots. For more complex multi-homing or source-based routing needs, leverage policy routing through ip rule and additional routing tables.
For operators running VPS infrastructure, ensure your provider supports the required networking features. If you’re evaluating VPS providers for projects that require flexible networking and reliable performance in the United States, consider checking offerings like USA VPS on VPS.DO for region choices and network capabilities. For general provider information and guides, the main site is VPS.DO.
Try a basic configuration on a non-production instance first: verify reachability, validate persistence, and document the route changes for operational procedures. With the techniques above you’ll be able to configure Linux static routes reliably in minutes and manage more complex routing scenarios as your environment grows.