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

Example with three network cards

You will also need the following in /etc/sysctl.conf: