Route all traffic from one interface to default gateway with different IP

5,407

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 192.168.1.0/24 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 192.168.2.253 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.

Share:
5,407

Related videos on Youtube

mtbrandao
Author by

mtbrandao

Updated on September 18, 2022

Comments

  • mtbrandao
    mtbrandao over 1 year

    The situation:

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

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

    eth1: 192.168.0.1

    eth2: 192.168.1.1

    All traffic from eth1 and eth2 are routed to 192.168.2.254 using 192.168.2.1

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

    I tried a lot of different configurations but no success.

  • mtbrandao
    mtbrandao over 6 years
    It worked! It didn't worked with a virtual interface on Debian, but I installed a spare NIC and it worked. Thank you.