DevOps · K8s · Volleyball · Travel  •  DevOps · K8s · Volleyball · Travel  •  DevOps · K8s · Volleyball · Travel
Explore NY Stream

How to add static and temporary route on Linux server

— ny_wk

How to add static and temporary route on Linux server

To add a static route on Linux, use ip route add for a temporary route that survives only until reboot, and write the route into a NetworkManager connection or a route-<iface> file to make it persistent across network restarts. This guide shows both methods with correct, copy-paste commands, then verifies the result.

The Problem: Routes That Vanish After a Restart

A very common Linux networking task is forcing traffic for a specific destination through a particular gateway instead of the system default gateway. You add the route, it works perfectly, and then after a reboot or a systemctl restart NetworkManager the connectivity silently breaks again.

The reason is simple: a route added on the command line with ip route add lives only in the kernel's in-memory routing table. It is never written to disk, so any event that re-initializes the interface wipes it out. To create a persistent static route on Linux, the route must be stored in a configuration file that the network stack reads at boot.

This walkthrough covers the full lifecycle: add a temporary route to confirm the path works, then promote it to a permanent static route, and finally verify it is loaded correctly.

Step 1: Add a Temporary Route With ip route

Always test the path with a temporary route first. If the temporary route does not give you connectivity, there is no point making it permanent. The general syntax is:

ip route add <destination> via <gateway> dev <interface>

For example, to send traffic for two specific hosts through the gateway 172.22.97.4 on interface eth0:

  1. Add the first host route:
    ip route add 172.22.126.18/32 via 172.22.97.4 dev eth0
  2. Add the second host route:
    ip route add 172.22.126.19/32 via 172.22.97.4 dev eth0

The /32 suffix is a CIDR prefix that means a single host (the equivalent netmask is 255.255.255.255). When you omit the prefix, ip assumes /32 for an IPv4 address, so both forms work, but writing /32 explicitly makes your intent unmistakable. To route an entire subnet instead of one host, use the network address with the right prefix, for example 172.22.126.0/24 via 172.22.97.4.

Important: the gateway must be directly reachable on the same interface and subnet you specify with dev. If the gateway is not on a connected network, the kernel rejects the command with RTNETLINK answers: Network is unreachable.

Step 2: Confirm the Temporary Route Is Active

Check that the kernel accepted the routes. The modern command is ip route show, and the legacy equivalent is route -n (from the deprecated net-tools package). Use whichever is installed:

ip route show

You should see lines like:

172.22.126.18 via 172.22.97.4 dev eth0
172.22.126.19 via 172.22.97.4 dev eth0

If you prefer the older numeric output, route -n prints a table where each host route shows a Genmask of 255.255.255.255 and the flags UGH (Up, Gateway, Host):

DestinationGatewayGenmaskFlagsIface
172.22.126.18172.22.97.4255.255.255.255UGHeth0
172.22.126.19172.22.97.4255.255.255.255UGHeth0
0.0.0.0172.22.97.10.0.0.0UGeth0

Reading the flags is the quickest sanity check. UGH means a host route through a gateway, UG is the default route (note the Genmask of 0.0.0.0), and a bare U with a real netmask is a directly connected network. Test reachability with ping or tracepath 172.22.126.18 to confirm packets now take the new path.

Step 3: Make the Static Route Persistent

The right way to persist a route depends on which network manager your distribution uses. There are three realistic scenarios.

Option A: NetworkManager With nmcli (Modern RHEL 8/9, Fedora, Ubuntu)

On current systems NetworkManager owns the interface, so the cleanest method is to attach the route to the connection profile. First find the connection name:

nmcli connection show

Then add the routes (replace "System eth0" with your actual connection name) and reactivate the connection:

  1. Append both routes to the profile:
    nmcli connection modify "System eth0" +ipv4.routes "172.22.126.18/32 172.22.97.4"
    nmcli connection modify "System eth0" +ipv4.routes "172.22.126.19/32 172.22.97.4"
  2. Apply the change without a reboot:
    nmcli connection up "System eth0"

Because NetworkManager stores this in its keyfile (under /etc/NetworkManager/system-connections/), the route reloads automatically on every boot. This is the recommended approach for any distribution from roughly 2019 onward.

Option B: Legacy route-<interface> File (RHEL/CentOS 6 and 7)

Older Red Hat-family systems using the classic network-scripts read per-interface route files from /etc/sysconfig/network-scripts/. The file is named route- followed by the interface, for example route-eth0.

  1. Change into the network-scripts directory:
    cd /etc/sysconfig/network-scripts
  2. Create or edit the route file for your interface:
    vi route-eth0
  3. Add one numbered block per route. The index (0, 1, ...) ties each ADDRESS to its matching NETMASK and GATEWAY:
    ADDRESS0=172.22.126.18
    NETMASK0=255.255.255.255
    GATEWAY0=172.22.97.4
    ADDRESS1=172.22.126.19
    NETMASK1=255.255.255.255
    GATEWAY1=172.22.97.4
  4. Reload the network so the file is read:
    systemctl restart network

    (On RHEL/CentOS 6 the equivalent older command is service network restart.)

