Ubuntu Tip:How to Connect/disconnect VPN from the command line
This tutorial will explain How to Connect/disconnect VPN from the command line
If you want to interact with NetworkManager from the command line you can use the "nmcli" command.
You can list all NM connections using the following command
You can start connection (wifi, vpn, etc) using the following command
nmcli con up id ConnectionName
You can disconnect the connection using the following command
nmcli con down id ConnectionName
Also note that regular users usually don't have permission to control networking. Using the commands above with sudo should work for most connections, but VPN specifically might fail with the following error
"Error: Connection activation failed: no valid VPN secrets."
Reason for this error
For most connection types, the default handling is that NM stores passwords itself. However, for certain connections,e.g. VPN, the passwords are rather
regarded as personal and thus they are stored by default in clients (e.g.keyring).
So, if you have configured my-openvpn connection for a user, but activate that under root, the password in user's keyring is not accessible.
To fix this, edit /etc/NetworkManager/system-connections/ConnectionName and under [vpn], change the password flags line to:
And add the following:
Then starting the VPN connection with sudo nmcli con up id ConnectionName should work without problems.
Thanks to Mark for this tip