Configuring Static Routes on CentOS 4

Last night we did some upgrades on a system in our datacenter. Among other things moving a few services from physical computers to virtual ones. One of these new machines needed contact with three different physical networks, and even more subnets.
If you don’t want to read about my whole example network, skip to the “fun part”.

In this blog entry I will use some bogus internal network addresses. We had the following:

eth0 directly connected to
eth1 directly connected to
eth2 directly connected to

Our new (virtual) server was configured using as default gateway, via eth1. But we also needed to reach the following networks via eth2:


Configuring this “on-the-fly” is easy. All we have to do is run the following commands as root:

route add -net gw
route add -net gw
route add -net gw
route add -net gw

As you have guessed, is the gateway being connected to eth2. Now the following is taking place:

Traffic for is directly pushed out eth0, no routing needed.
Traffic for is directly pushed out eth1, no routing needed.
Traffic for is directly pushed out eth2, no routing needed.
Traffic for,, and is pushed to gateway via eth2.
Everything else is directed to gateway via eth1.

Fun Part

To make this routing permanent, meaning it will return upon reboot, we need to store this information somewhere. In this case we’re using CentOS 4, so the file we need to edit is /etc/sysconfig/static-routes. Per default this file doesn’t exists, at least it didn’t on my machine, so I created one and entered the following:

any net 192.168.20/24 gw
any net 192.168.30/24 gw
any net gw
any net gw

Also, check the files /etc/sysconfig/network-scripts/ifcfg-ethx, replace x. Only eth1, in my example, should have a line which says “GATEWAY=”. If anyone of the other files also has a line which starts with “GATEWAY”, something will most likely go wrong.

I’m not sure how interesting this is for anyone. But at least I hope someone will benefit from it. I might start some more “in-depth” articles about network configuration in the future.

Please leave a comment if you found this useful, or ask questions if there is something I can improve.

4 thoughts on “Configuring Static Routes on CentOS 4

  1. Hi, I found your blog on this new directory of WordPress Blogs at I dont know how your blog came up, must have been a typo, i duno. Anyways, I just clicked it and here I am. Your blog looks good. Have a nice day. James.

  2. …Traffic for,, and is pushed to gateway via eth1…..

    You mean via eth2?! Network is not connected to eth1. Please, correct me if I am wrong.

Leave a Reply