Linux : How to add a network static route

By | June 14, 2011

Here is the traditional way to set static route on Linux OS.

route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

The best way to make it permanent, is to add the gateway directly to the interface by editing the interface script :

/etc/sysconfig/networking/devices/ifcfg-eth0

add :

GATEWAY=192.168.1.1

To remove a static route :

route del -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

On RHEL/CentOS Linux, there is a way to set this per interface through a single config file.

Edit the following file (create if non-existent) :

vi /etc/sysconfig/network-scripts/route-eth0

Append the following lines and modify with your values :

default 0.0.0.0 dev eth0
0.0.0.0/24 via 0.0.0.1 dev eth0
0.0.2.0 via 0.0.2.1 dev eth0

*Include the default route on the first line and then add as many subnet as you have.