Routing with multiple network cards
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
# This server is "special", as it has two IP addresses from the same subnet # using different NICs with different MACs route add default gw 126.96.36.199 dev eth3 route add default gw 188.8.131.52 dev eth0 ip route add 184.108.40.206/27 dev eth0 table 2 ip route add 0/0 via 220.127.116.11 dev eth0 table 2 ip rule add from 18.104.22.168 table 2 ip rule add to 22.214.171.124 table 2 ip route add 126.96.36.199/27 dev eth3 table 3 ip route add 0/0 via 188.8.131.52 dev eth3 table 3 ip rule add from 184.108.40.206 table 3 ip rule add to 220.127.116.11 table 3
Example with three network cards
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
You will also need the following in /etc/sysctl.conf: