#!/usr/bin/env bash # Enable VPN at boot on Raspberry Pi OS (NetworkManager or WireGuard). # # NetworkManager (import VPN in nm-connection-editor or nmcli first): # sudo ./scripts/setup-vpn-autoconnect.sh nm "My VPN connection name" # # WireGuard (/etc/wireguard/wg0.conf must exist; interface name without .conf): # sudo ./scripts/setup-vpn-autoconnect.sh wg wg0 # # OpenVPN single file (needs network-manager-openvpn): # sudo ./scripts/setup-vpn-autoconnect.sh import-openvpn /home/pi/OpenVPN-Config.ovpn # sudo ./scripts/setup-vpn-autoconnect.sh nm OpenVPN-Config # set -euo pipefail usage() { sed -n '2,17p' "$0" | sed 's/^# \{0,1\}//' exit 1 } if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then usage fi case "${1:-}" in import-openvpn|import-ovpn) FILE="${2:-}" if [ -z "$FILE" ] || [ ! -f "$FILE" ]; then echo "Usage: $0 import-openvpn /path/to/profile.ovpn" >&2 exit 1 fi if ! command -v nmcli >/dev/null 2>&1; then echo "nmcli not found." >&2 exit 1 fi ABS="$(readlink -f "$FILE")" nmcli connection import type openvpn file "$ABS" NM_NAME="$(basename "$FILE" .ovpn)" nmcli connection modify "$NM_NAME" connection.autoconnect yes nmcli connection modify "$NM_NAME" connection.autoconnect-priority 20 echo "Imported as NM connection: $NM_NAME (autoconnect on)" echo "Connect now: sudo nmcli connection up \"$NM_NAME\"" ;; nm|network-manager) NAME="${2:-}" if [ -z "$NAME" ]; then echo "Usage: $0 nm \"Connection name\"" >&2 exit 1 fi if ! command -v nmcli >/dev/null 2>&1; then echo "nmcli not found. Install NetworkManager." >&2 exit 1 fi if ! nmcli -t -f NAME connection show "$NAME" >/dev/null 2>&1; then echo "No NetworkManager connection named: $NAME" >&2 echo "List: nmcli -f NAME,TYPE connection" >&2 exit 1 fi nmcli connection modify "$NAME" connection.autoconnect yes nmcli connection modify "$NAME" connection.autoconnect-priority 10 echo "Enabled autoconnect for NM profile: $NAME" echo "Reconnect now: sudo nmcli connection up \"$NAME\"" ;; wg|wireguard) IFACE="${2:-}" if [ -z "$IFACE" ]; then echo "Usage: $0 wg wg0 (config must be /etc/wireguard/\${IFACE}.conf)" >&2 exit 1 fi CFG="/etc/wireguard/${IFACE}.conf" if [ ! -f "$CFG" ]; then echo "Missing $CFG — create it first (e.g. copy from your VPN provider)." >&2 exit 1 fi systemctl enable "wg-quick@${IFACE}.service" echo "Enabled: wg-quick@${IFACE}.service (starts at boot)" echo "Start now: sudo systemctl start wg-quick@${IFACE}" ;; *) usage ;; esac