Use nmcli to connect to OpenVPN Server on Ubuntu Desktop

I was trying to setup my Ubuntu machine to use nmcli to connect to my VPN and I just wanted wanted to document what I did for the next time I try it or for the next person

Change netplan renderer from networkd to NetworkManager

$ sudo cat /etc/netplan/10-lxc.yaml
network:
  version: 2
  ethernets:
    enp5s0:
      dhcp4: true
      dhcp-identifier: mac

# Create a backup
$ sudo cp -v /etc/netplan/10-lxc.yaml /root/

# Update to:
$ sudo vim /etc/netplan/10-lxc.yaml
network:
  version: 2
  renderer: NetworkManager

# Apply the changes
$ sudo netplan apply
$ reboot

# ref: https://askubuntu.com/a/1122769

Create a .nmconnection file

Create a .nmconnection file, similar to:

$ sudo ls -lah /etc/NetworkManager/system-connections/OpenVPN.nmconnection 
-rw------- 1 root root 418 May  3 17:06 /etc/NetworkManager/system-connections/OpenVPN.nmconnection

$ cat /etc/NetworkManager/system-connections/OpenVPN.nmconnection 
[connection]
id=OpenVPN
uuid=e87f6757-9e2a-3785-9a10-f022cccb4c1d
type=vpn

[vpn]
ca=/location/to-your-ca-cert/.cert/nm-openvpn/OpenVPN-ca.pem
cipher=AES-256-CBC
connection-type=password
dev=tun
password-flags=1
port=443
remote=ovpn.example.com
reneg-seconds=0
tunnel-mtu=1360
username=YOUR-USERNAME
service-type=org.freedesktop.NetworkManager.openvpn

[ipv4]
method=auto

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

[proxy]

# Change/Fix Permissions
$ chmod 0600 /etc/NetworkManager/system-connections/OpenVPN.nmconnection

Make sure to update:

  • ca
  • remote
  • username
  • maybe the id & uuid

Then restart your machine

Connect to the VPN

# Verify 
$ nmcli connection show
NAME                UUID                                  TYPE      DEVICE 
OpenVPN             e87f6757-9e2a-3785-9a10-f022cccb4c1d  vpn       --     

# Connect with
$ sudo nmcli connection up OpenVPN --ask