Strange routing issue, solvedStrange routing issue, solved

September 29, 2010

I just encountered this problem: I have an Ubuntu box, on which ran this downloading service. Because some complaints from the Internet provider, I decided to use another network (from another provider) for this downloading service. Everything else needed to remain the same.

So I installed a new network card on the system, and it became eth1. The first one, eth0, was connected to network 192.168.2.0. Now, I connected eth1 to network 192.168.1.0. Both networks provides DHCP.

The first issue was when I configured both NICs with DHCP. No outside access was available on the box because there were two default gateways. So I configured eth1 as static, and didn’t specify any default gateway for it. But this rendered the internet connection through eth1 useless, because all the traffic (for the outside world) was routed through eth0 now. So when the downloading service was bound to eth1, it couldn’t connect anywhere outside the network.

After some searching effort on the Internet, I found this solution:

/etc/network/interfaces:


# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp

# The secondary network interface
auto eth1
iface eth1 inet static
address 192.168.1.2
netmask 255.255.255.0

up /etc/network/prioritize_networks.sh

/etc/network/prioritize_networks.sh:


#!/bin/sh

#Set up the first subnet's routing table (we'll name it 70)
ip route flush table 70
ip route add table 70 to 192.168.2.0/24 dev eth0
ip route add table 70 to default via 192.168.2.1 dev eth0

#Set up the second subnet's routing table (we'll call it 80)
ip route flush table 80
ip route add table 80 to 192.168.1.0/24 dev eth1
ip route add table 80 to default via 192.168.1.1 dev eth1

#Create the rules to choose what table to use. Choose based on source IP
#We need to give the rules different priorities; for convenience name priority
#after the table
ip rule add from 192.168.2.0/24 table 70 priority 70
ip rule add from 192.168.1.0/24 table 80 priority 80

#Flush the cache to make effective
ip route flush cache

So now all the traffic goes by default through eth0, but all applications bound to eth1 are routed through the correct gateway of 192.168.1.0 network.

I found this hack here: http://www.physics.umd.edu/pnce/pcs-docs/Glue/linux-route-hack.html

Hope this helps anybody.

Be Sociable, Share!

Leave a Reply




*