How to Setup Persistent Static Routes on Linux CentOS 6.4

linuxStatic routing is a type of method of network routing. Static routing is not actually a routing protocol but it’s just a manual configuration on the network route. This usualy managed by network system administrator. In Linux, static route can be added and delete using “route” command. But the drawback is when linux server is rebooted, it will forget the static routes configuration. To avoid network interruption on the linux server, you can configure static route to take effect immediately without rebooting and also should apply after the next reboot. Static Routes that apply after next reboot is called Persistent Static Routes. To achieve this on CentOS 6.4, the following steps should be perform :

1. To make configuration take effect immediately, run the following command :

[root@centos62 ~]# route add -net <network> netmask <netmask> gw <gateway_ip> dev <interface>

Examples :

[root@centos64 ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0
[root@centos64 ~]# route add -net 172.13.1.0 netmask 255.255.0.0 gw 172.13.1.1 dev eth1

2. To apply the configuration on next reboot, configure the following in /etc/sysconfig/static-routes :

[root@centos62 ~]# vi /etc/sysconfig/static-routes

Add the followings :

any -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1
any -net 172.13.1.0 netmask 255.255.0.0 gw 172.13.1.1

Three Ways to Configure Persistent Static Routes on Linux CentOS

centos6There are several different ways to configure persistent static routes on Linux CentOS. By using Static routes, we can improves overall performance of the network and static routing is achieved by manually adding routes to the routing table. Static route can be added and delete using “route” command. But it will forget the static routes configuration when server rebooted. This will lead to the network interruption. Follow one of these three options to configure static route on CentOS.

1. Edit the file /etc/sysconfig/network-scripts/route- and then add the following lines :

[root@centos63 ~]# vi /etc/sysconfig/network-scripts/route-eth0

GATEWAY0=<gateway address>
NETMASK0=<network netmask>
ADDRESS0=<network address>
GATEWAY1=<gateway address>
NETMASK1=<network netmask>
ADDRESS1=<network address>

Examples :

GATEWAY0=192.168.1.1
NETMASK0=255.255.255.0
ADDRESS0=192.168.1.54
GATEWAY1=192.168.2.1
NETMASK1=255.255.255.0
ADDRESS1=192.168.2.54

2. Edit the file /etc/sysconfig/network-scripts/route- and then add the following lines :

192.168.1.0/24 via 192.168.1.1 dev eth0
192.168.2.0/24 via 192.168.2.1 dev eth0

3. Edit the file /etc/sysconfig/static-routes as below :

any -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1
any -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1

How to Configure Persistent Static Routes on Linux CentOS 6.2

In this post, i will share on how to configure persistent static routes on linux CentOS 6.2 server. By using Static routes, we can improves overall performance of the network. Static route can be added and delete using “route” command. But the drawback is when linux server is rebooted, it will forget the static routes configuration. To avoid network interruption the following condition should be follow :

1. The static route configuration should take effect immediately without rebooting.
2. The static route configuration should also apply after the next reboot.

Solutions :
To make configuration take effect immediately, run the following command :

[root@centos62 ~]# route add -net <network> netmask <netmask> gw <gateway_ip> dev <interface>

Examples :

[root@centos62 ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0
[root@centos62 ~]# route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1 dev eth1
[root@centos62 ~]# route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.3.1 dev eth2

To apply the configuration on next reboot, configure the following in /etc/sysconfig/static-routes :

[root@centos62 ~]# vi /etc/sysconfig/static-routes

Add the followings :

any -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1
any -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1
any -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.3.1

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