When configuring two interfaces, each on a different subnet, you must add an additional routing table to isolate interface traffic (e.g. eth0 in, eth0 out).

1. Create a routing table.

In our example we create a table named ‘secondary’ with identifier 252:

  $ echo '252 secondary' >> /etc/iproute2/rt_tables

If you check out /etc/iproute2/rt_tables you’ll see there are a few reserved identifiers one of which is for the primary routing table, main:

# reserved values
255	local
254	main
253	default
0	unspec

2. Add rules so the Linux kernel will use our new table:

$ ip rule add to table secondary
$ ip rule add from table secondary

$ ip rule list  
0:	from all lookup local 
32764:	from lookup secondary 
32765:	from all to lookup secondary 
32766:	from all lookup main 
32767:	from all lookup default 

3. Add an entry to the new routing table mapping the subnet traffic to the correct interface:

$ ip route add dev eth0 src table secondary

4. Add an entry specifying the gateway of the subnet:

$ ip route default via dev eth0 table secondary

That is it, now traffic on will be routed through the secondary table, and we can take a look at our routes:

$ ip route list table secondary
default via dev eth0 dev eth0  scope link  src

In Ubuntu, if you want this configuration to persist across boots you’ll need to add it your your /etc/network/interfaces config:

auto eth0
iface eth0 inet static
up ip rule add to lookup secondary
up ip rule add from lookup secondary
up ip route add dev eth0 src table secondary
up ip route add default via dev eth0 table secondary