An IPsec VPN is an encrypted tunnel between two networks, or between a device and a network, built with the IPsec protocol suite at Layer 3. It authenticates both ends with IKE, encrypts every packet with ESP, and protects any application because it works on the IP layer itself.
- IKE on UDP 500
- NAT-T on UDP 4500
- ESP is IP protocol 50
- Tunnel mode for site to site
- Transport mode for host to host
On this page
- What IPsec actually protects
- Anatomy of an ESP packet
- How an IPsec tunnel comes up
- What the configuration looks like
- IPsec VPN vs SSL VPN vs WireGuard
- Tunnel mode vs transport mode
- When it breaks: read the phase, not the vendor
- When to use an IPsec VPN, and when not to
- Thirty years in five stops
- Comparison
- FAQ
FundamentalsWhat IPsec actually protects
IPsec is not one protocol. It is a set of standards from the IETF that add three guarantees to IP traffic. Because it sits at the network layer, everything above it, from a database replication stream to a VoIP call, is covered without changing the application.
- CConfidentialityNobody on the path can read the payload. ESP encrypts it with AES-GCM or AES-CBC.
- IIntegrityNobody can alter a packet in transit without the receiver noticing. Every packet carries an integrity check value.
- AAuthenticationEach end proves who it is before any data flows, with a pre-shared key or a certificate.
The word "VPN" in IPsec VPN just means the tunnel is used to join two private networks, or one remote host to a private network, across a public one. The public network is almost always the internet, but the same design runs over MPLS or a partner link.
Packet anatomyAnatomy of an ESP packet
Every protected packet carries the same envelope. Two fields travel unencrypted on purpose: the SPI tells the receiver which key to use, and the sequence number lets it drop replayed packets without spending CPU on decryption.
Figure 2. The ESP packet from RFC 4303. Blue rows are encrypted, the white rows are sent in the clear so the receiver can find the right security association and reject replays before decrypting anything. Hover a field.
The handshakeHow an IPsec tunnel comes up
Before any user traffic flows, the two gateways negotiate. IKE (Internet Key Exchange) does that work in two phases, and almost every troubleshooting session starts by asking which phase failed.
- IKE Phase 1: build the management channel
Both peers agree on encryption, hashing, and Diffie-Hellman group, then authenticate with a pre-shared key or certificates. Result: the IKE SA, a secure channel used only for negotiation.
GW A →IKE_SA_INIT: proposals, DH public value, nonce← GW BIKE_SA_INIT: chosen proposal, DH value, nonceGW A →IKE_AUTH: identity, certificate, AUTH payload← GW BIKE_AUTH: identity, AUTH, first child SA - IKE Phase 2: build the data channel
Inside the protected channel the peers negotiate the IPsec SA: which traffic to protect, which cipher, and how long keys live. Two one-way SAs are created, one per direction, each with its own SPI.
GW A →CREATE_CHILD_SA: TS 10.1.0.0/24 ↔ 10.2.0.0/24← GW BCREATE_CHILD_SA: accepted, SPI 0x4a91c2e0 - ESP carries the traffic
Each packet that matches the selectors is encrypted and authenticated, wrapped in a new header, and sent. The receiving gateway looks up the SPI, checks the sequence number, verifies the integrity tag, decrypts, and forwards the inner packet into the LAN.
- Rekey and dead peer detection
Keys rotate on a timer or a byte count so a captured key exposes only a slice of traffic. DPD probes tell a gateway when the other side has gone silent so it can tear down the stale SA and fail over.
The tunnel in numbers
What a healthy IKEv2 site-to-site tunnel looks like once it is up.
ConfigurationWhat the configuration looks like
The same tunnel, three vendors. Every version answers the same five questions: who is the peer, how do we authenticate, which subnets, which ciphers, which IKE version.
# /etc/swanctl/swanctl.conf, site-to-site, IKEv2, certificates connections { hq-branch { local_addrs = 203.0.113.1 remote_addrs = 198.51.100.7 local { auth = pubkey certs = hqCert.pem } remote { auth = pubkey id = branch.example.com } children { lan { local_ts = 10.1.0.0/24 remote_ts = 10.2.0.0/24 esp_proposals = aes256gcm16-x25519 start_action = start } } version = 2 proposals = aes256-sha256-x25519 } }
! IKEv2 site-to-site with a virtual tunnel interface crypto ikev2 proposal PB-PROP encryption aes-gcm-256 prf sha256 group 19 crypto ikev2 profile PB-PROFILE match identity remote address 198.51.100.7 255.255.255.255 authentication remote pre-share authentication local pre-share keyring local PB-KEYS crypto ipsec transform-set PB-TS esp-gcm 256 mode tunnel interface Tunnel0 ip address 172.16.0.1 255.255.255.252 tunnel source 203.0.113.1 tunnel destination 198.51.100.7 tunnel mode ipsec ipv4 tunnel protection ipsec profile PB-IPSEC
# VPN > IPsec > Tunnels (values as shown in the GUI) Phase 1 Key Exchange IKEv2 Remote Gateway 198.51.100.7 Authentication Mutual PSK Encryption AES256-GCM, 128 bit Hash SHA256 DH Group 19 (nist ecp256) Phase 2 Mode Tunnel IPv4 Local Network 10.1.0.0/24 Remote Network 10.2.0.0/24 Protocol ESP Encryption AES256-GCM PFS 19 (nist ecp256)
ComparisonIPsec VPN vs SSL VPN vs WireGuard
The three are not rivals for the same job. IPsec is the site-to-site standard that every firewall speaks. SSL VPN is the remote-access default because it passes through almost any network. WireGuard is the newest and simplest, but its key model fits fleets you control more than partners you do not.
| Criterion | IPsec VPN | SSL VPN | WireGuard |
|---|---|---|---|
| OSI layer | 3 (network) | 4 to 7 (TLS session) | 3 (network) |
| Ports | UDP 500, 4500; proto 50 | TCP 443 (usually) | UDP 51820 (default) |
| Typical use | Site to site, cloud VPC links | Remote users, BYOD | Cloud, homelab, mobile |
| Key exchange | IKEv1 / IKEv2 | TLS handshake | Noise protocol, static keys |
| Cipher agility | Negotiated | Negotiated | Fixed suite |
| Passes guest Wi-Fi | Often blocked | Almost always | Sometimes blocked |
| Vendor support | Every firewall and cloud | Vendor specific clients | Growing; some firewalls |
| Config size | Large | Medium | ~10 lines |
Which VPN do you actually need?
Pick the situation that matches yours.
ModesTunnel mode vs transport mode
Tunnel mode encrypts the whole original packet and adds a new IP header, so the internal addresses never appear on the internet. It is the mode for gateways. Transport mode keeps the original header and encrypts only the payload; it is used host to host, or as the inner layer under L2TP.
TroubleshootingWhen it breaks: read the phase, not the vendor
Ninety percent of IPsec tickets fall into five buckets, and the log tells you which one before you open a single config file.
DecisionWhen to use an IPsec VPN, and when not to
Use it when you connect networks: a branch to headquarters, an on-premises firewall to an AWS or Azure VPC, or two data centers. Every mainstream firewall and every cloud provider exposes it as a first-class feature.
Skip it for individual remote users unless your firewall already bundles a good client. Hotel and airport networks routinely block UDP 500 and 4500, which SSL VPN on TCP 443 sails through. For a fleet of servers you control end to end, WireGuard needs a fraction of the configuration and CPU.
TimelineThirty years in five stops
- 1995RFC 1825 defines the IP security architecture. AH and ESP are born.
- 1998IKEv1 (RFC 2409) standardizes key exchange. Main mode and aggressive mode appear.
- 2005IKEv2 (RFC 4306) fixes the mess: fewer messages, NAT-T built in, dead peer detection.
- 2014RFC 7296 consolidates IKEv2; MOBIKE makes roaming clients practical.
- TodayAES-GCM and elliptic-curve groups are the default; every cloud sells a managed IPsec endpoint.
ComparisonIPsec, an SSL VPN and WireGuard on the criteria that decide it
| Criterion | IPsec with IKEv2 | SSL or TLS VPN | WireGuard |
|---|---|---|---|
| What it carries | Whole IP packets, and every protocol above them | Whatever the client proxies, in practice TCP applications or a browser session | Whole IP packets, and every protocol above them |
| Ports on the wire | UDP 500 for IKE, UDP 4500 once a NAT is in the path, otherwise ESP as IP protocol 50 | TCP 443, the one port open on every network you will ever sit on | UDP on a single port you choose, 51820 by default |
| Getting out of a hotel network | Often blocked, because ESP is neither TCP nor UDP and the gear in front of you drops it | Almost always passes, because it looks exactly like HTTPS | Passes wherever outbound UDP is allowed, fails where only TCP is |
| Cipher negotiation | Negotiated per tunnel, which is why two vendors can disagree for an afternoon | Negotiated in the handshake, though TLS 1.3 cut the list down | Nothing to negotiate. One fixed suite: ChaCha20, Poly1305, Curve25519, BLAKE2 |
| Two different vendors at each end | The only one where a firewall from one vendor reliably talks to a firewall from another | In practice the vendor client talks to the vendor gateway | The same implementation at both ends |
| Client moves to a new address | MOBIKE, RFC 4555, keeps the association alive across the change when both ends support it | A new connection opens and the session usually resumes | The peer updates its endpoint from the first authenticated packet that arrives from the new address |
| How narrow you can make access | Traffic selectors between networks, so everything inside the tunnel is reachable | Per user and per application, which is the answer an auditor is looking for | Allowed addresses per peer, explicit but coarse |
| What runs on the client | Already built into Windows, macOS, iOS and Android, so there is nothing to install | A vendor client, or nothing at all for browser based access | A small client everywhere, and in the Linux kernel since 5.6 in March 2020 |
| Where it hurts | MTU and fragmentation, and phase 2 selectors that do not match | Throughput, because a TCP tunnel carrying TCP stalls twice under loss | No address assignment, user directory or group policy of its own |
| Reach for it when | Two offices, or an office and a cloud network, need to behave like one network | A handful of remote people need a handful of internal applications | You control both ends and want a link that comes back instantly |
FAQFrequently asked questions
Is IPsec the same as a VPN?
No. VPN is the goal, a private path over a public network. IPsec is one way to build it. SSL/TLS, WireGuard, and older protocols like PPTP are other ways.
What ports does an IPsec VPN use?
IKE negotiates on UDP 500. When either side is behind NAT, both IKE and the encrypted data move to UDP 4500 (NAT-T). Without NAT, ESP travels as IP protocol 50, which has no port at all.
Which is more secure, IPsec or SSL VPN?
Configured well, both are secure. IPsec protects everything at the IP layer; SSL VPN is easier to expose accidentally because it lives on TCP 443 next to web traffic.
What is the difference between IKEv1 and IKEv2?
IKEv2 is faster to negotiate, has built-in NAT traversal and dead peer detection, supports MOBIKE for roaming clients, and is the version you should deploy today.
Does IPsec slow down the connection?
Encryption adds a few percent of overhead. On modern firewalls with AES-NI the tunnel usually saturates the WAN link before the CPU becomes a limit. MTU is the more common cause of slowness.
Can I run IPsec through a NAT router?
Yes, with NAT-T. The gateways detect the NAT during IKE and encapsulate ESP inside UDP 4500 so the router can track it like any other UDP flow.
Keep readingRelated concepts
Read next · Identity and access What Is MFA? The tunnel proves the gateway. Authentication proves the person behind it. Open this next16 min- Addressing · 15 min What Is a Subnet? The tunnel joins two address ranges, so the selectors that decide what enters it are subnets.
- Remote access · 13 min VPN vs Proxy The other thing people mean by VPN, and how it differs from a proxy.
- Remote access · 12 min VPN Technologies, and Which One Belongs on Which Job The protocol that owns the first job, in detail.
- Protocols · 10 min TCP vs UDP Why the tunnel uses UDP, and what happens when it does not.
- Remote access · 14 min WireGuard Explained The modern alternative, with one cipher and no negotiation at all.
- Infrastructure · 11 min MPLS Explained The private carrier network people often assume removes the need for one of these.
- Cryptography · 12 min Diffie-Hellman Key Exchange One of the places this exchange runs, and how the tunnel authenticates on top of it.
- Protocols · 11 min What Is ICMP? The protocol whose error messages keep a tunnel working once it is up.
- Remote access · 13 min Split Tunneling, and the Trade It Actually Makes The other side of the decision, and how the tunnel it builds is configured.
- Remote access · 11 min SSL VPN Explained, and the Reason It Needs Patching First The other remote access protocol, and where it remains the standard.
- Remote access · 11 min NAT Traversal, and Why One Way Audio Is Always the Same Bug The other thing NAT breaks, and how that one was solved.
- Diagnostics · 12 min MTU and Fragmentation, and the Numbers Worth Memorizing The tunnel that most often triggers the problem.
- Remote access · 10 min GRE vs IPsec, and the Reason They Are Normally Used Together The security half of this pairing, in full.
- Design · 9 min Hub and Spoke Topology, and the Traffic That Goes the Long Way What the links in this topology usually are, in practice.
- Infrastructure · 8 min Cisco SD-WAN, and Why the Controller Never Touches Your Traffic Cisco SD-WAN, which builds these tunnels automatically between every pair of sites a policy allows.