How to Add a Static Route in Linux

How to Add a Static Route in Linux

To how to add a static route in linux, use the ip route add command for a temporary route, or configure your network manager or distro networking files for a persistent route that survives reboot. The exact method depends on whether your system uses netplan, NetworkManager, systemd-networkd, or older interface configuration files.

What is a static route in Linux?

What is a static route in Linux

A static route is a manually defined path for network traffic. Instead of letting Linux decide the route entirely through a default gateway or dynamic routing protocols, you tell it which destination network should be sent through which gateway or interface.

Static routes are useful when traffic must reach a specific subnet, VPN network, lab segment, or another router that is not covered by the default route. They are also helpful when you need predictable routing for troubleshooting or for isolated networks.

In a route entry, the important parts are:

  • Destination network, the network or host you want to reach
  • Gateway, the next hop router that knows where to send the traffic
  • Interface, the local network device used to send the packet
  • Metric, a preference value when more than one route could match

How to add a temporary static route

How to add a temporary static route

A temporary route is active until the system reboots or the routing table is changed again. This is the safest way to test a route before making it permanent.

Basic IPv4 route example

Use the modern ip tool from iproute2:

sudo ip route add 192.168.50.0/24 via 192.168.1.1 dev eth0

This tells Linux to send traffic for 192.168.50.0/24 to gateway 192.168.1.1 using interface eth0.

Add a host route

If you only want to route to one IP address, use a /32 host route:

sudo ip route add 203.0.113.25/32 via 192.168.1.1 dev eth0

Add a route with a metric

When multiple routes exist, a lower metric is usually preferred:

sudo ip route add 10.20.0.0/16 via 192.168.1.1 dev eth0 metric 100

IPv6 example

For IPv6, use ip -6 route add:

sudo ip -6 route add 2001:db8:50::/64 via 2001:db8:1::1 dev eth0

Older route command

You may still see the legacy route command on older systems, but ip route is preferred:

sudo route add -net 192.168.50.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0

The ip command is more flexible and is the standard approach on modern Linux systems.

How to add a persistent static route

How to add a persistent static route

Persistence means the route remains after reboot. The exact steps depend on the network stack your Linux distribution uses.

Ubuntu and Debian with netplan

On systems that use netplan, add routes in the YAML file under the correct interface. A common location is /etc/netplan/.

network:
  version: 2
  ethernets:
    eth0:
      addresses:
        - 192.168.1.10/24
      gateway4: 192.168.1.1
      routes:
        - to: 192.168.50.0/24
          via: 192.168.1.1

After editing the file, apply the configuration:

sudo netplan apply

If you only need the route on one interface, make sure the route is placed under that interface block. YAML indentation matters, so double-check spacing before applying changes.

RHEL, CentOS, and Fedora with NetworkManager

On NetworkManager-based systems, you can add a persistent route with nmcli or by editing the connection profile.

Using nmcli

sudo nmcli connection modify "Wired connection 1" +ipv4.routes "192.168.50.0/24 192.168.1.1"
sudo nmcli connection up "Wired connection 1"

For IPv6, use the ipv6.routes setting instead.

Using the connection file

Connection profiles are commonly stored under /etc/NetworkManager/system-connections/. You can add a route entry in the profile, then reload or restart the connection.

Because profile formats can vary by version and settings, nmcli is often the cleanest option for most administrators.

Legacy ifupdown configuration

Older Debian-based systems may use /etc/network/interfaces. You can define a route with a post-up command:

auto eth0
iface eth0 inet static
    address 192.168.1.10
    netmask 255.255.255.0
    gateway 192.168.1.1
    post-up ip route add 192.168.50.0/24 via 192.168.1.1 dev eth0
    pre-down ip route del 192.168.50.0/24 via 192.168.1.1 dev eth0

This approach works, but modern systems usually rely on netplan or NetworkManager instead.

How to choose the right method

