Permanently add Static Route in Linux

Static routing is the process of manually entering the routes to the routing table to reach a particular destination. Basically two commands are used in Linux to add routes. The first command is the old traditional route add and second is the IP route command.

Earlier we learned how to route add in Linux, in this tutorial, here I will show you how to add permanent static routes in Linux distributions such as Centos and Ubuntu.

Adding temporary static routes

In order to add a static route temporarily on your Linux machine, you can use either route or ip command.

You can list the current routing table as follows.

$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface UG 100 0 0 eno1 U 1000 0 0 eno1 U 0 0 0 docker0 U 100 0 0 eno1

route -n command

If you want to add a route to the network through gateway, you can execute the following command.

$ sudo route add -net netmask gw eno1

Alternatively, you can use ip command as follows:

$ sudo ip route add via dev eno1

Adding permanent static routes

In the above section, we saw how to add routes in Linux. But the system will forget those routes on next reboot.

So we have to add routes to config file to make it persistent.

On RHEL or CentOS, you need to modify the interface file in ‘/etc/sysconfig/network-scripts’.

For example, here, we have to add routes on network interface ens192. Hence, the file we need to modify will be ‘/etc/sysconfig/network-scripts/route-ens192’.

The basic syntax of static route in this file is:

<target-address> via <gateway-address> dev <interface>


target-address – Destination network address.

gateway-address – Router address to reach the destination address.

dev – Indicates which interface to reach the destination, interface naming schemes are eno[1-N], ens[1-N], and or eth[0-N].

So, in order to add the above route in ‘route-ens192’ file, append the following line.

‘ via dev ens192’

Then, you need to restart the network service.

$ sudo service network restart

Finally, verify that the new routes are visible in the routing table using the following command.

$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface UG 100 0 0 ens192 U 100 0 0 ens192 UG 100 0 0 ens192

On Ubuntu Linux, if you want to add a permanent static route to your system, you have to add route entry to the ‘/etc/network/interfaces’. Using your favorite editor (nano, vim,…) to open the network interface file and insert the following lines to it.

‘up route add -net gw dev ens160’. For example:

source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto ens160
iface ens160 inet static
up route add -net gw dev ens160

In order to make the change take effect, you need to restart the networking service by running:

$ sudo /etc/init.d/networking restart

Verify that the route has been configured correctly:

$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface UG 0 0 0 ens160 UG 0 0 0 ens160 U 0 0 0 ens160 UG 0 0 0 tunl0 U 0 0 0 *

From Ubuntu 18.04 LTS and higher version, the operating system uses the netplan YAML files for network configuration – it is located in the ‘/etc/netplan’ directory. For example:

$ ls /etc/netplan

In order to add persistent static route, you have to add the following lines to the netplan configuration file ‘/etc/netplan/01-network-manager-all.yaml’

- to:

For example:

# Let NetworkManager manage all devices on this system
version: 2
renderer: NetworkManager
dhcp4: true
- to:

Apply the change by running:

$ sudo netplan apply

You can verify the route configuration by running command route -n or ip route. Here is the result:

The result after apply a new static route

In this tutorial, we learned how to add a permanent route by adding to respective config files in Linux. Thanks for reading and please leave your suggestion in the below comment section.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.