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:10.65.7.160 Bcast:10.65.7.255 Mask:255.255.254.0

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

The entries added by default in routing table would be as below.

10.65.6.0/23 dev eth0 proto kernel scope link src 10.65.7.160
169.254.0.0/16 dev eth0 scope link
default via 10.65.7.254 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

Eg,

# route add -host 10.65.6.1 dev eth0:1

After running the above command, all connections to 10.65.6.1 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,

10.65.6.1/32 dev eth0:1

The above line would add a routing to 10.65.6.1 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 10.65.6.1.

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

Eg,

10.65.6.1/32 dev eth0:1 src 10.65.6.10

How to verify this is working as expected?

From the system do "telnet 10.65.6.1 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.

4 comments:

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 :
10.133.141.223/32 dev eth0:1 src 10.133.142.228

ifcfg-eth0:1 :
DEVICE=eth0:1
HWADDR=00:0C:29:3D:CB:A8
MTU=1500
NM_CONTROLLED=no
ONBOOT=yes
BOOTPROTO=none
IPADDR=10.133.142.228
NETWORK=10.133.142.0
SRCADDR=10.133.142.228
NETMASK=255.255.255.0
NAME=eth0:1
TYPE=Ethernet
IPV6INIT=no
USERCTL=no
DHCPCLASS=""

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

ip route
10.133.141.223 dev eth0 scope link src 10.133.142.228

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.