Multiple default gateways

My home server has two interfaces named eth0 and eth1. eth0 is connected directly to an incoming WAN port, so the gateway for eth0 is determined depending on the ISP's DHCP server. eth1 is connected to my router, which is connected to another WAN port.

Below is the output of netstat -rn (111.111.111.126 is a mock public address):

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         111.111.111.126 0.0.0.0         UG        0 0          0 eth0
192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 eth1
111.111.111.126 0.0.0.0         255.255.255.128 U         0 0          0 eth0

I want to be able to choose an interface for the outgoing connections freely, but don't know how. curl www.google.com --interface eth1 should connect to www.google.com via eth1, it fails in the current configuration. route add default gw 192.168.0.1 fixes that, but then curl www.google.com --interface eth0 breaks.


Not sure of your aims, but sounds like a load balancing question. This page gives some guidance on setting up multiple routing tables to take advantage of multiple connections to the internet: Guide to IP Layer Network Administration with Linux, Section 10.4

Notice that this approach uses the Source IP Address to select outbound connection. This can still work on a single server system if you bind your server's sockets to specific interfaces. For example, Apache to the IP of eth0 and bittorrent to the IP of eth1. Not all servers will provide the configuration hooks required to do this, but many will -- even Minecraft! ;)


This is going to be a little tricky. You need to ensure that any single TCP stream comes out only via a single interface. Otherwise the other end of connection will have seen packets from different sources and this would break the connection.

Read these two pages:

http://kindlund.wordpress.com/2007/11/19/configuring-multiple-default-routes-in-linux/

and

http://generationip.com/documentation/network-documentation/93-howto-setup-multiple-default-gateway-on-linux

They should help.