Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static

auto eth1
iface eth1 inet static

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 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 dev eth1

Thank you in advance.

share|improve this question
I wrote 2 gateways because atm I am working disabling either one or another interface whenever I need to run an apt-get update or so. Most of the time eth0 is shut and the webserver is working fine. What I want is: every traffic from and to must go through eth1 (and therefore throught every traffic from and to the internet and my machine EXCEPT the ones on network must flow through eth0. Is this possible? – James Loy Jul 4 '11 at 12:14

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 tab 1 priority 500
/sbin/ip route add default via 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 The second line creates table "1" saying that the default gateway in that table is The last line ensures that this takes effect immediately.

For more info, see e.g.

share|improve this answer
I try this solution and let you know. It seems easy. – James Loy Jul 4 '11 at 12:16
I removed the last gateway from config and run those commands in sequence ` /sbin/ip rule add from tab 1 priority 500 /sbin/ip route add default via dev eth1 tab 1 /sbin/ip route flush cache ` but nothing works – James Loy Jul 4 '11 at 12:21
Not sure what the effect will be if you name the whole subnet in the rule. I think you want /sbin/ip rule add from 123.123.123.*45*/24 tab 1 priority 500 – Bittrance Jul 7 '11 at 23:10

Having two default gateways with the same metric results in a lottery for which one will be used. This explains your problems.

You can either follow the advice from Bittrance or make sure that your two default routes have different metrics. In that case the one with the smaller metric will be used unless the route is down. However, this only works if the second default gateway actually DOES provide a route to the public Internet.

In order to set up two default routes with different metrics, use the metric keyword in the ip route add command.

I personally would remove the gateway instructions from the interfaces file and rather add post-up commands.

EDIT Based on your additional comment, I am a little confused as to what exactly you want to achieve and how this network is set up.

Clearly the interface with the public IP must be connected directly to the Internet (presumably through a device provided by your ISP). If this server is also your default gateway to the Internet, i.e. all requests for public IP address must flow through this server, then the server must be configured as a router (i.e. IP forwarding must be enabled, and all other devices on the network must have the private IP of this server as their default gateway).

If your network has another computer which provides the default internet access, then that device must be set as the default gateway on all other network devices, and your server should not have a default route at all on its internal interface (eth0), since it is directly connected to that network.

share|improve this answer
Public interface[SERVER]-- Private interface. The box is a VirtualBox Ubuntu in which the 1st interface eth0 is natted to the main server and used to surf the internet freely. It's on a private network. The second one is bridged to a public interface directly connected to the internet without natting, behind a firewall. What I ask is to keep networks separate. The traffic from and to must flow through eth1. Everything else should go to eth0. The default gateway so should be the one on eth0. – James Loy Jul 5 '11 at 7:46
You could try source based routing, but that has gone out of fashion a long time ago. Nowadays IP routing is primarily destination based, and in your case it simply doesn't make any sense to route your "normal" internet traffic through a different gateway than the traffic coming in through a specific public IP. – wolfgangsz Jul 5 '11 at 8:40 Tried this too. No ways of making it work. I think I'll keep enabling and disabling the eth. – James Loy Jul 5 '11 at 9:49

You can not have two default gateways. Choose either one.

If you are looking to use two ISP's simultanously then you need BGP.

share|improve this answer

Could this answer to another question also lead to your answer?

Setting up two NICs on two separate LANs provides error

share|improve this answer

Your Answer


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.