Operating System CentOS

Set Up a strongSwan IKEv2 VPN with swanctl on AlmaLinux 9 and Rocky Linux 9

Build an IKEv2 remote-access VPN on the RHEL 9 family with strongSwan from EPEL: certificates with pki, a swanctl.conf connection, firewalld rules and client setup.

This guide sets up strongSwan as an IKEv2 remote-access VPN server on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9. Clients authenticate with a username and password (EAP-MSCHAPv2), the server proves its identity with a certificate, and all client traffic is routed through the server. The configuration uses swanctl.conf, the current strongSwan configuration format.

CentOS 7 reached end of life in June 2024. Older tutorials for it configure strongSwan through ipsec.conf and ipsec.secrets and enable IKEv1 with XAuth. The strongSwan project has deprecated those files in favour of swanctl, and IKEv1 is not worth deploying today: Windows, macOS, iOS and Android all have IKEv2 clients built in or freely available.

Requirements

  • A server from the RHEL 9 family with a public IP address (203.0.113.10 in the examples) and a user with sudo rights.
  • A DNS name that points to the server, here vpn.example.com. Clients check that the name they connect to appears in the server certificate.
  • firewalld running, as it is by default on these distributions.

Install strongSwan from EPEL

strongSwan is not in the RHEL 9 base repositories; the base system ships Libreswan instead. The strongswan package comes from EPEL:

sudo dnf install epel-release
sudo dnf install strongswan

The EPEL package keeps its configuration under /etc/strongswan rather than directly in /etc. Where the strongSwan documentation mentions /etc/swanctl, the path on this system is /etc/strongswan/swanctl. The pki tool is run as strongswan pki.

Create the certificates

You need a certificate authority (CA) and a server certificate signed by it. Work as root in a private directory:

sudo -i
mkdir -p /root/vpn-pki
chmod 700 /root/vpn-pki
cd /root/vpn-pki

Generate the CA key and a self-signed CA certificate valid for ten years. RSA keys are used because Windows clients do not accept Ed25519 certificates.

strongswan pki --gen --type rsa --size 4096 --outform pem > caKey.pem
strongswan pki --self --ca --lifetime 3652 --in caKey.pem \
    --dn "C=US, O=Example, CN=Example VPN Root CA" \
    --outform pem > caCert.pem

Generate the server key and a certificate request. The host name must be present as a subjectAltName:

strongswan pki --gen --type rsa --size 4096 --outform pem > serverKey.pem
strongswan pki --req --type priv --in serverKey.pem \
    --dn "C=US, O=Example, CN=vpn.example.com" \
    --san vpn.example.com --outform pem > serverReq.pem

Sign the request with the CA. The serverAuth flag is required by Windows and several other clients:

strongswan pki --issue --cacert caCert.pem --cakey caKey.pem \
    --type pkcs10 --in serverReq.pem --serial 01 --lifetime 1826 \
    --flag serverAuth --outform pem > serverCert.pem

Install the files where swanctl looks for them, and protect the keys:

install -m 0644 caCert.pem /etc/strongswan/swanctl/x509ca/
install -m 0644 serverCert.pem /etc/strongswan/swanctl/x509/
install -m 0600 serverKey.pem /etc/strongswan/swanctl/private/
chmod 600 caKey.pem

The CA key signs every certificate your clients will trust. Keep it off the server if you can, and never copy it to a client.

Configure the connection

The packaged swanctl.conf includes every *.conf file in /etc/strongswan/swanctl/conf.d. Still as root, create a new file there:

nano /etc/strongswan/swanctl/conf.d/ikev2-eap.conf

Add the following. Replace the host name, the DNS server address and the user credentials with your own.

connections {
    ikev2-eap {
        version = 2
        pools = vpn-pool
        send_cert = always
        dpd_delay = 30s

        local {
            auth = pubkey
            certs = serverCert.pem
            id = vpn.example.com
        }
        remote {
            auth = eap-mschapv2
            eap_id = %any
        }
        children {
            net {
                local_ts = 0.0.0.0/0
            }
        }
    }
}

pools {
    vpn-pool {
        addrs = 10.10.10.0/24
        dns = 203.0.113.53
    }
}

secrets {
    eap-deploy {
        id = deploy
        secret = "use-a-long-random-password-here"
    }
}

