Let’s say that you have a box that lives on subnet 172.16.16.0 /24. Your default route is set in /etc/sysconfig/network as 172.16.16.1, as shown below:
Now you need to get to network 22.214.171.124 /24 using gateway 172.16.16.250 and network 126.96.36.199 /24 using gateway 172.16.16.254. One option is to add those routes manually with the following commands:
# route add [-net|-host] netmask gw dev X
route add -net 188.8.131.52 netmask 255.255.255.0 gw 172.16.16.250
route add -net 184.108.40.206 netmask 255.255.255.0 gw 172.16.16.254
This will work in the short term, and can be doublechecked using the ‘route’ command – sample output shown below:
Destination Gateway Genmask Flags Metric Ref Use Iface
172.16.16.0 * 255.255.255.0 U 0 0 0 eth0
220.127.116.11 172.16.16.250 255.255.255.0 UG 0 0 0 eth0
18.104.22.168 172.16.16.254 255.255.255.0 UG 0 0 0 eth0
default 172.16.16.1 0.0.0.0 UG 0 0 0 eth0
NOTE: once the machine is rebooted, those statis routes will disappear, as they are stored in memory and are not recreated on startup.
To add a persistent static route in Redhat Enterprise Linux or CentOS, create a file called route-X in the /etc/sysconfig/network-scripts/ directory where is the interface number and X is the interface number. As you would expect, these are specified in separate files for each of the available interfaces.
In this particular case, we will be creating a file called ‘route-eth0’ in /etc/sysconfig/network-scripts in order to make those routes persistent through reboot, and populating it with the information shown below
22.214.171.124/24 via 172.16.16.250
126.96.36.199/24 via 172.16.16.254
Once that file has been modified, run the following command to restart the network:
service network restart
After that, run the route command and make sure that your routes are in place.