Wednesday, February 11, 2009

How to add static route through a virtual (alias) interface in RHEL5?

I have two ips assigned to to my eth0. One for eth0 and the other for eth0:1 and both ips are from the same network.

eth0 Link encap:Ethernet HWaddr 00:16:3E:74:30:8B
inet addr: Bcast: Mask:

eth0:1 Link encap:Ethernet HWaddr 00:16:3E:74:30:8B
inet addr: Bcast: Mask:

The entries added by default in routing table would be as below. dev eth0 proto kernel scope link src dev eth0 scope link
default via dev eth0

The default route means that the ip address of eth0:1 is not used as source address while contacting any machines in the network from this machine. Now I want all outgoing connetions to a specific machine in the network to be initiated with the source address of eth0:1. How this can be achieved?

1 - Using the "route" command. (This configuration is not persistent on reboot)

# route add -host destination-ip dev eth0:1


# route add -host dev eth0:1

After running the above command, all connections to should have the source address of eth0:1

2 - Through /etc/sysconfig/network-scripts/route-eth0:1 (This would be persistent on reboots). The tricky part comes in this configuration. Most people would add a line as below to this file which wouldn't give the expcted output.

destination-ip/32 dev eth0:1

Eg, dev eth0:1

The above line would add a routing to via eth0:1, but the source ip address used would be the ip of eth0, not eth0:1. This can be verified by running "ip route show" and netstat.

/etc/sysconfig/network-scripts/route-eth0:1 should have the below entry in it to use eth0:1's ip as source ip while contacting

destination-ip/32 dev eth0:1 src source-ip

Eg, dev eth0:1 src

How to verify this is working as expected?

From the system do "telnet 80" and check the output of "netstat -nalp | grep 80" on both source and destination machine. It should show the ip of eth0:1 as the source address.


Brent said...

Sorry to comment on a pretty old post, but this is exactly what I'd like to do (CentOS 5) to make the route persistent. But whenever I add the "src x.x.x.x" to the route-eth1 file, I receive "RTNETLINK answers: invalid argument".

Using "ip route add" allows me to add the src parameter no problem and all works well. I could write a startup script to do this, but I'm confused what would be blocking this in the route-eth1 file. Any ideas?

Sadique Puthen said...

What version of centos5 are you using?

What is the output of "cat route-eth1"?

Oliver said...

Well I tried everything and it still doesn't work
Im using RHEL6 ( on VMWare ESX4 ) and here is my config
route-eth0:1 : dev eth0:1 src

ifcfg-eth0:1 :

when I do ip route the route is still fix to eth0

ip route dev eth0 scope link src

I read maybe it has to do with some filter on RHEL ?

Sadique Puthen said...

Oliver, I think you have everything configured correctly.

Follow "How to verify this is working as expected?" to verify it's configured properly.