What the sections do:

  • version = 2 restricts the connection to IKEv2.
  • local is the server: it authenticates with the certificate and presents vpn.example.com as its identity.
  • remote is any client: it must pass EAP-MSCHAPv2 with a user name and password from the secrets section.
  • local_ts = 0.0.0.0/0 tells clients to send all IPv4 traffic through the tunnel.
  • pools hands each client a virtual address from 10.10.10.0/24 and the DNS resolver to use. Pick a range that does not overlap your clients' home networks, and a resolver the server can reach.
  • Each additional user gets another eap-name block in secrets.

The file contains passwords, so restrict it:

chmod 600 /etc/strongswan/swanctl/conf.d/ikev2-eap.conf

Enable forwarding and open the firewall

Allow the kernel to forward packets between the tunnel and the internet:

echo "net.ipv4.ip_forward = 1" > /etc/sysctl.d/90-vpn.conf
sysctl --system

Allow IPsec through firewalld and masquerade traffic coming from the VPN address pool, so replies find their way back:

firewall-cmd --permanent --add-service=ipsec
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.10.10.0/24" masquerade'
firewall-cmd --reload

If your provider has a separate network firewall in its control panel, open UDP ports 500 and 4500 there as well.

Start strongSwan

The strongswan service runs the charon-systemd daemon and loads the swanctl configuration at startup:

systemctl enable --now strongswan

Check that the connection, pool and certificates were loaded:

swanctl --list-conns
swanctl --list-pools
swanctl --list-certs

Whenever you change a file under /etc/strongswan/swanctl, for example to add a user, reload it without dropping existing tunnels:

swanctl --load-all

Leave the root shell with exit when you are done.

Connect a client

Copy /root/vpn-pki/caCert.pem to each client device and install it as a trusted root certificate authority. Then create an IKEv2 connection with these settings:

  • Server: vpn.example.com (the name, not the IP address, because it has to match the certificate)
  • Remote ID, where asked: vpn.example.com
  • Authentication: user name and password (EAP-MSCHAPv2), using the values from the secrets section

Windows, macOS and iOS have IKEv2 clients built in. On Android you can use the strongSwan app with the "IKEv2 EAP (Username/Password)" type.

Verify and troubleshoot

With a client connected, list the active tunnels on the server:

sudo swanctl --list-sas

You should see the user's EAP identity and the virtual address it was given. To watch a connection attempt as it happens:

sudo swanctl --log

The same messages are kept in the journal: sudo journalctl -u strongswan.

  • The client reports a certificate or "13801" error: the name it connects to is not in the server certificate's subjectAltName, or the CA certificate is not installed as a trusted root on the client.
  • "no matching peer config found" in the log: the client proposed an authentication method or identity the connection does not allow. Check that it is set to user name and password, not certificate or pre-shared key.
  • "NO_PROPOSAL_CHOSEN": client and server have no cipher suite in common. The log lists what the client offered; older clients may need stronger settings enabled on their side.
  • Connected, but no internet: check sysctl net.ipv4.ip_forward and sudo firewall-cmd --list-all. The masquerade rule must be listed, and the zone must allow forwarding (sudo firewall-cmd --permanent --add-forward followed by a reload if forward shows no).

The complete option reference is in the strongSwan documentation for swanctl.conf, and the certificate commands are explained in its PKI quickstart.

Frequently asked questions

Can I use a certificate from a public CA instead of my own?

Yes. If the server certificate for vpn.example.com comes from a CA that client devices already trust, you do not have to install a CA certificate on each client. Put the certificate in x509, its key in private and any intermediate CA certificate in x509ca, and reload with swanctl --load-all after every renewal.

Why not a pre-shared key?

A shared key is the same secret on every device: anyone who has it can impersonate the server to the others, and removing one person's access means changing it everywhere. A server certificate plus per-user passwords lets you remove a single user by deleting one secrets block.

Does this configuration carry IPv6?

No. It assigns IPv4 addresses and routes IPv4 only. For IPv6, add an IPv6 pool, add ::/0 to local_ts, enable net.ipv6.conf.all.forwarding and make sure the server has routed IPv6 space to hand out.

How do I disconnect a user?

Remove or change the user's entry in secrets, run swanctl --load-all, then end the active tunnel. swanctl --list-sas shows the connection name and ID, and swanctl --terminate --ike ikev2-eap closes the tunnels of that connection.

Share story:

Continue reading other guides on How VPS.