21

One can import .ovpn files to NetworkManager through GUI tool nm-connection-editor (right click on nm-applet and click on Edit connections): Add -> Import a saved VPN configuration....

My goal is to do same thing but via command line using tools like nm-cli. Is it possible somehow?

4
  • Did you make any progress with this? I am trying to do the same thing so will post an answer if I get it working. Oct 13, 2014 at 7:57
  • @DavidMason, unfortunately, no. I'm waiting for your solution.
    – petRUShka
    Oct 13, 2014 at 12:11
  • I'm looking actively for for a solution too.
    – Raspbeguy
    Sep 18, 2015 at 14:01
  • I'd like to see a solution for importing by command line Mar 15, 2017 at 18:16

3 Answers 3

21

I'm using nmcli version 1.2.6 and I can use it to import openvpn configs.

nmcli connection import type openvpn file ~/myconfig.ovpn

From now on even in NetworkManager UI my vpn is visible.

To further configure the connection you can use the showcommand to find out the connection name and modify to change config values.

nmcli connection show
nmcli connection modify myvpnconnectionname +vpn.data username=myusername
3
  • 1
    any idea how to use -vpn.data if I accidentally added something wrong to the configuration data?
    – 32r34wgf3e
    Aug 4, 2018 at 7:13
  • @32r34wgf3e don't know. When my configuration is wrong I delete it and do a fresh set up from the command line as outlined in my answer. Tip: Store your commands in a text file for later reuse.
    – bastian
    Aug 6, 2018 at 13:45
  • 1
    You just pointed me to the right direction with "modify", thank you sir. Dec 11, 2020 at 14:59
6

A workaround I use to get past the lack of functionality that comes with nmcli on debian-based systems is to use commands to copy an existing VPN config file in the /etc/NetworkManager/system-settings folder to a new file (as root, of course) in the same folder and make string replacements to the permitted user, gateway, username and password values in the new copy. Then I restart network manager to apply the changes.

For example:

A typical config file in /etc/NetworkManager/system-settings folder might look as such:

[connection]
id=<<id>>
uuid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
type=vpn
permissions=user:<<permissions_user>>:;
autoconnect=false

[vpn]
password-flags=0
service-type=org.freedesktop.NetworkManager.pptp
require-mppe-128=yes
mppe-stateful=yes
user=<<user>>
refuse-eap=yes
refuse-chap=yes
gateway=<<gateway>>
refuse-pap=yes

[vpn-secrets]
password=<<password>>

[ipv4]
method=auto

... so you can just create a new config file that looks similar to the one above...

cd /etc/NetworkManager/system-settings
cp "existing-working-vpn-config-file" "new-vpn-config-file"

...then replace the '<<>>' values above with your own VPN settings, eg:

sed -i "s/<<permissions_user>>/my_permissions_user/g" new-vpn-config-file
sed -i "s/<<user>>/my_user/g" new-vpn-config-file
sed -i "s/<<gateway>>/my_gateway/g" new-vpn-config-file
sed -i "s/<<password>>/my_password/g" new-vpn-config-file

... and then finally restart network manager via the following command:

service network-manager restart

Note: The UUID setting doesn't seem to matter, even if it's non-unique. No idea why. Stuff just works.

Also, If you are adding a new file rather than copying, make sure the permissions to the file are set to 600 (read and write), and the owner is root.

Give it a shot and tell me what you think. It works for me, all via command line.

0

I used this to run from root:

USER=japie
runuser -l $USER -c "sudo -S nmcli connection import type openvpn file ~/vpn_clients/flappie3-TO-IPFire.ovpn"

Connection 'flappie3-TO-IPFire' (26d0b28e-9212-4e71-90dc-3911ddf231e5) successfully added.

6
  • Why use runuser only to have the user use sudo? This does not add anything not already mentioned in e.g. Import VPN config files to NetworkManager from command line
    – Kusalananda
    Sep 12, 2019 at 10:35
  • laptop enrollment script runs as root
    – djieno
    Sep 13, 2019 at 11:45
  • Sure, and the you switch to japie using runuser, but then you switch back to root with sudo...
    – Kusalananda
    Sep 13, 2019 at 11:46
  • if you don't add "sudo ..." the current user/japie doesn't have enough rights to run nmcli
    – djieno
    Sep 13, 2019 at 11:49
  • So don't switch to that user with runas then... In short what I'm saying is that it ought to be enough with nmcli connection import type openvpn file ~japie/vpn_clients/flappie3-TO-IPFire.ovpn.
    – Kusalananda
    Sep 13, 2019 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .