Linux Ubuntu – Two NICs separate lan

gatewaylinuxnetworkingnicrouting

I have this /etc/network/interfaces (IPs on eth1 are fake)

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 10.0.2.15
netmask 255.255.255.0
broadcast 10.0.2.255
gateway 10.0.2.2

auto eth1
iface eth1 inet static
address 123.123.123.45
netmask 255.255.255.0
broadcast 123.123.123.255
gateway 123.123.123.1

I use eth0 to connect to the internet while on eth1 I configured a public IP to a web server.

The connection to internet works fine if i keep up only eth0 and to the other side the web server works fine too if i keep up only eth1. They work perfectly as intended.

The problem is that I want both networks on at the same time and use exclusively eth0 to surf the internet / download updates and so on and I want to route all the traffic incoming to 123.123.123.45 to just that network.

How can I do that? I searched many tutorials but can't find a working solution or maybe I am doing something wrong. I do not want to reroute traffic from one interface to another, I would like to keep them separate and reroute traffic based on what IP they are coming from.

Is this possible?

I already tried

route add -host 123.123.123.45 dev eth1

Thank you in advance.

Best Answer

Your immediate problem is that you declare a default gateway on both entries. I suppose that this will result in whichever comes up last to "win" and be default gateway.

However, if you remove the one on eth1, you will have the problem that the box will reply on eth0, even if the packet arrived on eth1. In order to tell the box to reply on a particular interface, you need to do what is commonly referred to as source routing.

I have the following set of commands on a box:

/sbin/ip rule add from 1.2.3.4/24 tab 1 priority 500
/sbin/ip route add default via 1.2.3.1 dev eth2 tab 1
/sbin/ip route flush cache

The first line tells the box to look in table "1" for info on packets that go out from IP 1.2.3.4. The second line creates table "1" saying that the default gateway in that table is 1.2.3.1. The last line ensures that this takes effect immediately.

For more info, see e.g. http://lartc.org/howto/lartc.rpdb.html