Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 two ethernet interfaces in my computer, which is running Ubuntu 9.04. Both interfaces sport static IPs, but use separate gateways. My /etc/network/interfaces file looks something like this:

auto eth0 eth1
iface eth0 inet static

iface eth1 inet static

I want to have all traffic going to the internet-at-large run through eth0, but it seems to want to go through eth1. Is there a way that I can channel my general outbound traffic through eth0 instead and only use eth1 for traffic to its subnet?

The answer should be persistent; that is to say, it should survive reboot without a superuser needing to run a command after restart.

EDIT: as requested, here is the output of my route -n command:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface   U     0      0        0 eth0   U     0      0        0 eth1     U     1000   0        0 eth1         UG    100    0        0 eth1         UG    100    0        0 eth0
share|improve this question
I suggest you to add the output of "route" command to your question. – Anonymous Jul 15 '09 at 14:49
up vote 11 down vote accepted

You should only have one default gateway. If you remove the gateway line from eth1, it'll all just work (after networking is restarted).

share|improve this answer
Manual routing isn't needed here. – theotherreceive Jul 15 '09 at 15:01
Depends. The OP hasn't specified whether his eth1 has networks other than the local one connected to it. The fact that he's bothered specifying a gateway would make me assume he has. – Cian Jul 15 '09 at 15:04
Question clearly states that eth1 should only be used for traffic within it's own subnet. – theotherreceive Jul 15 '09 at 15:06
You're right. Edited to reflect that. – Cian Jul 15 '09 at 15:11

Remove gateway from eth1 in /etc/network/interfaces and add it as a static route:

route add -net netmask gw

With this, will be your default gateway and eth1 will be used only to connect to network

UPDATE: The route command is superfluous. You only need to remove the gateway from eth1.

share|improve this answer
That route command looks superfluous. – Dan Carley Jul 15 '09 at 14:58
You're right, is not necessary. My fault. – HD. Jul 15 '09 at 15:02
It is superfluous. Interfaces will automatically route to directly connected networks. – Cian Jul 15 '09 at 15:02
And of course - if you think about what it would do - you don't want to static route all of a direct attached subnet to one host on the same subnet. – Dan Carley Jul 15 '09 at 15:04

Look at your current routing table by typing in route. Your machine will use the first default gw it sees in the routing table. This will be denoted by default or as the destination. If you only want eth1 to be used for the local network, remove the gateway command. This should make eth1's default gw your only default one.

share|improve this answer

Modifing the routing table with route as suggested will fix this temporarily, but it will revert to it's current state on the next boot.

It would be possible to set route rules in /etc/rc.local or some kind of other init script, but in this case the simplest option is just to remove the gateway from eth1 in the interfaces file.

By removing that gateway that system will still automatically use eth1 as the route 192.168.2/24, but will route all other traffic through eth0.

share|improve this answer
If you need to specify manual routes, I'd rather do it through post-/up interface-options and either running ip route directly or through a script, and possibly have a pre-/down interface-option to remove those routes again. This interfaces better with ifup/ifdown. – Kjetil Joergensen Jul 15 '09 at 15:11
@Kjetil agreed, that is a nicer solution. – theotherreceive Jul 15 '09 at 15:15

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.