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 0.0.0.0 192.168.206.1 0.0.0.0 UG 100 0 0 eno1 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eno1 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0 192.168.206.0 0.0.0.0 255.255.255.0 U 100 0 0 eno1
route -n command
If you want to add a route to the network 188.8.131.52 through gateway 192.168.206.1, you can execute the following command.
$ sudo route add -net 184.108.40.206 netmask 255.255.255.0 gw 192.168.206.1 eno1
Alternatively, you can use ip command as follows:
$ sudo ip route add 220.127.116.11/24 via 18.104.22.168 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.
‘10.9.8.0/24 via 10.9.8.100 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 0.0.0.0 10.9.8.100 0.0.0.0 UG 100 0 0 ens192 10.0.0.0 0.0.0.0 255.0.0.0 U 100 0 0 ens192 10.9.8.0 10.9.8.100 255.255.255.0 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 10.9.7.0/24 gw 10.9.8.100 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 address 10.9.8.41 netmask 255.255.255.0 gateway 10.9.8.100 up route add -net 10.9.7.0/24 gw 10.9.8.100 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 0.0.0.0 10.9.8.100 0.0.0.0 UG 0 0 0 ens160 10.9.7.0 10.9.8.100 255.255.255.0 UG 0 0 0 ens160 10.9.8.0 0.0.0.0 255.255.255.0 U 0 0 0 ens160 10.233.70.0 10.9.8.45 255.255.255.0 UG 0 0 0 tunl0 10.233.90.0 0.0.0.0 255.255.255.0 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 01-network-manager-all.yaml
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’
routes: - to: 192.168.205.0/24 via: 192.168.206.1
# Let NetworkManager manage all devices on this system network: version: 2 renderer: NetworkManager ethernets: eno1: dhcp4: true routes: - to: 192.168.205.0/24 via: 192.168.206.1
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.