Adding persistent static routes on centos

Adding persistent static routes on centos

When you need to access networks located on a different network segment, you need to have a route set up so the PC knows how to get to the other network segment. This generally just points to your main gateway, but you may want to set up additional static routes, where you don’t want the traffic going through your main default gateway.

To set up a static route on CentOS so that it automatically gets created every time the PC starts, you need to create a ‘route-<interface>‘ file in the ‘/etc/sysconfig/network-scripts’ folder.

Use your favorite text editor and edit/create the route-<interface> file.

eg:

# nano /etc/sysconfig/network-scripts/route-eth0

The route will get set up when the specified interfaces comes up.

This file will probably wont exist if you don’t have any existing static routes.

Add a line with the route information in the following format to the file.

x.x.x.x/y via z.z.z.z dev interface

where:
x.x.x.x is the subnet address.
y = the subnet mask. eg. 24
z.z.z.z = the gateway address
interface = the ethernet interface for this route

eg:

192.168.40.0/24 via 192.168.30.1 dev eth0

Modify the above to suit the route you want to add. If you take off the “dev eth0″ part it will automatically determine which interface to use at the time.

To make these changes take effect, you can run the following command to bring down the interface, and bring it back up again:

Take down the eth0 interface, and bring it back up:

 

# ifdown eth0; ifup eth0

or

Restart all network interfaces:

# /etc/init.d/network restart

or simply try a reboot.

Run the following command to ensure the route has been set up correctly:

$ route -n

Source