How to Add Persistent Static Routes on RHEL 6/CentOS 6

This example will show you on how to configure the Persistent static route in RedHat Enterprise Linux 6 (RHEL 6) server. This configuration may different with other version of RHEL but the basic is almost the same. Persistent static route will permanently stored the setting and will not be deleted after rebooted. The following setting is just example only, you need to update the ip addresses and netmask in order to fit your environment :

To add a persistent static route in Redhat Enterprise Linux 6, create a file called route-X in the /etc/sysconfig/network-scripts/ directory. In this case, i will add persistent static route for eth0 and eth1.
1. Create a file route-eth0 rot eth0 routing :

[root@rhel6 ~]# vi /etc/sysconfig/network-scripts/route-eth0
ADDRESS0=192.168.1.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.1.1

2. Create a file route-eth1 rot eth1 routing :

[root@rhel6 ~]# vi /etc/sysconfig/network-scripts/route-eth1
ADDRESS1=192.168.2.0
NETMASK1=255.255.255.0
GATEWAY1=192.168.2.1

Display routing table :

[root@rhel6 ~]# netstat -rn

How to Configure Static Routes on CentOS 6.2

Routing is the process of selecting paths in a network along which to send network traffic. It’s not necessary to configure Static Routes on Red Hat Enterprise Linux servers or CentOS servers since routing will be configured on routing devices such as routers, bridges, gateways, firewalls and switches. However, if static routes are required they can be configured for each interface. This can be useful when you have multiple network interfaces in different subnets such as eth0, eth1 and eth2. The following static route commands has been tested on Linux CentOS 6.2 server.

Display routing table using the following command :

[root@centos62 ~]# netstat -rn

This command will add the Static Routes on CentOS 6.2 server. For example network 192.168.2.0/24 available via 192.168.2.1 on device eth1 :

[root@centos62 ~]# ip route add 192.168.2.0/24 via 192.168.2.1 dev eth1

Alternatively, you can use this command :

[root@centos62 ~]# route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1 dev eth1

Note : The drawback of using these two commands is it will forget the static routes when server rebooted.

How to Add Persistent Static Routes in CentOS 5.6

This example will show you on how to configure the persistent static route in CentOS 5.6 server. This configuration may different with other version of CentOS but the basic is almost the same. Persistent static route will permanently stored the setting and will not be deleted after rebooted.

1. Go to /etc/sysconfig/network-scripts/

[root@server ~]# cd /etc/sysconfig/network-scripts/

2. Modify the original default route to your requirement.

[root@server network-scripts]# vi route-eth0
192.0.2.0/24 dev eth0 scope host
default via 192.0.2.1

Change to below :

192.0.2.0/24 dev eth0 scope host
default via 192.0.2.1
192.168.2.0/24 via 192.168.13.1 dev eth0

3. Restart the network to apply the new routing table

[root@server network-scripts]# service network restart
[root@server network-scripts]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.2.0     192.168.13.1    255.255.255.0   UG        0 0          0 eth0
192.0.2.0       0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
0.0.0.0         0.0.0.0         0.0.0.0         U         0 0          0 eth0

How to Add Static Routes in CentOS 5.6

This tutorial was performed at my VPS server, therefore the NIC interface is using venet0 instead of eth0. This may different with your environment but all the steps should be same. For your information, this configuration will be erase once you rebooted the server. You should use persistent static route if you want to permanently apply the routing table.

This is my original routing table

[root@server ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.0.2.0       0.0.0.0         255.255.255.0   U         0 0          0 venet0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 venet0
0.0.0.0         0.0.0.0         0.0.0.0         U         0 0          0 venet0

1. Static route for a specific network
To add a static route for a specific network in Linux, issue the following command :

[root@server ~]# route add -net 192.168.2.0/24 gw 192.168.13.1
[root@server ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.2.0     192.168.13.1    255.255.255.0   UG        0 0          0 venet0
192.0.2.0       0.0.0.0         255.255.255.0   U         0 0          0 venet0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 venet0
0.0.0.0         0.0.0.0         0.0.0.0         U         0 0          0 venet0

To delete a static route for a specific network in Linux, issue the following command :

[root@server ~]# route del -net 192.168.2.0/24 gw 192.168.13.1
[root@server ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.0.2.0       0.0.0.0         255.255.255.0   U         0 0          0 venet0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 venet0
0.0.0.0         0.0.0.0         0.0.0.0         U         0 0          0 venet0

2. Static route for a specific host
To add a static route for a specific host in Linux, issue the following command :

[root@server ~]# route add -host 192.168.2.5 gw 192.168.13.1
[root@server ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.2.5     192.168.13.1    255.255.255.255 UGH       0 0          0 venet0
192.0.2.0       0.0.0.0         255.255.255.0   U         0 0          0 venet0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 venet0
0.0.0.0         0.0.0.0         0.0.0.0         U         0 0          0 venet0

To delete a static route for a specific host in Linux, issue the following command :

[root@server ~]# route del -host 192.168.2.5 gw 192.168.13.1
[root@server ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.0.2.0       0.0.0.0         255.255.255.0   U         0 0          0 venet0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 venet0
0.0.0.0         0.0.0.0         0.0.0.0         U         0 0          0 venet0

To add a default gateway.

[root@server ~]# route add default gw 192.168.13.1
[root@server ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.0.2.0       0.0.0.0         255.255.255.0   U         0 0          0 venet0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 venet0
0.0.0.0         192.168.13.1    0.0.0.0         UG        0 0          0 venet0
0.0.0.0         0.0.0.0         0.0.0.0         U         0 0          0 venet0

To delete a default gateway.

[root@server ~]# route del default gw 192.168.13.1
[root@server ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.0.2.0       0.0.0.0         255.255.255.0   U         0 0          0 venet0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 venet0
0.0.0.0         0.0.0.0         0.0.0.0         U         0 0          0 venet0