Linux – How to configure linux routing/filtering to send packets out one interface, over a bridge and into another interface on the same box

filteringiptableslinuxnetworking

I'm trying to test a ethernet bridging device. I have multiple ethernet ports on a linux box. I would like to send packets out one interface, say eth0 with IP 192.168.1.1, to another interface, say eth1 with IP 192.168.1.2, on the same subnet.

I realize that normally you don't configure two interfaces on the same subnet, and if you do the kernel routes directly to each interface, rather than over the wire. How can I override this behavior, so that traffic to 192.168.1.2 goes out the 192.168.1.1 interface, and visa-versa?

Thanks in advance!

Best Answer

Use network namespaces. It feels like running a VM but it's not a VM, just something that look like a separate IP stack.

ip netns add otherhost
ip netns exec otherhost /bin/bash

This will open a shell under the otherhost network namespace. If you examine the network configuration in it, you will see that there is no interface. It's like if you were running a different host.

Now, move the eth1 interface to the otherhost network namespace:

ip link set eth1 netns otherhost

Now, the otherhost namespace has your eth1 interface. Configure it like you would do if it were a separate host, and do the same for eth0 on your default network namespace. It's as simple as that.

Note that if you close all your shell to otherhost, the network namespace will disappear, and its interfaces will be moved back into the default network namespace.