System typeRecommended methodBest use case
Any Linux systemip route addTemporary testing or quick changes
Ubuntu with netplanNetplan YAMLPersistent routes managed at boot
RHEL, CentOS, Fedora with NetworkManagernmcli or connection profilePersistent desktop or server networking
Older Debian-based systems/etc/network/interfacesLegacy configuration environments

How to verify the routing table

After adding a route, verify that Linux accepted it and that traffic follows the expected path.

Check the route table

ip route show

For IPv6:

ip -6 route show

You can also use the older display format:

route -n

Test the route

Ping a host in the destination network:

ping 192.168.50.10

Or trace the path if you need to confirm the next hop:

traceroute 192.168.50.10

If the destination is reachable only through the new route, these tests help confirm that the gateway and interface are correct.

Useful verification checklist

  • Confirm the destination network is written with the correct subnet mask or prefix
  • Confirm the gateway is reachable from the local interface
  • Confirm the route uses the correct interface
  • Check for overlapping routes or a competing default route
  • Reboot or reload the network service to confirm persistence

How to remove or change a static route

How to remove or change a static route

If you entered a temporary route and need to remove it, use del:

sudo ip route del 192.168.50.0/24 via 192.168.1.1 dev eth0

To replace a route, delete the old one first if necessary, then add the corrected version. Some systems also support ip route replace:

sudo ip route replace 192.168.50.0/24 via 192.168.1.254 dev eth0

For persistent routes, change the network configuration file or connection profile, then reload the networking service or bring the connection down and back up.

Common problems and fixes

Most route issues come from a small set of mistakes. If the route does not work, check these points first.

Wrong subnet or prefix length

A common error is entering the wrong network range. For example, 192.168.50.0/24 is not the same as 192.168.50.0/16. A wrong prefix can send traffic to the wrong place or override another route.

Gateway is not on the local network

The next-hop gateway usually must be reachable from one of the machine’s directly connected networks. If Linux cannot reach the gateway, the route will not work.

Missing interface

If more than one interface is active, be explicit about the correct device. Omitting the interface can lead to ambiguity, especially on multi-homed systems.

Conflicting default route

If another gateway has a lower metric, Linux may still send traffic through that path. Check the full routing table and adjust metrics if needed.

Route works until reboot

This usually means the route was added with ip route add but not saved in the permanent network configuration. Use netplan, NetworkManager, or the appropriate interface configuration file for persistence.

DNS or firewall confusion

A routing problem can look like a DNS or firewall issue. If the route is present but the connection still fails, test with an IP address directly and check local firewall rules separately.

Best practices for static routes

  • Test the route temporarily before making it persistent
  • Document why the route exists and what network it serves
  • Use the most specific route that solves the problem
  • Keep metrics consistent across related routes
  • Recheck routes after network changes, upgrades, or interface renames

For broader context on network setup, it also helps to understand subnet masks, default gateways, and how Linux stores the routing table.

Frequently Asked Questions About how to add a static route in linux

What is the command to add a route in Linux?

The most common modern command is ip route add. It lets you specify the destination, gateway, interface, and optional metric.

How do I add a static route in Linux permanently?

Use your distribution’s network configuration system, such as netplan on Ubuntu, NetworkManager on RHEL-based systems, or legacy interface files on older Debian-based setups.

How do I check if a static route was added successfully?

Run ip route show or route -n, then test connectivity with ping or traceroute.

How do I make a route persist after reboot in Linux?

Add it to the active network configuration rather than using only a one-time ip route add command. The exact file or tool depends on the network stack in use.

What is the difference between ip route add and route add?

ip route add is the modern, preferred method on Linux. route add is older and still appears on some systems, but it is less flexible and is generally considered legacy.

Can I add both IPv4 and IPv6 static routes?

Yes. Use ip route add for IPv4 and ip -6 route add for IPv6, then store them in the matching persistent configuration if you need them after reboot.

Conclusion

Adding a static route in Linux is straightforward once you separate temporary testing from persistent configuration. Use ip route add to test the path first, then save the same route in netplan, NetworkManager, or the appropriate legacy config so it survives reboot. If the route fails, check the destination, gateway, interface, and metric before looking elsewhere.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *