There are several possibilities, depending on how you want to decide what packets go where. Most of them will require some understanding of how TCP/IP networking works in Linux. The main tools you'll have to know to do complex things are iptables
(Ubuntu: iptables
) and iproute2 (ip
command) (Ubuntu: iproute
, iproute-doc
).
If you can discriminate fully by target IP address, it's simple: route the IP addresses according to your wishes. For example, the following commands will cause all packets for 1.2.3.x and 1.2.4.2 to go via ppp0
, and other packets to go via eth0
.
route add -net 1.2.3.4/24 ppp0
route add -host 1.2.4.2 ppp0
route add -net 0.0.0.0/0 eth0
For more complex requirements, you need to start using iptables
and ip route
. For example, the following commands set up special routing tables so that all packets marked 1 go out via eth0
and all packets marked 2 go out via ppp0
(except that packets intended for localhost
stick to the loopback interface).
ip rule add fwmark 1 table 1
ip route add 127.0.0.0/0 table 1 dev lo
ip route add 0.0.0.0/0 table 1 dev eth0
ip rule add fwmark 2 table 2
ip route add 127.0.0.0/8 table 2 dev lo
ip route add 0.0.0.0/0 table 2 dev ppp0
Now you can use iptables
to “mangle” outgoing packets, adding a mark that will decide what route they take. For example, here's how to send all outgoing SMTP traffic (port 25) via eth0
, and all traffic originated by an application running as the user proxy
via ppp0
.
iptables -t mangle -A OUTPUT -p tcp --dport 25 -j MARK --set-mark 1
iptables -t mangle -A OUTPUT -m owner --uid-owner proxy -j MARK --set-mark 2
See also 2 network interfaces connected to internet. Choose the one to use according to the domain name and bind software to different network interfaces.
You'll need to arrange for these commands to run when both interfaces are connected. I recommend that you write a script called /etc/network/if-up.d/0justin-routes
that runs the commands you want. This script will be executed whenever a network interface is brought up; as its name begins with a 0
it will run early in that process, before application-specific setup that might expect the routes to be in place. There is a symmetric /etc/network/if-down.d/
in case you also want to do things when one of the interfaces comes down. (All associated routes will automatically be erased, which may leave some packets stranded when you'd like them to fall back to the other interface.)
The ifup scripts are documented in the interfaces(5) man page
. The main thing to know is that the name of the interface being brought up or down is in the environement variable IFACE
. You can find out whether the other interface is already up with if ifconfig | sed 's/ .*//' | grep -Fqx 'eth0'; then …
.