When connecting branch offices or bridging multi-cloud environments, network teams often default to pricey appliances from Cisco or Fortinet. Yet two commodity Linux servers running StrongSwan can easily saturate a 10 Gbps fiber link. You get enterprise-grade IPsec tunneling with zero recurring license bills. Modern IKEv2 setups handle dynamic re-keying and unstable links cleanly, leaving behind the fragile workarounds of legacy IPsec.
Quick Start (5 Min)
Let’s spin up a minimal tunnel between two endpoints: Site A (Subnet: 10.10.0.0/24, Public IP: 203.0.113.10) and Site B (Subnet: 10.20.0.0/24, Public IP: 198.51.100.20). We’ll use Debian/Ubuntu packages and a Pre-Shared Key (PSK) to establish the link.
1. Install StrongSwan and Enable Packet Forwarding
First, install the daemon and cryptographic plugins on both gateway machines:
sudo apt update && sudo apt install -y strongswan strongswan-pki libcharon-extra-plugins
Enable IPv4 packet forwarding next. Without this, your gateway drops transit traffic instead of routing it between subnets:
sudo sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.d/99-ipsec.conf
sudo sysctl --system
2. Configure the Tunnel on Site A
Open /etc/ipsec.conf on the Site A gateway and define your connection parameters:
config setup
charondebug="ike 1, knl 1, cfg 0"
uniqueids=no
conn sitea-to-siteb
keyexchange=ikev2
ike=aes256-sha256-modp2048!
esp=aes256-sha256!
dpdaction=restart
dpddelay=30s
dpdtimeout=120s
authby=secret
auto=start
# Local side (Site A)
left=203.0.113.10
leftsubnet=10.10.0.0/24
leftid=203.0.113.10
# Remote side (Site B)
right=198.51.100.20
rightsubnet=10.20.0.0/24
rightid=198.51.100.20
Store your pre-shared secret in /etc/ipsec.secrets. Lock the file permissions down to 0600:
sudo chmod 600 /etc/ipsec.secrets
echo '203.0.113.10 198.51.100.20 : PSK "K9x#vL7$mQ2@zP4!wR8^bN1&"' | sudo tee -a /etc/ipsec.secrets
3. Configure the Mirror on Site B
On Site B, simply swap the left and right IP addresses and subnets in /etc/ipsec.conf. Add the identical PSK string to Site B’s /etc/ipsec.secrets, then reload both gateways:
sudo systemctl restart strongswan-starter
sudo ipsec statusall
You should see an active Security Association (SA) labeled ESTABLISHED within 2 to 3 seconds. Run a test ping from 10.10.0.15 to 10.20.0.42 to verify packet flow.
Deep Dive: Architecture and Packet Flow
Troubleshooting IPsec becomes much simpler once you separate control plane signaling from data plane throughput. StrongSwan runs in user space as the Charon daemon. It manages IKE negotiations, authenticates peers, and installs crypto policies into the Linux kernel via Netlink. It does not touch your actual payload packets. Instead, the Linux kernel’s native XFRM framework handles hardware-accelerated AES-NI encryption directly in wire time.
IKEv2 vs Legacy IKEv1
Sticking with IKEv2 avoids several well-known design flaws in IKEv1:
- Reliable State Synchronization: IKEv2 enforces strict request/response message pairs. If an intermediate packet drops, the peer retransmits cleanly without deadlocking into a half-open state.
- Built-in NAT Traversal: The protocol detects NAT gateways during the initial negotiation. When needed, it automatically flips encapsulation to UDP port 4500 without requiring vendor-specific hacks.
- Lower Handshake Latency: IKEv2 sets up an encrypted session in just 4 packets. Legacy IKEv1 Main Mode requires 6 to 9 packets before passing traffic.
- Native Dead Peer Detection (DPD): Heartbeats are part of the core standard rather than a patchwork of incompatible vendor extensions.
Firewall and NAT Traversal Requirements
Ensure edge firewalls and cloud security groups (AWS Security Groups or Azure NSGs) permit these protocols:
- UDP Port 500: Carries initial IKE negotiations.
- UDP Port 4500: Handles NAT-Traversal (NAT-T) encapsulated ESP packets.
- IP Protocol 50 (ESP): Carries raw encrypted payloads when neither gateway sits behind NAT.
Watch out for outbound masquerading. If your gateway runs a catch-all MASQUERADE rule, it rewrites local IPs before the kernel checks XFRM policies. Once rewritten, traffic misses the tunnel selector entirely. Insert an explicit bypass rule at the top of your NAT chain to keep original source IPs intact:
sudo iptables -t nat -I POSTROUTING 1 -s 10.10.0.0/24 -d 10.20.0.0/24 -j ACCEPT
Advanced Usage: Modern VICI and swanctl
While ipsec.conf works, StrongSwan deprecated it in favor of the Versatile IKE Configuration Interface (VICI) and swanctl. The new syntax brings structured formatting, native JSON/REST bindings, and live reloads that won’t drop existing customer tunnels.
Migrating to swanctl.conf
Place your connection file in /etc/swanctl/conf.d/sitea-to-siteb.conf:
connections {
sitea-to-siteb {
version = 2
local_addrs = 203.0.113.10
remote_addrs = 198.51.100.20
local {
auth = psk
id = 203.0.113.10
}
remote {
auth = psk
id = 198.51.100.20
}
children {
branch-subnets {
local_ts = 10.10.0.0/24
remote_ts = 10.20.0.0/24
esp_proposals = aes256-sha256-modp2048,default
dpd_action = restart
start_action = start
}
}
}
}
secrets {
ike-1 {
id-1 = 198.51.100.20
secret = "K9x#vL7$mQ2@zP4!wR8^bN1&"
}
}
Apply changes instantly without interrupting ongoing connections:
sudo swanctl --load-all
sudo swanctl --list-sas
X.509 Certificate Authentication
Pre-shared keys turn into an operational nightmare once you manage dozens of interconnected gateways. Moving to mutual X.509 certificate authentication gives you automated expiration dates and instant certificate revoking. Generate your keys with StrongSwan’s pki utility:
# 1. Generate CA key and self-signed certificate
pki --gen --type rsa --size 4096 --outform pem > caKey.pem
pki --self --ca --lifetime 3650 --in caKey.pem \
--dn "CN=VPN Root CA" --outform pem > caCert.pem
# 2. Issue Gateway Certificate for Site A
pki --gen --type rsa --size 2048 --outform pem > siteAKey.pem
pki --issue --lifetime 730 --cacert caCert.pem --cakey caKey.pem \
--in siteAKey.pem --dn "CN=site-a.example.internal" \
--san 203.0.113.10 --outform pem > siteACert.pem
Copy caCert.pem to /etc/swanctl/x509ca/, place the host cert in /etc/swanctl/x509/, and lock the key in /etc/swanctl/private/. Finally, update the local block in swanctl.conf with auth = pubkey and certs = siteACert.pem.
Field Notes & Troubleshooting
Running high-throughput tunnels in production brings subtle edge cases. ICMP pings often succeed while interactive sessions mysteriously freeze up.
1. Clamp MSS to Stop Silent Packet Drops
IPsec encapsulation adds roughly 56 to 72 bytes of ESP and UDP overhead. If a client transmits a standard 1500-byte frame with the Don’t Fragment (DF) flag set, the gateway cannot encapsulate it without exceeding the WAN MTU. When upstream routers block ICMP type 3 code 4 (Fragmentation Needed), Path MTU Discovery breaks. You get a classic black hole: small SSH commands work, but large outputs like cat /var/log/syslog freeze the terminal.
Clamp TCP MSS at your gateway to enforce safe payload sizes:
sudo iptables -t mangle -A FORWARD -p tcp --tcp-flags SYN,RST SYN \
-m tcpmss --mss 1361:1536 -j TCPMSS --clamp-mss-to-pmtu
2. Kernel-Level Inspection Commands
When tunnels stall, stop restarting the daemon blindly. Inspect the actual transformation states and routing policies inside the kernel:
# View installed IPsec cryptographic states and SPIs
sudo ip xfrm state
# Confirm traffic selectors match your subnets
sudo ip xfrm policy
# Tail Charon daemon logs in real time
sudo journalctl -u strongswan -f
3. Surviving Flaky WAN Links
If your ISP drops line quality or cycles your public IP address, avoid dpdaction = clear. Set dpdaction = restart paired with auto = route in ipsec.conf (or start_action = trap in swanctl). This tells the Linux kernel to keep the security policy active even if the remote peer goes silent. As soon as an internal host emits an outbound packet, the kernel automatically fires an acquisition request to revive the tunnel on demand.
