2

I want to quickly import ovpn files into NetworkManager and append my login details into the final conf file that NetworkManager will use.

Basically turning this

client
dev tun
proto udp
remote italy.privateinternetaccess.com 1197
resolv-retry infinite
nobind
persist-key
persist-tun
cipher AES-256-CBC
auth SHA256
tls-client
remote-cert-tls server
auth-user-pass
comp-lzo
verb 1
reneg-sec 0
crl-verify crl.rsa.4096.pem
ca ca.rsa.4096.crt
disable-occ

into this

[connection]
id=Singapore
uuid=<unique id gen by uuidgen>
type=vpn
permissions=
secondaries=

[vpn]
connection-type=password
auth=SHA256
password-flags=0
remote=sg.privateinternetaccess.com:1197
cipher=AES-256-CBC
comp-lzo=yes
reneg-seconds=0
username=<username>
remote-cert-tls=server
ca=ca.rsa.4096.crt
dev=tun
service-type=org.freedesktop.NetworkManager.openvpn

[vpn-secrets]
password=<password>

[ipv4]
dns-search=
method=auto

[ipv6]
addr-gen-mode=stable-privacy
dns-search=
method=auto

Normally when NetworkManager imports the provided OVPN file, it outputs this.

[connection]                                                                                                                                                                                                     
id=Ireland
uuid=174fa5b0-0b49-40bc-a301-756acd341c12
type=vpn
permissions=
secondaries=

[vpn]
connection-type=password
auth=SHA256
password-flags=1
remote=ireland.privateinternetaccess.com:1197
cipher=AES-256-CBC
comp-lzo=yes
reneg-seconds=0
remote-cert-tls=server
ca=ca.rsa.4096.crt
dev=tun
service-type=org.freedesktop.NetworkManager.openvpn

[ipv4]
dns-search=
method=auto

[ipv6]
addr-gen-mode=stable-privacy
dns-search=
method=auto

I can quickly change passwords-flags via sed, but I'm at a lost on how to append the username and [vpn-secrets] section.

Any help would be appreciated.

3 Answers 3

4

The new PIA OpenVPN config files contain some errors in them that may prevent an OpenVPN connection from starting.

These are the errors.

...
cipher aes-256-cbc
auth sha-256
...

The fix is relative simple, just change them into fully uppercase. You can use sed to do it quickly for all the config files.

sed -i 's/aes-256-cbc/AES-256-CBC/g' *.ovpn

sed -i 's/sha256/SHA256/g' *.ovpn

To quickly import the files using NetworkManager, use this command.

for i in *.ovpn; do nmcli connection import file "$i" type openvpn; done

If you wish to add in your username and password into the NetworkManager config file, follow these steps.

First create a script.

#!/bin/bash

# Changes password-flags from 1 to 0
sed -i 's/password-flags=1/password-flags=0/g' $1

# Adds in a username entry after reneg-seconds
sed -i '/reneg-seconds=0/a username=<insert username here>' $1

# Adds in a section [vpn-secrets] for passwords after line 21
sed -i '21 a [vpn-secrets]' $1

# Adds in password into the config file after [vpn-secrets]
sed -i '/\[vpn-secrets]/a password=<insert password here>' $1

# Adds in a new line after password=    
sed -i '/password=/a\\' $1

Navigate to /etc/NetworkManager/system-connections or wherever NetworkManager stores its config files.

To quickly iterate the script over all the config files, run this command.

for i in "*"; do <script location> "$i"; done
5
0

Here's a modified version of the script of ghostinzshell that works for ProtonVPN configs. Turns out he used the fixed line 21 to add vpn-secrets, I just added below some know text (service-type):

#!/bin/bash

# Changes password-flags from 1 to 0
sed -i 's/password-flags=1/password-flags=0/g' $1

# Adds in a username entry after reneg-seconds
sed -i '/reneg-seconds=0/a username=USERNAME_HERE' $1

# Adds in password into the config file after [vpn-secrets]
sed -i '/service-type=/a\
\
[vpn-secrets]' $1
sed -i '/\[vpn-secrets]/a password=PASSWORD_HERE' $1
0

I created a script which does this using only the officially supported API (no sed munging):

#! /bin/sh

read -sp 'Username: ' username
echo
read -sp 'Password: ' password

for path in "$@"
do
    name=$(basename "$path" ".ovpn")

    sudo nmcli --terse connection delete id "$name" > /dev/null
    sudo nmcli --terse connection import type openvpn file "$path" > /dev/null

    sudo nmcli connection modify "$name" vpn.persistent true
    sudo nmcli connection modify "$name" vpn.user-name "$username"
    sudo nmcli connection modify "$name" +vpn.data "password-flags = 0, username = $username"
    sudo nmcli connection modify "$name" +vpn.secrets "password=$password"
done

echo

Assuming that the script is called import-openvpn, you can use it like this:

$ import-openvpn *.ovpn
Username:
Password:

This will delete the existing VPN connections, and then remake them with the specified username and password.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy