How to automatically connect to VPN with Network-Manager

74,333

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/title/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 -> Wi-Fi or Lan -> Settings (General tab) -> Auto connect to VPN...

(Since Gnome 42, this path leads to a new simplified menu, but the complete one can still be accessed through nm-connection-editor directly)

Screenshot of GUI

After saving, a secondaries= line is added in your configuration file in the [connection] section. It will contain a list of secondary connection UUIDs to be activated. The configuration file is usually /etc/NetworkManager/system-connections/.

This information can be useful if you need to activate more than one secondary connection. You can add multiple connection UUIDs in the configuration file separated by ;, even though the GUI (at the moment: 2022) does not allow to select more than one entry.

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

I can confirm this following solution working for me. I am running Ubuntu 14.04 LTS.

I went to the shutdown icon and then system settings.

When the settings screen opens, I went to 'Network' and the WiFi network with which I was connected. There is a small arrow one one side of the name of the network.

Settings screen shot, its in Arabic

Click that icon and you should see an option called "settings" or "options" (or something similar) here:

Click settings

When the next window opens, simply go to the 'General' tab and you should see an option that gives you an option to Automatically connect to a VPN. Choose the VPN You'd like to connect when connected to this WiFi, and boom! Everytime you connect to this network, Ubuntu will also connect you to that VPN. :)

For me its working. I hope it works for everyone else too. Doesn't require any sort of text editor or code editor or anything..

Solution 5

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.

Share:
74,333

Related videos on Youtube

David Guo
Author by

David Guo

Updated on September 18, 2022

Comments

  • David Guo
    David Guo over 1 year

    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.

    • rlemon
      rlemon about 12 years
      how are you coming along with this?
    • David Guo
      David Guo about 12 years
      I didn't need it for quite a long time now. Maybe come april on new university.
    • nilsonneto
      nilsonneto almost 12 years
      Possibel duplicate? askubuntu.com/questions/86365
    • David Guo
      David Guo almost 12 years
      @fossfreedom not a duplicate exactly because my question is about vpnc rather than openconnect.
    • Stefan van der Walt
      Stefan van der Walt over 9 years
      I wrote a short block post on how to block your internet connection unless the VPN is up and running: mentat.za.net/blog/2015/01/24/vpn-only-internet
  • David Guo
    David Guo almost 13 years
    I still haven't come around to actually testing this. I'll mark it answered when I will. Promise.
  • None
    None almost 13 years
    in my experience this doesn't actually work. I believe this to be a bug in NetworkManager
  • eggonlegs
    eggonlegs over 11 years
    Yep this is broken, even in Precise. It doesn't connect automatically O_o
  • gertvdijk
    gertvdijk over 11 years
    I think this approach is less elegant, due to a background process being active for this all the time, while it could be event-driven like in the answer of @hugoheden.
  • iElectric
    iElectric over 11 years
    It does more than his answer: it also handles VPN reconnect and cases where user manually disables vpn connection (meaning it shouldn't try to reconnect VPN in that case)
  • Admin
    Admin almost 11 years
    The Arch wiki hack does work in fact! I tried the script on the arch wiki on july 11th 2013, and modifying it from 'nmcli ...' to 'su [user] -c "nmcli..."' does not work! follow instructions as in the wiki and don't forget the chmod +x ;) cheers!
  • iGadget
    iGadget over 10 years
    The bug mentioned by Hugo Eden (bugs.launchpad.net/ubuntu/+source/network-manager/+bug/2805‌​71) has been fixed upstream over 5 months ago. However, I'm still experiencing it in Precise... What gives?
  • user48956
    user48956 about 10 years
    Yup -- broken. Does not work (Ubunutu 12.04).
  • fosslinux
    fosslinux almost 8 years
    The OP is asking about Ubuntu.
  • jw00druff
    jw00druff almost 8 years
    Kali and Ubuntu are both Debian based so the solution is often the same. I just went ahead and tested this on an Ubuntu 15.10 instance and the nmcli error was the same as I'd mentioned above. The solution worked just the same as well. The nmcli version installed on the 15.10 box is 1.0.4.
  • Ramon Melo
    Ramon Melo over 3 years
    Only option that worked for me. On a side note, if you know its name, you can just grep it without having to connect it first by dropping the --active.
  • Valentin Bajrami
    Valentin Bajrami over 2 years
    Such a great answer! Thank you midopa!
  • B T
    B T over 2 years
    This is exactly what I needed! Very unintuitive tho. I saw that the option was greyed out in my VPN connection config, and I didn't think it might be an option for connecting to a different connection profile.