Skip to content

Installing strongSwan IPsec on Ubuntu

A production strongSwan install — swanctl-style configuration, the systemd unit, certificates, and the verification checks an MSP runs before relying on the tunnel.

A directive lands on the ticket: “stand up an IPsec gateway.” This article is the strongSwan install we run on modern Ubuntu — the charon-systemd daemon with the swanctl configuration style (not the legacy ipsec.conf form), wired into a clean PKI, with a working test connection before we hand it to clients. The goal is “IPsec terminates on this box, every Security Association is observable, and the operator can answer swanctl --list-sas without thinking.”

How to verify

Before installing, confirm the box is in a state where strongSwan can be useful — no competing IKE daemon, kernel modules available, and the public IP that clients will hit is actually on the host.

ss -lunp | grep -E ':(500|4500)\b'
dpkg -l | grep -E 'strongswan|libreswan|openswan'
lsmod | grep -E 'esp|ah|xfrm'
ip -4 addr show

If a previous engineer left libreswan on the box, remove it first — the two share kernel hooks and configuration files in conflicting ways. If UDP 500 or 4500 is bound, identify the process before installing.

What’s happening

strongSwan is an IKE (Internet Key Exchange) daemon. It does not move data packets; it negotiates Security Associations with a peer, then hands key material to the kernel’s xfrm subsystem which encrypts ESP traffic in-line. Two protocols you will see:

  • IKEv1 — legacy, mode-config style, still used by older Cisco and pfSense gear.
  • IKEv2 — modern, MOBIKE for roaming, EAP for user authentication. Default for new builds.

The charon daemon handles IKE; swanctl is the CLI you use to load configs, list SAs, and trigger negotiations. The swanctl.conf file (under /etc/swanctl/conf.d/) is the modern, structured replacement for the older ipsec.conf — fewer footguns, but you must opt into it by installing strongswan-swanctl and using the strongswan systemd unit (not strongswan-starter).

Certificates live under /etc/swanctl/{x509,private,x509ca}/. The daemon reads them on swanctl --load-all.

The procedure

  1. Install the packages. Modern Ubuntu ships strongSwan 5.9.x; pick the swanctl flavour.

    apt update
    apt install -y strongswan strongswan-swanctl strongswan-pki libcharon-extra-plugins
    systemctl disable --now strongswan-starter 2>/dev/null || true
    systemctl enable --now strongswan
  2. Generate a PKI. strongSwan ships pki for this — it is parallel to Easy-RSA but with IPsec-friendly defaults.

    cd /etc/swanctl
    pki --gen --type ed25519 --outform pem > private/ca.key
    pki --self --in private/ca.key --type ed25519 \
        --dn "CN=vpn-ca.example.org" --ca --lifetime 3650 \
        --outform pem > x509ca/ca.crt
    
    pki --gen --type ed25519 --outform pem > private/server.key
    pki --pub --in private/server.key --type ed25519 | \
      pki --issue --cacert x509ca/ca.crt --cakey private/ca.key \
          --dn "CN=vpn.example.org" --san vpn.example.org \
          --flag serverAuth --flag ikeIntermediate \
          --lifetime 730 --outform pem > x509/server.crt
    chmod 600 private/*.key
  3. Write /etc/swanctl/conf.d/server.conf. Minimal IKEv2 server-side config:

    connections {
        roadwarrior {
            version = 2
            proposals = aes256gcm16-prfsha384-ecp384
            pools = roadwarrior-pool
            local {
                auth = pubkey
                certs = server.crt
                id = vpn.example.org
            }
            remote {
                auth = eap-mschapv2
                eap_id = %any
            }
            children {
                net {
                    local_ts  = 0.0.0.0/0
                    esp_proposals = aes256gcm16-ecp384
                }
            }
        }
    }
    
    pools {
        roadwarrior-pool {
            addrs = 10.20.0.0/24
            dns = 1.1.1.1, 9.9.9.9
        }
    }
    
    secrets {
        eap-alice {
            id = alice
            secret = "TOP-SECRET-passphrase"
        }
    }
  4. Load the config and verify.

    swanctl --load-all --noprompt
    swanctl --list-conns
    swanctl --list-certs
  5. Enable forwarding and NAT.

    sed -i 's/^#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf
    sysctl -p
    iptables -t nat -A POSTROUTING -s 10.20.0.0/24 -o eth0 -j MASQUERADE
    iptables -A FORWARD -m policy --pol ipsec --dir in  -s 10.20.0.0/24 -j ACCEPT
    iptables -A FORWARD -m policy --pol ipsec --dir out -d 10.20.0.0/24 -j ACCEPT
    iptables-save > /etc/iptables/rules.v4
  6. Open the firewall on UDP 500 and 4500.

    ufw allow 500/udp
    ufw allow 4500/udp
    ufw reload
  7. Smoke-test with a client. macOS, Windows, iOS, and Android all speak IKEv2/EAP natively. Configure the server hostname, the CA cert (drag-imported into Keychain or equivalent), and the alice EAP credentials. Then:

    swanctl --list-sas
    journalctl -u strongswan --since '5 min ago'

Common pitfalls

  • Mixing ipsec.conf and swanctl.conf — pick one. Modern Ubuntu defaults to the swanctl style; the strongswan-starter unit is the legacy path.
  • Forgetting the serverAuth and ikeIntermediate extended key usage flags on the server cert — Windows clients reject the connection with a vague TLS error.
  • Skipping the --san argument on the cert — iOS and Android clients enforce SAN matching; CN alone is no longer enough.
  • Not opening UDP 4500 — IKEv2 with NAT-T uses 4500 once it detects NAT; clients behind NAT will fail with “phase 2 negotiation failed” if 4500 is closed.
  • Storing EAP secrets in plain swanctl.conf files committed to git — they are passwords. Use secrets { include /etc/swanctl/secrets/*.secrets } and keep the secrets dir out of source control.

In the engagements we run, strongSwan gateways are paired with structured logging into Loki, alerts on SA churn, and a quarterly cert rotation. The install is the easy hour; the Managed Operations layer is what keeps the tunnel up at 3 a.m. when an upstream changes its IKE parameters without telling anyone.