Static routes tell your server exactly how to reach specific networks, overriding the default gateway for those destinations — useful for VPN configurations, multi-network setups, or directing traffic to specific paths deliberately.
Understanding Routing Basics
By default, traffic to any destination not on the local network goes through the default gateway. A static route creates an exception: traffic to a specific network/host goes through a different path instead.
Step 1 — View the Current Routing Table
ip route show
Shows existing routes, including the default gateway (marked default) and any local network routes automatically configured.
Step 2 — Add a Temporary Static Route (Testing)
sudo ip route add 192.168.50.0/24 via 10.100.0.2
Routes traffic destined for the 192.168.50.0/24 network through 10.100.0.2 (perhaps a VPN gateway or another server) instead of the default gateway.
Step 3 — Test the Route
ping 192.168.50.10
traceroute 192.168.50.10
Confirm traffic is actually taking the expected path.
Step 4 — Make the Route Persistent (Netplan)
sudo nano /etc/netplan/50-cloud-init.yaml
network:
ethernets:
eth0:
routes:
- to: 192.168.50.0/24
via: 10.100.0.2
sudo netplan apply
Adding a Route to a Single Host (Not an Entire Subnet)
sudo ip route add 203.0.113.50/32 via 10.100.0.2
Removing a Route
sudo ip route del 192.168.50.0/24
Common Use Case: Routing Through a Site-to-Site VPN
After setting up a site-to-site VPN (see How to Set Up a Site-to-Site VPN Between Two VPS Servers), a static route ensures traffic to the remote site's subnet is correctly directed through the tunnel rather than attempted over the public internet directly.
Verifying Which Route a Specific Destination Uses
ip route get 192.168.50.10
Shows exactly which route/interface would be used for that specific destination, useful for confirming configuration without generating actual traffic.
Common Errors
Route added but traffic still uses the default path — verify there isn't a more specific conflicting route already present (more specific routes/longer prefixes take precedence), and double check the exact syntax and via address.
Route doesn't survive a reboot — the route was added with ip route add directly (temporary) rather than defined in the persistent Netplan configuration.
Best Practices
- Always test routes temporarily before making them persistent, to catch mistakes before they survive a reboot
- Document why each static route exists — non-obvious routing configuration is easy to forget the reasoning behind later
- Keep out-of-band console access available when testing routing changes on a remote server
Continue Reading
- How to Set Up a Site-to-Site VPN Between Two VPS Servers
- How to Bond Multiple Network Interfaces for Redundancy
- How to Configure Multiple IP Addresses on One VPS
Browse more articles in Advanced Networking & VPN.
