Routing with two default gateways

From lxadm | Linux administration tips, tutorials, HOWTOs and articles
Jump to: navigation, search

If you have two network interfaces, Linux will reply on the first interface (i.e. eth0), even if the packets came from eth1.

Assuming you have two default gateways like below and your public IP is 100.200.300.400 (such address in not possible, it's just for the purpose of this document) on eth1:

# ip route
default via 10.178.171.1 dev eth0 proto static 
default via 100.200.300.400 dev eth1 proto dhcp src 100.200.300.1 metric 100 


Fix:

ip rule add from 100.200.300.1 table public
ip route add default via 100.200.300.400 dev eth1 table public