How to automatically connect to VPN with Network-Manager

I do have a working VPN connection. Now I want this VPN connection to automatically activate when I'm in a certain wireless network. Is there a non-intrusive graphical way using network-manager-applet? If I remember correctly it worked for a while and doesn't anymore.

I made the wireless-connection in question available for all users for not having to input the password every time.

The CLI program used is vpnc with Hybrid-Auth enabled (cisco VPN-client compatible) from sroecker's PPA

EDIT: I can' try the answers anymore (because of eduroam) and since no answer seems to gain a majority of votes I'm just gonna leave it unanswered for now until one is upvoted then I'm gonna accept that.


Solution 1:

The solution suggested by con-f-use should work but doesn't, due to a long-standing bug:

https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/280571

There are workarounds though. For recent NetworkManager versions, there is a commandline utitility, nmcli, that can be edited and saved as something like /etc/NetworkManager/dispatcher.d/vpn-up:

#! /bin/bash

REQUIRED_CONNECTION_NAME="<name-of-connection>"
VPN_CONNECTION_NAME="<name-of-vpn-connection>"


activ_con=$(nmcli con status | grep "${REQUIRED_CONNECTION_NAME}")
activ_vpn=$(nmcli con status | grep "${VPN_CONNECTION_NAME}")
if [ "${activ_con}" -a ! "${activ_vpn}" ];
then
    nmcli con up id "${VPN_CONNECTION_NAME}"
fi

[I haven't tested this -- please feel free to test and edit according to results]

See: https://wiki.archlinux.org/index.php/NetworkManager#Use_dispatcher_to_connect_to_a_vpn_after_a_network-connection_is_established has more info.

Solution 2:

You can find VPN auto connect option in the nm-connection-editor GUI in the individual saved Wi-Fi settings. In Ubuntu 14.04 you can find it under:

System settings -> Network -> check interested Wi-Fi or Lan -> Settings (General tab) -> select "Auto connect to VPN... "

Screenshot of GUI

Solution 3:

I was able to get this working on Ubuntu 20.04 by using nm-connection-editor. For whatever reason, the option doesn't appear anymore in neither the Wifi/Ethernet nor the VPN config UIs.

  1. Open a terminal

  2. Type nm-connection-editor

  3. Select the network connection you want to auto-connect

    Network connections selection window, settings button in red

  4. Click the gear icon to open settings for that connection

  5. Go to the General tab

  6. Enable the Automatically connect to VPN option and select the VPN in the dropdown menu to the right

    Network configuration window, general tab, auto-connect option in red

  7. Click Save

Solution 4:

If I remember correctly it worked for a while and doesn't anymore.

  • This might be obvious, but sometimes I don't think of it. Did you try reinstallation like:

    sudo apt-get purge network-manager-vpn sudo apt-get install network-manager-vpnc Then: enter image description here

Now I want this VPN connection to automatically activate when I'm in a certain wireless network.

  • There's a way to suppress the "couldn't connect"-type of messages when your users are in the wrong network, but can't remember how it went. I see no other way to automatically connect in a graphical way. Maybe you'll have to write a script doing the connecting when in said wlan-network.

Hope that helped.