The Best VPN Kill Switch For Linux Using Easy Firewall RulesCategory: misc
A 2 Minute Read
03 Feb 2017
Image By Matt Barber
I’ve written previously about the dangers of your VPN dropping and ways to prevent it from being disastrous. Indeed, while many VPN clients have drop protection built in, Linux users often are forced to use their built in Network Manager to connect to a VPN, which notably lacks drop protection. As a workaround, I recommended using a script called VPNDemon to act as a killswitch, so that when your VPN drops you don’t accidentally expose your IP address.
While this solution worked for me for quite some time, I had several readers write in and notify me that VPNDemon failed to work properly for them. Fortunately, there’s another simple solution that I have found since then that works flawlessly (so far at least). This time, instead of using a script to constantly monitor Network Manager, I’ve found a solution that uses Uncomplicated Fire-Wall’s (UFW) firewall rules to enforce a VPN connection.
The first step to getting this up and running is to install ufw. Ubuntu should have this installed by default, while on Debian you’ll have to type into the terminal:
sudo apt-get install ufw -y
Next, connect to your vpn and type the following into your terminal to ensure that your VPN connects to tun0 (look for tun0 as a network interface):
After you’ve ensured that your VPN is using tun0, disconnect from it, and copy and paste this into your favourite text editor, before saving it as your filename of choice (such as firewall.sh) in your home folder:
#!/bin/bash sudo ufw reset sudo ufw default deny incoming sudo ufw default deny outgoing sudo ufw allow out on tun0 from any to any sudo ufw enable
What this script does is reset all your ufw firewall rules, and then change them to only allow traffic to go in or out on tun0. Of course, you’ll eventually need to undo this. For that, you’ll want to copy and paste the following into a text editor as well, and save it in your home folder (I called mine unfirewall.sh):
#!/bin/bash sudo ufw reset sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw enable
This script once again resets your ufw firewall rules, and then sets them to a regular sane default (allow outgoing, but deny uninvited incoming traffic).
Now we need to make these two scripts executable. To do this, we will type the following into our terminal (assuming you have named your scripts the same as I have):
sudo chmod +x firewall.sh unfirewall.sh
To actually operate these, all you’ll need to do is connect to your VPN, and then execute the first script by typing into your terminal:
Once you’ve done this, no traffic will be allowed to enter or leave your computer that isn’t through the VPN interface. I recommend testing it though to make sure everything is set up correctly by disconnecting your VPN. If your internet doesn’t work, that’s a good sign.
Whenever you’re done with your VPN and want to be able to connect back to the regular internet, just execute the second script by typing into your terminal: