Routing with multiple network cards

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

Once I needed to setup routing on a Linux machine with multiple (two, three, or more) network cards, but all connected to the same switch, and all in the same subnet. Some aspects of such a setup might remind bonding, but it's not.

Normally, in such setup, if you established a connection with eth1, you would get replies from eth0. As a consequence, if you disconnected eth0 cable, your machine would not be able to communicate.

These instructions below will cause that each IP address/network card will communicate using its own interface: connections to eth0 will use eth0 network card, connections to eth1 will use eth1 network card, and so on.

Similarly, you may need a similar trick if your hosting provider requires you to strictly use a defined MAC address for traffic from a given IP address - one of such providers is Hetzner, and these instructions will help you when you use virtualization and multiple IPs in the same container / virtual machine.

Make sure to run this when your system boots (i.e. in /etc/rc.local or a dedicated startup script).


Example with two network cards[edit]

# This server is "special", as it has two IP addresses from the same subnet
# using different NICs with different MACs
route add default gw 148.251.121.161 dev eth3
route add default gw 148.251.121.161 dev eth0

ip route add 148.251.121.160/27 dev eth0 table 2
ip route add 0/0 via 148.251.121.161 dev eth0 table 2
ip rule add from 148.251.121.185 table 2
ip rule add to 148.251.121.185 table 2

ip route add 148.251.121.160/27 dev eth3 table 3
ip route add 0/0 via 148.251.121.161 dev eth3 table 3
ip rule add from 148.251.121.184 table 3
ip rule add to 148.251.121.184 table 3

Example with three network cards[edit]

route add default gw 192.168.111.65 dev eth2
route add default gw 192.168.111.65 dev eth1
route add default gw 192.168.111.65 dev eth0
 
ip route add 192.168.111.64/26 dev eth0 table 2
ip route add 0/0 via 192.168.111.65 dev eth0 table 2
ip rule add from 192.168.111.100 table 2
ip rule add to 192.168.111.100 table 2
 
ip route add 192.168.111.64/26 dev eth1 table 3
ip route add 0/0 via 192.168.111.65 dev eth1 table 3
ip rule add from 192.168.111.98 table 3
ip rule add to 192.168.111.98 table 3
 
ip route add 192.168.111.64/26 dev eth2 table 4
ip route add 0/0 via 192.168.111.65 dev eth2 table 4
ip rule add from 192.168.111.97 table 4
ip rule add to 192.168.111.97 table 4