8

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
  address 10.0.0.5
  netmask 255.255.255.0
  gateway 10.0.0.1

iface eth1 inet static
  address 192.168.2.5
  netmask 255.255.255.0
  gateway 192.168.2.1

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
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth1
0.0.0.0         192.168.2.1     0.0.0.0         UG    100    0        0 eth1
0.0.0.0         10.0.0.1        0.0.0.0         UG    100    0        0 eth0
Marcus Griep
  • 245
  • 1
  • 5
  • 8

5 Answers5

12

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

Cian
  • 5,838
  • 1
  • 28
  • 40
4

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

route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1

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

hdanniel
  • 4,293
  • 23
  • 25
2

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.

theotherreceive
  • 8,365
  • 1
  • 31
  • 44
  • 2
    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
1

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 0.0.0.0 as the destination. If you only want eth1 to be used for the local 192.168.2.0/24 network, remove the gateway command. This should make eth1's default gw your only default one.

Francois Wolmarans
  • 1,590
  • 10
  • 14
1

I've been using this solution for 4+ years, have published it on the Linode wiki, and it's been working flawlessly. Edit /etc/network/interfaces and make it match this:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
    address <the outbound IP>
    netmask <the netmask for the outbound IP, usually 255.255.255.0>
    gateway <the gateway for the outbound IP>
    up /sbin/ip addr add <the primary IP>/24 dev eth0
Dan Dascalescu
  • 601
  • 1
  • 10
  • 21