How to keep VPN connection alive automatically on debian

In today’s world of relentless surveillance, censorship and raising authoritarianism, using VPN –while it is still legal– should be a routine practice.

I wrote a script to keep my VPN connection alive. This script runs when you log in and checks every 20 seconds whether your VPN is connected or not. If it finds out that your VPN is not connected it tries to establish the connection. It works silently in the background.

Here are the steps I have taken to keep my VPN almost always connected on debian:

  1. Create a VPN connection and connect to it using Gnome panel (network-manager-gnome) or in any other way possible (I will not go into detail of this here).
  2. In Terminal: nmcli c show and note UUID of your VPN connection
  3. In Terminal: mkdir ~/Scripts ; gedit ~/Scripts/autovpn.sh
  4. Copy & paste the following script:
  5. Close your text editor and save the file.
  6. In Terminal: chmod +x /home/yourusername/Scripts/autovpn.sh
  7. In Terminal: gedit ~/.config/autostart/autovpn.desktop
  8. Copy & paste the following (do not forget to change ‘yourusername’ in Line 5):
  9. Close your text editor and save the file.
  10. Log out and log in or reboot your computer to see if it works.

Keep in mind that this script does not prevent any leak via your unsafe (not VPN) connections. When your VPN disconnects even for a short moment your software may access the internet via unsafe connections during that short period of time. IPTABLES may be used to prevent this. I will talk about that in another post. Here is my post about how to do that simply and effectively.

Lastly, you should check if there is a DNS leak, while your VPN is active. dnsleaktest.com can be used for this purpose. If you use OpenVPN and Network Manager (default networking interface of Ubuntu) and you have DNS leak, check this post.

This is tested on debian 8.3 but it should also work on its derivatives like Ubuntu and Linux Mint.

Leave a Reply