Iptables – Route a specific user’s traffic via VPN but still allow local networking

iptablesroutingvpn

So, I want to route certain traffic via a VPN connection and the rest via my normal Internet connection. I want to run several different programs and most of them don't support binding to a specific network interface (tun0 in my case).

I've managed to send a specific user's traffic via the VPN following the answers given here:
iptables – Target to route packet to specific interface?

But unfortunately, when I run a server that connects to the Internet and has a web interface running on a local IP (127.0.0.1/192.168.0.*), all the Internet traffic correctly goes via tun0, but I'm unable to connect to the web interface from a local IP as a different user.

When I log in as the VPN-ified user, I can access services running on local IPs, but other users/machines can't access any servers I start.

Can anyone point me in the right direction?

Best Answer

first add a firewall rules:

iptables -t mangle -A OUTPUT -m owner --uid USER -j MARK --set-mark 1
iptables -t nat -A POSTROUTING -m mark --mark 1 -j MASQUERADE

then add a routing rule:

ip rule add fwmark 0x1 table 100

and then add routes to your new routing table:

ip route add SOMEROUTE via SOMEGATEWAY table 100
Related Topic