Some VPN services offer tens, or even hundreds, of different access servers in different locations. You can import them one at a time via the NetworkManager GUI. Or you can use nmcli (NetworkManager Command Line Interface) to quickly import all of the ovpn files one time. This also comes in handy for headless servers, or scripting.

In this Linux quick tip, we will show you how to import .ovpn files. We will also show you how to use a loop to install a bunch of files. Let’s get started…

Import An ovpn File from the Command Line

To import a single openvpn file all we have to do is pass a few options to nmcli.

nmcli connection import type openvpn file <filename.ovpn>

These options are fairly self-explanatory. However, let’s break it down for the people who are new to using nmcli commands.

  • nmcli – invoke the NetworkManager Command Line Interface
  • connection – Tell nmcli we are working on a connection
  • import – Tell nmcli we wish to import a connection
    • type – Specify the type of connection we want to import
      • openvpn – Type specified
    • file – Tell nmcli to import for a file
      • Pass the filename to be imported

At any time you can add --help to the end of your command to find more information about that option.

Here is an example of a successful import of an OpenVPN file using nmcli.

[[email protected] ~]$ nmcli connection import type openvpn file australia.ovpn 
Connection 'australia' (315b3d2a-138f-2964-36fa-7fdf4a932d99) successfully added.

Once the import is done, the VPN will be added to the NetworkManager connection list. It will also be available in the GUI. In Gnome, it will be available in Settings > Network network area as well as in the quick settings drop down menu on the top right of the screen.

gnome settings showing vpn access installed via openvpn import

Importing Multiple ovpn Files with nmcli

I have multiple OpenVPN connection files from around the world. Each has it’s own .ovpn file.

[[email protected] ~]$ ls -lrt
total 228
-rwxr-xr-x. 1 mcherisi mcherisi 5533 May  9 19:04 australia.ovpn
-rwxr-xr-x. 1 mcherisi mcherisi 5529 May  9 19:04 austria.ovpn
-rwxr-xr-x. 1 mcherisi mcherisi 5525 May  9 19:04 belgium.ovpn
-rwxr-xr-x. 1 mcherisi mcherisi 5525 May  9 19:04 brazil.ovpn
...OUTPUT TRUNCATED...

What if I wanted to use nmcli to import all the openvpn files?? My first instict was to pass the * wildcard. However, nmcli did not seem to like that.

[[email protected] ~]$ nmcli connection import type openvpn file *.ovpn
Error: invalid extra argument 'belgium.ovpn'.

[[email protected] ~]$ nmcli connection import type openvpn file *
Error: invalid extra argument 'belgium.ovpn'.

We can get by this with a simple for loop. Here we loop through a listing of the directory and pass each filename to the nmcli command.

[[email protected] ~]$ for i in $(ls *.ovpn); do nmcli connection import type openvpn file $i; done
Connection 'austria' (e371fab8-0e1b-4323-90f4-1c5a43725c41) successfully added.
Connection 'belgium' (ef362130-9c88-440e-13de-cb42e34af820) successfully added.
Connection 'brazil' (0dd57651-522c-30b5-44f4-123628fa9b04) successfully added.
Connection 'california' (cb3644fc-5c8e-3859-8039-8e321b1b2296) successfully added.
Connection 'canada' (5e44f749-e6f1-4a08-9aeb-428825822670) successfully added.
Connection 'colorado' (6c3b1fe9-e633-4f4a-9d06-3b2566aff8d2) successfully added.
...OUTPUT TRUNCATED...

That’s it, we were able to easily import all of the openvpn files using nmcli.

Conclusion

I hope you are now comfortable using nmcli to import your OpenVPN configuration files. Although the NetworkManager Command Line Interface is a very powerful tool, I enjoy it’s straight forward options. If you have a better method for importing the files sound off in the comments below.

Resources