Log inSign up

Build an IKEv2 VPN gateway with strongSwan

Install charon-systemd, strongswan-swanctl and the extra plugins, get a public certificate for vpn.example.net, and describe the gateway in /etc/swanctl/conf.d/rw.conf with certificate authentication for the server and EAP-MSCHAPv2 passwords for users. Open UDP 500 and 4500, masquerade the client pool, and load everything with sudo swanctl --load-all. Windows, macOS, iOS and Android then connect with their built-in IKEv2 clients, no app needed.

Topic: VPN and private DNS · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13 · Updated 2026-12-03

When is IPsec the right choice over WireGuard?

Pick IPsec when you want the VPN client that is already built into the operating system, or when the other end is a router or office firewall that only speaks IKEv2. Every major desktop and mobile system ships an IKEv2 client, and most business routers support IPsec site-to-site tunnels. For your own devices where installing an app is fine, the WireGuard server guide is simpler to run.

This guide uses the modern swanctl interface of strongSwan rather than the legacy ipsec.conf files. The gateway gets the name vpn.example.net pointing at 198.51.100.24, clients receive addresses from 10.10.10.0/24, and the public interface is called eth0.

Which packages do you install?

On Ubuntu 24.04, 22.04 and Debian 12 or 13, install the systemd based daemon, the swanctl tool and the plugin packages that contain EAP support:

sudo apt update
sudo apt install -y charon-systemd strongswan-swanctl strongswan-pki libcharon-extra-plugins libstrongswan-extra-plugins certbot
swanctl --version
apt policy charon-systemd

Avoid the old strongswan metapackage here, because it pulls in the legacy starter that reads ipsec.conf. The daemon runs as strongswan.service, and its configuration tree is /etc/swanctl with separate folders for certificates, CA certificates and private keys.

How do you give the gateway a trusted certificate?

Use a certificate from Let's Encrypt so client devices trust the server without importing a custom CA. Create an A record for vpn.example.net first (see DNS records), open port 80 briefly, and request a certificate in standalone mode:

sudo ufw allow 80/tcp
sudo certbot certonly --standalone -d vpn.example.net --key-type rsa
sudo ufw delete allow 80/tcp

RSA keys give the widest client support. Copy the files into the strongSwan tree, because the daemon is confined by AppArmor and should not read the certbot folder directly:

sudo cp /etc/letsencrypt/live/vpn.example.net/cert.pem /etc/swanctl/x509/
sudo cp /etc/letsencrypt/live/vpn.example.net/chain.pem /etc/swanctl/x509ca/
sudo cp /etc/letsencrypt/live/vpn.example.net/privkey.pem /etc/swanctl/private/
sudo chmod 600 /etc/swanctl/private/privkey.pem

The Certbot guide explains renewal; a deploy hook later in this page repeats the copy automatically.

How do you write the road warrior connection?

Create /etc/swanctl/conf.d/rw.conf. The server proves itself with the certificate, users prove themselves with a name and password, and every client gets an address from the pool:

connections {
    rw {
        version = 2
        local_addrs = 198.51.100.24
        pools = rw_pool
        send_certreq = no
        proposals = aes256gcm16-prfsha384-ecp384,aes256-sha256-modp2048,default
        local {
            auth = pubkey
            certs = cert.pem
            id = vpn.example.net
        }
        remote {
            auth = eap-mschapv2
            eap_id = %any
        }
        children {
            rw {
                local_ts = 0.0.0.0/0
                esp_proposals = aes256gcm16-ecp384,aes256-sha256,default
                dpd_action = clear
            }
        }
    }
}

pools {
    rw_pool {
        addrs = 10.10.10.0/24
        dns = 9.9.9.9
    }
}

secrets {
    eap-alice {
        id = alice
        secret = "use-a-long-random-passphrase"
    }
}

Add one eap- block per user. Keep the file readable by root only with sudo chmod 600 /etc/swanctl/conf.d/rw.conf. If you run a private resolver such as Unbound on the same server, let it listen on a pool address and put that address under dns.

How do you route client traffic to the internet?

Turn on forwarding and masquerade the pool behind the public address. Enable forwarding persistently:

printf "net.ipv4.ip_forward=1\n" | sudo tee /etc/sysctl.d/90-ipsec.conf
sudo sysctl --system

With ufw, add a NAT table at the top of /etc/ufw/before.rules, above the existing *filter line:

*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 10.10.10.0/24 -o eth0 -j MASQUERADE
COMMIT

*mangle
:FORWARD ACCEPT [0:0]
-A FORWARD -s 10.10.10.0/24 -o eth0 -p tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1361:1536 -j TCPMSS --set-mss 1360
COMMIT

