The situation:

eth0 - Default network with internet access IP: Defatult Gateway:

eth0:1 Virtual NIC, same thing as eth0 but different IP. Same cable This ip has a different internet access



All traffic from eth1 and eth2 are routed to using

I need to route all traffic coming from eth2 to but using the secondary ip

I tried a lot of different configurations but no success.


What you want is called source policy routing. Instructions for configuring source policy routing can be found here in the Linux Advanced Routing & Traffic Control HOWTO.

The crux of it is that you want to add a new routing table to your system with a name like INET2 (these commands must be run as root):

echo 200 INET2 >> /etc/iproute2/rt_tables

Then you want to add a source rule to the main IP routing table (I'm guessing this is a /24 network):

ip rule add from table INET2

Next you need to assign a default route for the INET2 table (you did not mention the default gateway for your second connection so I'm guessing here):

ip route add default via dev eth0:1 table INET2

Lastly, you need to flush the route cache on the system:

ip route flush cache

Rules created this way are ephemeral and will disappear when the system is restarted (though the INET2 table will persist) so you'll need to put the rules into a startup script for the network interface. Exactly how to do this depends on your particular distribution of Linux and is really another question.

  • It worked! It didn't worked with a virtual interface on Debian, but I installed a spare NIC and it worked. Thank you. – mtbrandao Sep 28 '17 at 19:32

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.