ITNuvoAkademiya
Academy homePackagesPlaylistsToolsSubnet calculatorSearchGlossary
CiscoCCNA · CCNA: networking fundamentals1. Introduction2. Network Fundamentals3. Network Access4. IP Connectivity5. IP Services6. IPv67. Security Fundamentals8. AutomationCCNP: Enterprise networks · coming soon
LinuxLinux: system administrator üçün · Linux for system administrators1. Getting to know Linux2. Working with text3. Users and permissions4. Processes and packages5. Inside the system6. Monitoring and logs7. NetworkingRed Hat Enterprise Linux · Red Hat Enterprise Linux: from beginner to advanced1. Getting started2. Packages3. Users and permissions4. SELinux5. Services6. Storage7. Networking8. Building services9. Boot and recovery10. DevOps11. Security and monitoring
FortinetFortiGate Administrator · FortiGate Administrator: from first login to ZTNA1. Getting to know FortiGate2. Routing, policy and NAT3. Security profiles4. Users and logs5. Resilience, SD-WAN and automation6. VDOM7. VPN and ZTNA8. Proxy9. DMZ
MonitoringZabbix · Zabbix: from installation to alerts1. Introduction2. Installation and the database3. Basics: groups, templates, users4. Connecting devices5. Alerts, Grafana and the API
VirtualizationProxmox VE · coming soonVMware ESXi · coming soon
My account / Sign in
AZENRU
← itnuvo.com

IPsec basics: IKE, phase 1 and phase 2

Lesson 7.110 minAdvancedNovember 24, 2026
Open

This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.

Key points

  • IPsec encrypts traffic (confidentiality), checks that it was not altered (integrity) and confirms who the other side is (authentication).
  • IKE is the protocol that negotiates the tunnel's keys (UDP 500, or UDP 4500 behind NAT). The data itself is carried by ESP.
  • Phase 1 builds a trusted control channel between two devices: the sides identify each other and agree on a key. Phase 2 defines the encryption keys for real traffic and which networks go through the tunnel.
  • Both sides must have the same IKE version, proposal (encryption and hash), DH group, authentication (PSK or certificate) and phase 2 selectors.
  • On a FortiGate you build a route-based VPN: the tunnel appears as a virtual interface and traffic is steered to it with an ordinary route and policy.

Why IPsec?

The head office and a branch communicate over the internet. The internet is an open network: someone on the way could read the packets, change them or pretend to be the branch.

IPsec solves all three:

RequirementWhat IPsec doesWith what
ConfidentialityEncrypts the inside of the packetAES
IntegrityChecks that the packet was not changed on the waySHA hash
AuthenticationConfirms that the other side really is who it claimsPre-shared key or certificate

As a result the private networks of the two offices (10.10.10.0/24 and 10.20.0.0/24) see each other directly, as if a cable ran between them.

The protocols

ProtocolPortWhat it does
IKEUDP 500Identifies the sides and negotiates keys
IKE NAT-TUDP 4500If one side is behind NAT, IKE and ESP move to this port
ESPIP protocol 50Carries the encrypted data
AHIP protocol 51Integrity only, no encryption. Hardly ever used

If the provider or a firewall in between blocks UDP 500, UDP 4500 and ESP, the tunnel is not established.

Two phases

FGT-HQ203.0.113.2 FGT-BR198.51.100.2 Phase 1 · IKE SAwho are you? let us agree on a key · control channel Phase 2 · IPsec SAwhich networks? · data travels in ESP
Figure 1 · Phase 1 first builds a secure channel; inside it, phase 2 negotiates the keys for real traffic

Phase 1 answers the questions "who are you?" and "how do we talk to each other securely?" The result is an IKE SA (security association): an encrypted control channel between the two devices. No user traffic passes through this channel yet.

Phase 2 answers, inside that channel, the questions "which traffic will be encrypted?" and "with which key?" The result is an IPsec SA, and data starts to flow in ESP. In fact there is one SA per direction, so two.

The parameters of phase 1

ParameterMeaningRecommendation
IKE version1 or 22
Remote gatewayThe public IP of the other side
AuthenticationPre-shared key (PSK) or certificateA long random PSK, or a certificate
ProposalEncryption + hashaes256-sha256
DH groupThe strength of the key exchange14 or higher (19, 20, 21)
Key lifetimeHow often the key is renewed86400 seconds (default)
Mode (IKEv1 only)Main or aggressiveMain

Diffie-Hellman (DH) lets two sides create a shared secret key over an open channel: the key itself never crosses the network. The group number shows the mathematical strength. Groups 1, 2 and 5 are weak; do not use them.

IKEv1 and IKEv2