The MSS clamp prevents stalled HTTPS pages caused by the smaller packet size inside the tunnel. Then allow IKE and the forwarded traffic:

sudo ufw allow 500,4500/udp
sudo ufw route allow in on eth0 out on eth0 from 10.10.10.0/24
sudo ufw reload

The ufw guide covers the before.rules layout if you have edited it before.

How do you start the service and check it?

Enable the daemon and load connections, pools and secrets:

sudo systemctl enable --now strongswan
sudo swanctl --load-all
sudo swanctl --list-conns
sudo swanctl --list-certs

The certificate list should show your server certificate with a valid private key and the intermediate in the CA section. Watch a client connect in real time:

sudo journalctl -u strongswan -f

After a device connects, sudo swanctl --list-sas shows the IKE and child security associations with byte counters, and the client address from the pool.

How do clients connect?

Every native client needs the same three facts: the server name vpn.example.net, the type IKEv2, and a username and password.

  • Windows: Settings, Network, VPN, add a connection with type IKEv2 and sign-in info user name and password.
  • macOS and iOS: add a VPN of type IKEv2, enter the server name as both server and remote ID, choose username authentication.
  • Android: recent versions offer IKEv2/IPSec MSCHAPv2 in the VPN settings; the strongSwan app works on any version.

Once connected, a visit to any address-check page from the client should show 198.51.100.24. If the tunnel comes up but pages do not load, revisit the NAT and MSS rules.

How do you link an office network site to site?

A second file describes a fixed tunnel to a router with a static address, here 192.0.2.10 with the office LAN 192.168.50.0/24. A pre-shared key is common for such links:

connections {
    office {
        version = 2
        local_addrs = 198.51.100.24
        remote_addrs = 192.0.2.10
        local {
            auth = psk
            id = 198.51.100.24
        }
        remote {
            auth = psk
            id = 192.0.2.10
        }
        children {
            lan {
                local_ts = 198.51.100.24/32
                remote_ts = 192.168.50.0/24
                start_action = trap
            }
        }
    }
}
secrets {
    ike-office {
        id = 192.0.2.10
        secret = "a-different-long-random-key"
    }
}

Save it as /etc/swanctl/conf.d/office.conf, run sudo swanctl --load-all, and mirror the same proposals and subnets on the router. Allow UDP 500 and 4500 from 192.0.2.10; with start_action = trap the tunnel comes up when traffic first needs it.

How do you keep the certificate renewed and fix failures?

Add a certbot deploy hook that copies new files and reloads credentials. Create /etc/letsencrypt/renewal-hooks/deploy/swanctl.sh:

#!/bin/sh
cp /etc/letsencrypt/live/vpn.example.net/cert.pem /etc/swanctl/x509/
cp /etc/letsencrypt/live/vpn.example.net/chain.pem /etc/swanctl/x509ca/
cp /etc/letsencrypt/live/vpn.example.net/privkey.pem /etc/swanctl/private/
swanctl --load-creds

Make it executable with sudo chmod +x. Standalone renewals need port 80, so allow it in a pre hook or switch to a DNS challenge. Frequent errors and causes:

  • no matching proposal: the client offers algorithms you did not list; add default or the specific suite from the log.
  • EAP failed: wrong username or password, or the extra plugins package is missing.
  • certificate not trusted: the client expects the remote ID to equal vpn.example.net, or the chain file was not copied.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-2a handful of road warriors1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months
F2-4more users and faster encryption per tunnel2 vCPU, 4 GB RAM, 128 GB NVMe$14.40/mo$43.20 for 3 months

Questions about IPsec VPN with strongSwan

Does IPsec work behind NAT at home or in hotels?

Yes. IKEv2 detects NAT and moves to UDP port 4500 with encapsulation, which is why both 500 and 4500 must be open.

Can I add IPv6 for clients?

Yes. Add an IPv6 range from a unique local prefix to the pool, set local_ts to include ::/0, enable IPv6 forwarding and add a matching NAT rule in before6.rules.

Is EAP-MSCHAPv2 secure enough?

Inside IKEv2 it runs only after the server proved its identity with the certificate, so the password exchange is protected by the encrypted IKE channel. For higher assurance use client certificates with auth = pubkey on the remote side.

Why not use the ipsec.conf format?

It still works through the legacy starter, but swanctl is the current interface, reloads without restarts and is what the strongSwan project documents today.

Is there a ready image with a VPN?

Yes, the OpenVPN and WireGuard app images arrive configured. IPsec is a manual setup on a plain Ubuntu or Debian image.