1

I'm trying to isolate outbound network traffic to a specific target for metering purposes, so I thought I'd route it via a dedicated IP address. In order to achieve this I have setup a subinterface eth0:1, which appears as follows:

# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast  state UP group default qlen 1000
link/ether fa:16:3e:42:ae:d3 brd ff:ff:ff:ff:ff:ff
inet 192.168.111.6/24 brd 192.168.111.255 scope global eth0
   valid_lft forever preferred_lft forever
inet 192.168.111.5/24 brd 192.168.111.255 scope global secondary eth0:1
   valid_lft forever preferred_lft forever

So far so good. The routing piece that's supposed to make it work is this:

# ip route add 10.10.0.10 via 192.168.111.1 dev eth0:1

When I check the routing table, I get:

# ip route list
default via 192.168.111.1 dev eth0 
10.10.0.10 via 192.168.111.1 dev eth0 
192.168.111.0/24 dev eth0  proto kernel  scope link  src 192.168.111.6 

Notice the 2nd line, which ends in dev eth0 rather than eth0:1 as I'd expect but so far this could just be a display issue. However, when observing the traffic using tcpdump, it's obvious the route is behaving as the above display suggests, ie: traffic to 10.10.0.10 transits via the main interface eth0, using its IP 192.168.111.6 rather than eth0:1's IP 192.168.111.5.

The weird thing is that if I use a dedicated NIC (eth1) rather than a subinterface as above, everything works as expected. Is this a limitation of subinterfaces, a bug or am I doing smth wrong?

The OS is Ubuntu 14.04.3 server running on kernel 3.13.0-76. It's a guest VM hosted on a KVM hypervisor (though I doubt this is a factor).

2

You should be able to accomplish what you want using this command:

ip route add 10.10.0.10 dev eth0:1 src 192.168.111.5

If you enter ip route list you should see the change of src.

  • Many thanks Oliver; the only thing I had to add was the next hop, as in: ip route add 10.10.0.10 via 192.168.111.1 dev eth0:1 src 192.168.111.5; this works flawlessly. Thanks again! – sxc731 Feb 3 '16 at 13:03
  • Actually, you don't even need "dev eth0:1". – Laszlo Valko Feb 4 '16 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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