IKEv1IKEv2
Number of messagesMain mode 6, aggressive 3, then 3 for phase 24 in total
NAT traversalAs an add-onBuilt in
Remote-access authenticationXAuth (an add-on)EAP (built in)
Stability, reconnectionWeakBetter

Choose IKEv2 for new tunnels. IKEv1 is needed only when an old device on the other side supports nothing else. In IKEv1, aggressive mode sends the identity unencrypted and is less secure.

The parameters of phase 2

ParameterMeaning
ProposalEncryption and hash of the data (aes256-sha256)
PFS (perfect forward secrecy)A fresh DH exchange for phase 2. If one key leaks, earlier traffic cannot be opened. Keep it on
SelectorsThe local and remote network that go through the tunnel
Key lifetime43200 seconds (default)

The selector is where most mistakes are made. If HQ has "local 10.10.10.0/24, remote 10.20.0.0/24", BR must have the exact mirror: "local 10.20.0.0/24, remote 10.10.10.0/24". If they do not match, phase 1 comes up and phase 2 does not.

Between two FortiGates you can also leave the selectors at 0.0.0.0/0 and control which traffic passes with routes and policies. When working with another vendor's device, however, the selectors must match exactly.

What must match

PhaseMust be the same on both sides
Phase 1IKE version, proposal, DH group, PSK, (in IKEv1) the mode
Phase 2Proposal, PFS and DH group, selectors (mirrored)

The lifetime may differ: the sides take the smaller one.

Route-based VPN

On a FortiGate a tunnel creates a virtual interface (named after phase 1). From then on the tunnel is like an ordinary port:

  1. A route: remote network → tunnel interface.
  2. A policy: LAN port → tunnel interface (and the reverse direction).

The advantages: you can put the tunnel in a zone or in SD-WAN, run OSPF over it, and follow it with the sniffer and debug flow.

The old method, policy-based VPN, defined the tunnel inside a policy with action ipsec. It is not used in new deployments.

Topologies

TypeDescriptionLesson
Site-to-siteTwo offices, both with a fixed address37
Hub-and-spokeAll branches connect to the head office
Dial-up (remote access)A staff laptop connects with FortiClient; its address is not known in advance38

Extra mechanisms

  • NAT traversal: if one side is behind NAT, ESP packets are wrapped in UDP 4500. It is on by default.
  • DPD (dead peer detection): checks whether the other side still answers. If not, the tunnel is torn down and a new attempt is made.
  • Auto-negotiate: brings phase 2 up by itself without waiting for traffic.

A phase 1 example

FGT-HQ
FGT-HQ # config vpn ipsec phase1-interface
FGT-HQ (phase1-interface) # edit "to-BR"
FGT-HQ (to-BR) # set interface "port1"
FGT-HQ (to-BR) # set ike-version 2
FGT-HQ (to-BR) # set peertype any
FGT-HQ (to-BR) # set proposal aes256-sha256
FGT-HQ (to-BR) # set dhgrp 14
FGT-HQ (to-BR) # set remote-gw 198.51.100.2
FGT-HQ (to-BR) # set psksecret <key>
FGT-HQ (to-BR) # next
FGT-HQ (phase1-interface) # end

In the next lesson we complete this with phase 2, a route and policies and build a working tunnel.

Watch out on a real device: a pre-shared key that is short or a dictionary word is the weakest link of IPsec. Make the PSK a random string of at least 20 characters and use a different one for each tunnel. Do not send it openly by email. In a network with many tunnels, certificate authentication is more reliable.

🧪 Hands-on lab · Device: FortiGate-VM

IPsec phase 1

Create phase 1 for a tunnel to the branch (public address 198.51.100.2). Name to-BR, outgoing port port1, IKEv2, proposal aes256-sha256, DH group 14, any pre-shared key. After next type show system interface: a new tunnel interface named after phase 1 has been created.

Checked:

  • to-BR: interface port1
  • IKEv2
  • remote-gw 198.51.100.2
  • proposal aes256-sha256
  • dhgrp 14
  • Tunnel interface to-BR

▶ Start the lab

Check yourself

5 questions · answers are shown instantly · 3 correct answers complete the lesson

1What is the result of phase 1?

Phase 1 identifies the sides and agrees on a key; no data passes yet.

2Which parameter defines which networks go through the tunnel?

Selectors name the local and remote network and must mirror each other on the two sides.

3One of the sides is behind NAT. Which port does IPsec move to?

NAT traversal wraps ESP packets in UDP 4500.

4Phase 1 is up but phase 2 is not. What is the most likely cause?

The PSK and gateway belong to phase 1; in phase 2 the selectors and proposal must match.

5In a route-based VPN on a FortiGate, how is traffic steered into the tunnel?

The tunnel appears as a virtual interface and needs a route and a policy like an ordinary port.
Was this lesson useful?

Comments

Sign in to comment.