I finally found a solution based on this here.
This is how I did it:
Install Network Manager and strong swan plugin
sudo apt install network-manager network-manager-strongswan
Edit the globally managed devices file and change unmanaged devices to none
echo "[keyfile]
unmanaged-devices=none" | sudo tee /usr/lib/NetworkManager/conf.d/10-globally-managed-devices.conf
Restart the network manager service
sudo systemctl restart NetworkManager
Import the .p12 certificate
CERT_PATH=path/to/your/.p12
openssl pkcs12 -in $CERT_PATH -cacerts -nokeys -out $HOME/ca.cer
openssl pkcs12 -in $CERT_PATH -clcerts -nokeys -out $HOME/client.cer
openssl pkcs12 -in $CERT_PATH -nocerts -nodes -out $HOME/client.key
rm $CERT_PATH
sudo chown root:root ca.cer client.cer client.key
sudo chmod 600 ca.cer client.cer client.key
Create a VPN connection
Note: Here the name VPN
can be anything you want
$SERVER="your server IP/domain"
sudo nmcli c add type vpn ifname -- vpn-type strongswan connection.id VPN connection.autoconnect no vpn.data 'address = $SERVER, certificate = $HOME/ca.cer, encap = no, esp = aes128gcm16, ipcomp = no, method = key, proposal = yes, usercert = $HOME/client.cer, userkey = $HOME/client.key, virtual = yes'
Connect to it
nmcli c up 'Wired connection 1'
nmcli c up VPN
nmcli c
Check if connected
ifconfig
You should see a new entry for your VPN, usually called tunnel
It is recommended to run all the steps here as root sudo su
so that the $HOME will be /root
.
Here is a complete script I created based on these steps, you can run it as ./vpn.sh path/to/.p12 vpn.yourdomain.com
vpn.sh
#!/bin/bash
# Check if the correct number of arguments were provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <certificate path> <server address>"
exit 1
fi
CERT_PATH=$1
SERVER_ADDRESS=$2
# Step 1: Install Network manager and strongswan plugin
sudo apt update
sudo apt-get install -y network-manager network-manager-strongswan
# Step 2: Edit the globally managed devices file and change unmanaged devices to none
echo "[keyfile]
unmanaged-devices=none" | sudo tee /usr/lib/NetworkManager/conf.d/10-globally-managed-devices.conf
# Step 3: Restart the network manager service
sudo systemctl restart NetworkManager
# Step 4: Check if the devices are managed
nmcli d
# Step 6: Import the .p12 certificate
openssl pkcs12 -in $CERT_PATH -cacerts -nokeys -out $HOME/ca.cer
openssl pkcs12 -in $CERT_PATH -clcerts -nokeys -out $HOME/client.cer
openssl pkcs12 -in $CERT_PATH -nocerts -nodes -out $HOME/client.key
rm $CERT_PATH
sudo chown $USER:$USER $HOME/ca.cer $HOME/client.cer $HOME/client.key
sudo chmod 600 $HOME/ca.cer $HOME/client.cer $HOME/client.key
# Step 7: Create a VPN connection in NetworkManager and enable it.
sudo nmcli c add type vpn ifname -- vpn-type strongswan connection.id VPN connection.autoconnect no vpn.data "address = $SERVER_ADDRESS, certificate = $HOME/ca.cer, encap = no, esp = aes128gcm16, ipcomp = no, method = key, proposal = yes, usercert = $HOME/client.cer, userkey = $HOME/client.key, virtual = yes"
nmcli c up 'Wired connection 1'
nmcli c up VPN
nmcli c