setup multi default gateway under linux

If you need to configure a linux in different network with different gteway this howto is for you.

I present you a solution to do the multi default gateway under linux based on iproute2 commande line.

Schema example :

Step 1 :

Creation of the table definition in iproute2:

root# cat /etc/iproute2/rt_tables

# reserved values
#
255 local
254 main
253 default
0 unspec

200 t1
201 t2
#
# local
##1 inr.ruhep

In this file a i have add 2 entries :

200 t1
201 t2

Now we have to define the routing workflow for the different interface and network.

ip route add 10.2.0.0 dev eth0 src 10.2.0.100 table t1
ip route add 10.1.0.0 dev eth1 src 10.1.0.10 table t2

We have to define the default route on the routing table:

ip route add default via 10.2.0.1 table t1 ip route add default via 10.1.0.1 table t2

This part allow the routing to the direct neigbor over the good interface :

ip route add 10.2.0.0 dev eth0 src 10.2.0.1
ip route add 10.1.0.0 dev eth1 src 10.1.0.1

This part define the preference on the default gateway :

ip route add default via 10.2.0.1

Now you have to configure the routing rules :

ip rule add from 10.1.0.10 table t1 ip rule add from 10.2.0.100 table t2

If you have more than 1 ip addres on the interface you have to make a rule also on it

ip rule add from 10.1.0.11 table t1

The last section is important to allow the incomming on each interface

ip route add 127.0.0.0/8 dev lo table t1
ip route add 127.0.0.0/8 dev lo table t2
ip route add 10.2.0.0 dev eth0 table t2
ip route add 10.1.0.0 dev eth1 table t1

 

Now we have a configuration for your 2 ISP, but if you want to use a local network, you have to add it like in the last section present above.

Tips and tricks

If you want to balance the outgoing traffic between those interfaces, you have to applie a load balancing rules like that :

ip route add default scope global nexthop via 10.2.0.1 dev eth0 weight 1 nexthop via 10.1.0.0 dev eth1 weight 1

If you lose the parametter after each reboot, you can see to put all this in the rc.local file.

 

 

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