Posts from 2020
Jan 3, 20204 minutesQuantum
Sometimes, you have multiple Internet connections, whether physical or virtual, and you want a few programs to access the Internet through one connection without making it the default gateway. For example, if you want a program to connect to the Internet through a VPN, but without forcing the entire system’s traffic through the VPN as well.
The traditional way to do this is with packet marking with
ip ruleto force marked packets through a different routing table to send the traffic to the correct destination. However, as the source IP was selected before routing, an
iptablesis required to change the source IP. This is ugly and clearly a hack.
However, since around 2013, Linux has introduced networking namespaces, which can be managed via
ip netnsas part of the
iproute2package. We can easily exploit this feature to achieve the desired goal with minimal fuss.