How To Add Static Routes In Linux

Process to add static route in RHEL 7 or RHEL 8  

Adding Static Routes On Various *NIX (Linux,AIX,HP-UX)

Static routes are generally required for  traffic that must not, or should not, go through the default gateway. In this article we will discuss how to add static routes in variox nix.

Scenario : Suppose if you want that all the traffic to network 172.168.102.0/24 should use 172.168.101.1 as gateway. This can be done by adding a static route in the kernel routing table as shown below.

Adding Static Route in Linux from the Command line :

# route add -net 172.168.102.0 netmask 255.255.255.0 gw 172.168.101.1 dev eth0

OR

# ip route add 172.168.102.0/24 via 172.168.101.1 dev eth0

Above Commands will make changes to the routing table temporary and not permanent. Use any of below mention command To check Routing tables in Linux :

[root@amitmaheshwari.in ~]# route -n
[root@amitmaheshwari.in ~]# netstat -nr

Steps to make the static Route Persistent Across the reboot :

In case of RHEL5.X / CentOS 5.X

Create a file  route file

[root@amitmaheshwari.in ~]# vi /etc/sysconfig/network-scripts/route-eth0

172.168.102.0/24 via 172.168.101.1 dev eth0

Save and close the file and  Restart network service

[root@amitmaheshwari.in ~]# service network restart

In case of RHEL6.X / CentOS 6.X

[root@amitmaheshwari.in ~]# vi /etc/sysconfig/network-scripts/route-eth0

GATEWAY= 172.168.101.1
NETMASK=255.255.255.0
ADDRESS= 172.168.102.0

Save and close the file and  Restart network service

[root@amitmaheshwari.in ~]# service network restart

In case of RHEL 7 & RHEL8 
[root@amitmaheshwari.in ~]#  systemctl restart NetworkManager
[root@amitmaheshwari.in ~]# systemctl restart network