Skip to content

Instantly share code, notes, and snippets.

@samveen
Last active March 1, 2025 07:25
Show Gist options
  • Save samveen/1b4b3c47721a2b5180375d9f47f3b47d to your computer and use it in GitHub Desktop.
Save samveen/1b4b3c47721a2b5180375d9f47f3b47d to your computer and use it in GitHub Desktop.
MicroHOWTO - NetworkManager controlled OpenVPN connections
#!/bin/bash
# {MicroHOWTO|Script} to setup a NetworkManager controlled openvpn connection
# OVPN Config file is the parameter to the script (no checks, blind faith)
OVPN_CONF="$1"
# Install the requirements if not available
# SUDO REQUIRED
sudo apt install -y openvpn network-manager-openvpn # Debian 10/*buntu 20.04 and later
# yum -y install openvpn NetworkManager-openvpn # RHEL 7/Rocky 7/Fedora ?? and later
# Import connection into NetworkManager
# SUDO NOT REQUIRED: Well thought out implementation of NM
nmcli connection import type openvpn file "${OVPN_CONF}"
# Fix the problem of VPN adding it's default routes at higher priority
# than default route of underlying local network (and thus breaking network access)
nmcli con modify "${OVPN_CONF//.ovpn}" ipv4.never-default true
nmcli con modify "${OVPN_CONF//.ovpn}" ipv6.never-default true
# Fix issue with interface not asking for username
USER="$(grep -Eh '^\s+Subject:.* CN=' "${OVPN_CONF}" | sed 's#.* CN=\([^,/]\+\)#\1#g')"
nmcli con modify "${OVPN_CONF//.ovpn}" vpn.user-name "$USER"
# Show the connection info
nmcli con show "${OVPN_CONF//.ovpn}"
# Use NetworkManager UI applet to start VPN
echo "Use the Network applet of your UI to control ${OVPN_CONF//.ovpn} VPN start/stop as per your requirements."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment