How to add a new static route on RHEL7 Linux

Previously we have talked about how to configure a default gateway on RHEL7 Linux. This time we will talk about how to add a static route on RHEL7 Linux.

Any network we are trying to reach is accessed via default gateway only if it is not implicitly overwritten by another static route definition. Let’s have a look at a current routing table on our Redhat 7 Linux box:

root@rhel7 ~]# ip route show
default via dev enp0s3  proto static  metric 1024 dev enp0s3  proto kernel  scope link  src

From the above we can see that any packets to reach a destination network ID should travel via enp0s3 interface with and any other destination network not implicitly defined should use a default gateway

To add a new static route means to define yet another destination network as well as specify via which IP address and interface the packet should travel through in order to reach its destination. For example, let’s add a static route to destination network via ip address and enp0s3 interface. To do this create a new file route-enp0s3 in a /etc/sysconfig/network-scripts/ directory. To define an above mentioned persistent static route add a following line into your enp0s3 file: via dev enp0s3

Once done restart your network:

root@rhel7 ~]# systemctl restart network

Next, check whether your new static route was correctly defined:

[root@rhel7 ~]# ip route show