Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

On Linux, is it possible to have all requests done to a specific IP to be routed through a virtual interface, so that on the other end they appear with a different IP address that the host's main? Suppose that eth0 has the IP address 1.1.1.1 while eth0:1 has the address 1.1.1.2. How can I make all requests to 1.2.3.4 appear as coming from eth0:1? I am trying the following but it doesn't seem to work:

route add 1.2.3.4 dev eth0:1
share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Yes.

One way to do this would be to use IPTABLES to rewrite the packets leaving the machine with multiple IP addresses to say they come from the virtual interface. Try

/sbin/iptables -t nat -I POSTROUTING -d DEST.IP -j SNAT --to VIRTUAL.IP

(You do, of-course, need to have the virtual IP set up as you contemplated, ie eth0:1 or equivalent needs to exist so traffic can find its way back to the machine)

share|improve this answer
    
Thanks, worked like a charm! –  Georgios Gousios Feb 8 '13 at 19:12
add comment

You should be able to add additional addresses to an interface, if that's what you are asking.

I know this is possible using iproute2's ip command:

ip addr add 192.168.1.10/24 dev eth0
ip addr add 192.168.2.10/24 dev eth0

ifconfig will only show the first one, IIRC. You can use ip addr show eth0 instead.

If that's not what you were looking for, try this similar question

share|improve this answer
    
This is not I asked. I already have mutliple virtual interfaces on my machine with different IPs. Thanks anyway! –  Georgios Gousios Feb 8 '13 at 19:14
add comment

It may be necessary to use the command

ip route add 1.2.3.4 dev eth0:1 src 1.1.1.2

or route with a similar option. If that does not work as expected you may need advanced routing: Create an additional routing table for this target in /etc/iproute2/rt_tables, put the above entry as the only one into this table and activate this table:

ip rule add to 1.2.3.4 priority 100 table to__1_2_3_4
ip route flush cache

The desperate last resort solution would be netfilter: You could use SNAT with iptables in the POSTROUTING chain to rewrite the source address for packets to this destination.

share|improve this answer
    
Unfortunately, I had to resort to SNAT (see answer above), tuning IP routing tables is not my strong point :-) Thanks! –  Georgios Gousios Feb 8 '13 at 19:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.