This is a quick guide for setting up a killswitch using UFW (Uncomplicated FireWall). It is assumed you are using OpenVPN and optionally Network-Manager with network-manager-openvpn.
Before we can start we're going to need the IP address (or the IP addresses) of your VPN so that we can whitelist those later on, write them down. They are obviously going to be different for every VPN and VPNs with multiple servers, so I'll leave this up to you.
On some systems UFW is installed and enabled by default (Ubuntu, for example). Installation procedure is going to be different for every distribution of GNU/Linux, but once you've got it installed enabling it is easy (assuming you have sudo):
sudo ufw enable
Block all outgoing traffic:
sudo ufw default deny outgoing
And also block all incoming traffic:
sudo ufw default deny incoming
It is assumed you are using TUN as a network adapter (if you're unsure you most definitely are). Allow outgoing traffic on tun0:
sudo ufw allow out on tun0 from any to any
And optionally allow incoming traffic on tun0 (if you're a seeder, for example):
sudo ufw allow in on tun0 from any to any
At this point you're technically done, but with this setup you would need to disable UFW every time OpenVPN needed to connect to your VPN and then re-enable UFW when it has connected. Instead of doing that you could add the IP addresses mentioned earlier as exceptions to UFW.
To add a single IP:
sudo ufw allow out from any to 123.123.123.123
To add a range, us a mask:
sudo ufw allow out from any to 123.123.123.0/24
sudo ufw status
Congratulations, you've configured a VPN Killswitch on your GNU/Linux system!
Thanks for this! The above won't work for me unless I enable outgoing as default. Following your instructions to the 'T' didn't seem to work for me. My rules:
To Action From
Anywhere on tun0 ALLOW Anywhere
Anywhere (v6) on tun0 ALLOW Anywhere (v6)
Anywhere ALLOW OUT Anywhere on tun0
198.144.157.54 ALLOW OUT Anywhere
199.189.26.122 ALLOW OUT Anywhere
159.203.4.110 ALLOW OUT Anywhere
184.75.223.90 ALLOW OUT Anywhere
Anywhere (v6) ALLOW OUT Anywhere (v6) on tun0