For the security and privacy conscious, a trusted VPN is a great tool that provides encrypted communications and assured privacy while accessing unsecured networks (the internet in particular).
I use several VPNs, I have my own private OpenVPN server which I run on my main (personal) server, and also use a well known VPN service. I do use both (not at the same time) for various purposes and for different use cases which won't be discussed here.
One of the important reasons for using a VPN is to obfuscate (hide) your ip address. It's important to note that even the best VPN setups are subject to occasional drops and disconnects. When this occurs, your system will likely fallback to using its standard network interfaces (which it would use without a VPN) and incidentally expose your actual ip address. In sensitive environments (or countries) this can potentially lead to some very undesirable situations (including being able to find your actual location).
Enter a Kill Switch. A kill switch is basically some system which ensures your ip address is not leaked, and ideally blocks all traffic that is not through the VPN. Hence if your VPN connection drops, your system doesn't fall back to standard network interfaces.
Many VPN services provide built in Kill Switch functionality. However, I generally prefer to roll my own, with an implementation that I trust (because I did it).
Requirements and setup
This implementation doesn't require much. Basically you need
- sudo access
- ufw (uncomplicated firewall)
All Linux distros should have these (mostly likely by default).
Make sure UFW is setup and installed. For distros that use systemd, you would simply do
My killswitch consists of a single bash script. This script supports both starting the killswitch and stopping the killswitch (which resets the firewall), plus another option to open an outgoing route to a specific CIDR:
You'll need to make the script executable and ideally put it in some folder on your path. To make them executable,
cd into whatever folder contains the scripts and:
Basic usage is to simply call the script with a valid option. The supported options are:
Note that the
-t option will likely be needed if you also run a web server (for example) on the same server that is running your VPN and want to access web server resources.
Turn on (enable) the killswitch
Turn on (enable) the killswitch and allow outgoing route to to some server, e.g. 184.108.40.206:
Turn off (disable) the killswitch
Integrating killswitch into OpenVPN client ovpn
If you (or your VPN provider) uses OpenVPN you can integrate the killswitch script into your client
.ovpn file so that when you connect the killswitch script is automatically run. This comes in handy especially if you run your own OpenVPN server (which I do) - and also use that server for other things (such as a web server etc.). In these types of use cases you can also open an outgoing route to your server (for example to access server web resources while also being connected to said server via OpenVPN).
You can simply add a
route-up directive with the
vpnkillswitch command. For example, in my client
.ovpn I simply added:
<IP-ADDRESS-OF-OPENVPN-SERVER> with the IP address of the outgoing route you want to allow.
route-up here as OpenVPN will execute the script after routes to the VPN server has been added. This is after any
up commands have been run (which you might use in your client config to set DNS addresses etc.).
Don't forget that you'll need to disable the kill switch after disconnecting with from OpenVPN. You can do this by executing: