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