In ubuntu/CentOS you can add a static route, but if its temporary it won’t withstand a network restart/reboot/powercycle . So we need to add it permanent/persistent .
To add a temporary route in ubuntu , you can run the below command , here 192.168.10.0 network is routed via 192.168.1.1 gateway . You will need this rule only if you are not doing a persistent rule .
route add -net 192.168.10.0 netmask 255.255.255.0 gw 192.168.1.1 eth0
To make this a permanent route, use your favorite editor and edit the /etc/network/interfaces file
nano /etc/network/interfaces
Add the following rule :
up route add -net 192.168.10.0/24 gw 192.168.1.1 dev eth0
So once the route is added, it is better to restart the network manager but if you have multiple interfaces and in a production enviornment , you can just restart the specific interface
ifdown eth0 && ifup eth0
To create a temporary route in CentOS :
ip route add 192.168.10.0/24 via 192.168.1.1 dev eth0
So in CentOS the network file is different. To create route for a specific interface, we need to create a route file forthat interface in the network-scripts location . In this case , eth0 is our interface
nano /etc/sysconfig/network-scripts/route-eth0
192.168.10.0/24 via 192.168.1.1 dev eth0
service network restart or
ifdown eth0 && ifup eth0