I have a linux VM running a VPN client and I want to make sure all new, outbound connections to the internet go over the VPN tunnel, while also allowing local network devices to connect to it. This VM connects to the internet, and it also has to talk to 2 other local VMs. If the VPN goes down, so does the VM's internet connection (but local network still allows connections so I can SSH in and restart services).
OpenVPN creates a tun0
interface and, as I understand it, this is just a virtual adapter that runs over UDP port 1198 via my VM NIC, ens160
.
I can get by adding and removing firewall filter rules (make sure dns/ssh/icmp is allowed), but I'm not good enough with networking to design this myself correctly.
- What would be the best way to accomplish this with iptables?
- Would I use the nat table at all, or just filter would be able to accomplish this?
- Do I target outbound connections using
tun0
, orens160
over port 1198? or both? - Is is possible to have the source of the connection be a given service/application?
- Do I also need to enable split tunneling so the LAN connections will work? How?
- Is there anything other than iptables that would be a better solution, in 2022?
I'm usually pretty good at figuring things out on my own, I just need to be pointed in the right direction. Please give explanations or links to helpful docs. Even some keywords I can search for: I couldn't find anything online that was related to my specific needs.
https://serverfault.com/a/403943/976907 This answer is close to what I need, but I don't have a static private IP for my VPN server, the OpenVPN connection is set to a domain name and a port. Also they mention a caveat about routes falling back to default, and my lack of confidence in things networking makes me want more info.
https://community.openvpn.net/openvpn/wiki/IgnoreRedirectGateway I found this which might be related but I'm not 100% on what a redirect-gateway is and couldn't find a definition that made sense to me.