This article is aimed at those who configured their VPN connection using the Network Manager applet, and by extension those who configured their VPN connection so that it is available in the nmcli command. It does not apply if you use gOpenVPN or GADMIN-OPENVPN-Client.
VPN connections become more and more popular, for a variety of reasons. If you are using a VPN connection for privacy or any other reasons, you probably fear to be disconnected while you are not in front of your computer, because this could leave your activity unencrypted on your ISP network. The Gnome Network Manager applet doesn’t offer a way to reconnect automatically. Fortunately, there is a solution. I will show you a little Bash script that adds this functionality.
Here is the script:
#!/bin/bash while [ "true" ] do VPNCON=$(nmcli con status) if [[ $VPNCON != *MyVPNConnectionName* ]]; then echo "Disconnected, trying to reconnect..." (sleep 1s && nmcli con up uuid df648abc-d8f7-4ce4-bdd6-3e12cdf0f494) else echo "Already connected !" fi sleep 30 done
- MyVPNConnectionName is the name of you VPN connection in the Network Manager applet
- df648abc-d8f7-4ce4-bdd6-3e12cdf0f494 is the uuid of your VPN connection. You can find it using the following command:
Save this script in your user personal directory, for example.
Do not forget to make this script executable by running the following command:
chmod +x /path/to/my/script/my_script
This script will check if you are connected to the VPN by testing if the $VPNCON variable contains the line corresponding to your VPN connection. If the $VPNCON variable doesn’t contain it, then the VPN is not connected and the script attempt to connect. Otherwise we just wait for 30 seconds (you can change this delay, but you should give enough time to fully reconnect).
Just save this script in your home directory and run it when you need.
How to run this script automatically when a user has logged in?
It’s very easy. Just append the following line to your .profile (in the root of your user directory):
In some distributions, it may be the .bashrc file. In Debian it’s .profile (it doesn’t work as expected with .bashrc). Just choose what works for you.
Finally, save your changes, logout from your session, and login again. The script should be automatically started, and your VPN should connect immediately. Hurray!
Q: It doesn’ t work with my Linux distribution! What can I do?
A: This script has been tested with Debian Squeeze (6.0.3) and works like a charm. Please refer to your distribution documentation.