When one subnet must leave through a specific gateway, adding a static route is safer than changing the default route for the whole host. A narrow route sends only the selected destination prefix to the alternate next hop, which is useful for VPN ranges, partner networks, lab subnets, or temporary migration paths.
Linux stores active routes in kernel routing tables, and ip route from iproute2 can add a route immediately with a destination prefix, a via next hop, and a dev interface. The next hop must be reachable from that interface unless the route is deliberately marked onlink, so check the connected subnet before adding the entry.
A route added with ip route add changes live traffic as soon as it succeeds and may disappear when the interface is restarted or the host reboots. Save long-lived routes in the network stack that owns the interface, such as NetworkManager, netplan, or systemd-networkd, after the live route has been checked.
Related: How to show routes in Linux
Related: How to check the default route in Linux
Related: How to remove a static route in Linux
Tool: Static Route Commands Generator
Steps to add a static route in Linux:
- Confirm that the next-hop gateway is on a connected subnet for the selected interface.
$ ip route show 192.0.2.0/24 dev eth1 192.0.2.0/24 dev eth1 proto kernel scope link src 192.0.2.40
Replace 192.0.2.0/24, 192.0.2.1, and eth1 with the connected network, gateway, and interface from the host being changed.
- Add the static route to the active routing table.
$ sudo ip route add 203.0.113.0/24 via 192.0.2.1 dev eth1
Traffic for 203.0.113.0/24 starts using this gateway immediately. Use the narrowest destination prefix that matches the required network.
- Display the new route entry.
$ ip route show 203.0.113.0/24 203.0.113.0/24 via 192.0.2.1 dev eth1
No output means the route was not added to the main table. If the route belongs in another table, include table <table> in both the add and show commands.
- Ask the kernel which path it will use for a destination inside the routed network.
$ ip route get 203.0.113.50 203.0.113.50 via 192.0.2.1 dev eth1 src 192.0.2.40 uid 1000 cacheThe via and dev values should match the static route. This lookup does not send packets to the destination.
- Save the route in persistent network configuration if it must survive interface reloads or reboots.
Use the tool that owns the interface on that host, such as a NetworkManager profile, a netplan YAML file, or a systemd-networkd unit. Recheck with ip route show 203.0.113.0/24 after reloading the connection.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.