How to force Ubuntu to access the Internet ONLY through a VPN and disable it when disconnected?

Due to privacy issues I'd like Ubuntu to allow applications to send traffic to the Internet only when the VPN connection is established.

The thing is that there are some applications like Skype or Dropbox that will reconnect after the VPN connection is dropped for some reason. And I don't want that, I want them not to be able to.

Any way to do this?


Solution 1:

Try this:

To get your default gateway IP, show your routes with the ip route list command.

Edit your main internet connection to remove the default route: in nm-connection-editor, edit your connection > IPv4 tab > Routes > check Use this connection only for resources on its network.

Then add a specific route to reach your VPN server via your gateway. This will allow your computer to reach only one IP on the internet: your VPN server, so that your VPN connection will still work. (in the Routes window, add a route like: Adress: VPN_SERVER_IP Netmask: 255.255.255.255 Gateway:GATEWAY_IP).

To allow internet access through your VPN, you may also need to edit the Routes of your VPN connection to uncheck "Use this connection only for resources on its network", or add a specific route for a custom gateway of your VPN.

nm-connection-editor edit routes screen