How can I have my VPN connect automatically when the wireless connects?

When setting up a VPN connection through Network Manager selecting the Connect automatically option should mean that the VPN does automatically connect, however this isn't working due to a bug: https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/280571

It can still achieved though through workarounds, one way of doing this would be to use vpnautoconnect

  • Set up your VPN as normal through the network settings, making sure that Connect automatically is selected:

enter image description here

  • Download the appropriate .deb file from the download page: http://sourceforge.net/projects/vpnautoconnect/files/ (ending in amd64 for 64bit, ending in i386 for 32bit).
  • Double click the downloaded .deb file and install it.
  • Run the VPNautoconnect application

enter image description here

  • Select Preferences from the VPNautoconnect menu

enter image description here

  • Click the small arrow to create a new tab and select your wireless connection in the Parent Connection drop down menu, and your VPN in the VPN Connection drop down menu. Then click Save

enter image description here

enter image description here

Your VPN should now connect automatically whenever the selected wireless network is connected (if you use more than one wireless network you'll need to create a new tab in the preferences menu for each one you want to auto connect to a VPN whilst you are using).


In Ubuntu Gnome 16.04, I can't find in Network Connections the GUI option to edit per-network VPN settings. The solution for me was to launch the "Network Connections" panel manually in the terminal by typing:

nm-connection-editor

then edit the desired network and add a VPN connection to it.


On 18.04, and some previous version also, there is not even a GUI settings to automatically enable a VPN connection now.

So instead, I enable automatic connection to ethernet and then setup the VPN connection with an autostart config file and script.

Add a vpn.desktop file in ~/.config/autostart with the following content

[Desktop Entry]
Name=Start VPN connection
GenericName=Network helper
Comment=Start automatically VPN connection
Exec=/home/me/bin/vpn-start.sh
Terminal=false
Type=Application
Icon=network-vpn
Categories=Network;
StartupNotify=false
X-GNOME-Autostart-enabled=true

This will call a file that you can put everywhere, but I have put in in /home/me/bin/ and call it vpn-start.sh

#/bin/bash
LANG=C
while ! nmcli dev status|grep -q connected; do
    sleep 1
done
exec nmcli con up '<name of your vpn connection>'

You need to put the name of your VPN connection. Look for it in the output of nmcli con show.

Make it executable with chmox +x vpn-start.sh.

And logout-relogin to test for it.