How to automatically connect to VPN with Network-Manager
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)
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.
-
Open a terminal
-
Type
nm-connection-editor
-
Select the network connection you want to auto-connect
-
Click the gear icon to open settings for that connection
-
Go to the General tab
-
Enable the
Automatically connect to VPN
option and select the VPN in the dropdown menu to the right -
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.
Click that icon and you should see an option called "settings" or "options" (or something similar) here:
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:
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.
Related videos on Youtube
David Guo
Updated on September 18, 2022Comments
-
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 about 12 yearshow are you coming along with this?
-
David Guo about 12 yearsI didn't need it for quite a long time now. Maybe come april on new university.
-
nilsonneto almost 12 yearsPossibel duplicate? askubuntu.com/questions/86365
-
David Guo almost 12 years@fossfreedom not a duplicate exactly because my question is about vpnc rather than openconnect.
-
Stefan van der Walt over 9 yearsI 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 almost 13 yearsI still haven't come around to actually testing this. I'll mark it answered when I will. Promise.
-
None almost 13 yearsin my experience this doesn't actually work. I believe this to be a bug in NetworkManager
-
eggonlegs over 11 yearsYep this is broken, even in Precise. It doesn't connect automatically O_o
-
gertvdijk over 11 yearsI 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 over 11 yearsIt 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 almost 11 yearsThe 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 over 10 yearsThe bug mentioned by Hugo Eden (bugs.launchpad.net/ubuntu/+source/network-manager/+bug/280571) has been fixed upstream over 5 months ago. However, I'm still experiencing it in Precise... What gives?
-
user48956 about 10 yearsYup -- broken. Does not work (Ubunutu 12.04).
-
fosslinux almost 8 yearsThe OP is asking about Ubuntu.
-
jw00druff almost 8 yearsKali 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 over 3 yearsOnly 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 over 2 yearsSuch a great answer! Thank you midopa!
-
B T over 2 yearsThis 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.