A cleaner alternative supported by the same scripts is the ip-command syntax, where each line in route-eth0 is exactly what you would type after ip route add:

172.22.126.18/32 via 172.22.97.4 dev eth0
172.22.126.19/32 via 172.22.97.4 dev eth0

Pick one style; do not mix the ADDRESS0= format and the ip-command format in the same file.

Option C: systemd-networkd or Netplan (Ubuntu Server)

If the host uses systemd-networkd, add a [Route] section to the relevant .network file in /etc/systemd/network/:

[Route]
Destination=172.22.126.18/32
Gateway=172.22.97.4

On Ubuntu with Netplan, add the routes under the interface in /etc/netplan/*.yaml and run netplan apply:

      routes:
        - to: 172.22.126.18/32
          via: 172.22.97.4

YAML is whitespace-sensitive, so keep the indentation consistent with the rest of the file.

Step 4: Verify the Persistent Static Route

Configuration is only trustworthy after a clean reload. Restart the network service (or reboot) and confirm the routes survived.

  1. Reload the stack:
    systemctl restart network    # or: nmcli connection up "System eth0"
  2. Display the routing table again:
    ip route show
  3. Confirm both host routes are present with the correct gateway:
    172.22.126.18 via 172.22.97.4 dev eth0
    172.22.126.19 via 172.22.97.4 dev eth0

For an explicit lookup of how the kernel will route a given destination, use ip route get, which returns the exact path a packet would take:

ip route get 172.22.126.18

The output should name 172.22.97.4 as the gateway and eth0 as the device. If it shows the default gateway instead, the persistent route did not load and you should re-check the configuration file.

Common Pitfalls When Adding a Static Route on Linux

Most failed static routes come down to a handful of recurring mistakes:

  • Editing the running kernel and expecting it to persist. ip route add never writes to disk. If you only run the command, the route is gone after the next interface restart.
  • Gateway not on the same subnet as the interface. The next-hop gateway must be directly reachable, or you get Network is unreachable. Verify with ip addr show eth0 that the interface's subnet contains the gateway.
  • Wrong interface name. Modern Linux uses predictable names such as enp0s3 or ens192, not always eth0. Confirm with ip link before writing the config.
  • Mismatched index numbers in route-eth0. ADDRESS0 must pair with NETMASK0 and GATEWAY0. A typo in the index silently drops the route.
  • Mixing managers. If NetworkManager controls the interface, hand-edited route-eth0 files may be ignored. Use nmcli on NetworkManager-managed systems and reserve the file method for legacy network-scripts hosts.
  • Forgetting to reload. Editing the file is not enough; you must restart the service or bring the connection up for the change to take effect.

Temporary vs Persistent Static Route at a Glance

AspectTemporary routePersistent route
Command / locationip route addnmcli profile or route-eth0 / netplan
Stored on diskNo (kernel memory only)Yes (config file)
Survives rebootNoYes
Best useTesting a path quicklyProduction configuration

The professional pattern is to combine them: prove the path with a temporary route, then commit it persistently once you are confident it is correct.

Key Takeaways

  • A temporary static route on Linux created with ip route add <dest> via <gw> dev <iface> exists only in kernel memory and is lost on restart.
  • For persistence, store the route in a NetworkManager connection (nmcli ... +ipv4.routes), a legacy route-eth0 file, or a netplan/systemd-networkd config.
  • A /32 prefix (netmask 255.255.255.255) targets a single host; use a subnet prefix like /24 to route a whole network.
  • The next-hop gateway must be directly reachable on the named interface, or the route is rejected as unreachable.
  • Always verify after a reload with ip route show and ip route get <dest> before trusting the configuration.

Frequently Asked Questions

How do I make a Linux route permanent?

Store it in a configuration file rather than only on the command line. On modern systems use nmcli connection modify "<conn>" +ipv4.routes "172.22.126.18/32 172.22.97.4" then nmcli connection up "<conn>". On legacy RHEL/CentOS, add the route to /etc/sysconfig/network-scripts/route-eth0 and restart the network service.

What is the difference between ip route and the old route command?

route is from the deprecated net-tools package and is read-only on many modern distros. ip route, from the iproute2 suite, is the current standard and supports everything route did plus newer features. Use ip route show to view routes and ip route add to create them.

Why does my static route disappear after rebooting?

Because it was only added with ip route add, which writes nothing to disk. The kernel routing table is rebuilt from configuration files at boot, so any route not saved in a NetworkManager profile, a route-<iface> file, or a netplan/networkd config is discarded.

How do I add a static route for an entire subnet instead of one host?

Use the network address with the appropriate CIDR prefix. For example, ip route add 172.22.126.0/24 via 172.22.97.4 dev eth0 routes the whole 172.22.126.0/24 network through that gateway, instead of using /32 for a single host.

For more hands-on Linux and sysadmin walkthroughs, subscribe to YouTube @explorenystream.