How to install IPSEC IKEv2 vpn server on CentOS 7 linux

As we are going through demonstrating vpn technologies, we reach to show how to install IPSEC IKEv2 vpn server on CentOS 7 linux.
IKE stands for Internet Key Exchange and is used for security association phase of IPSEC protocol to transfer security attributes either by using preshared key or digital certificate.

Here is our environment:

OS: CentOS 7 on VMWare
Firewall: firewalld
SELinux: enforcing
Server IP address:
Client IP address:

1- Install libreswan

To install IPSEC IKEv2, we should install libreswan package:

# yum install libreswan

to use cryptographic functions during IKE negotiation, libreswan uses NSS (Network Security Services) database. so we create a new sqlite database for it:

# ipsec initnss

2- Generate Certificates

to encrypt traffic and authenticate users, we should generate three type of certificates. a CA, a server and a client certificate. Here we generate CA certificate:

# certutil -S -x -n "ExampleCA" -s "O=Example,CN=My CA" -k rsa -v 120 -d sql:/etc/ipsec.d -t "CT,," -2

to create private key, you should type randomly on keyboard until it gets completed.
When you got a message to say the process is finished, press Enter.
then if this is a CA certificate, type y for yes.
after that if it asked for a path length constraint, press Enter.
finally when asked if this is a critical extension, type n for no.
Then we must create server certificate. this certificate will be signed by CA certificate that we previously have generated:

# certutil -S -c "ExampleCA" -n "" -s "O=Example,CN=" -k rsa -v 12 -d sql:/etc/ipsec.d -t ",," -1 -6 -8 ""

Note: Remember to change IP address with your own.
Again, you must do some random typing until the process is finished, at which point you press Enter.
now the process will ask you to do some choose, select choices as below:
0 – Digital signature
2 – Key encipherment
8 – end
When asked if this is a critical extension, type n for no.
when it asked you about extended key usage, select the following in turn:
0 – Server auth
1 – Client auth
8 – end
When asked if this is a critical extension, type n for no.
The last certificate that we must generate is client certificate. here we generate it for client1. do it as follow:

# certutil -S -c "ExampleCA" -n "" -s "O=Example,CN=" -k rsa -v 12 -d sql:/etc/ipsec.d -t ",," -1 -6 -8 ""

Note: you can change “” with IP address of your client.
same as previous process, you must do random typing until finishing the process.
Enter the key usage and extended key usage choices the same as you did for the server certificate.

3- Transfer certificates to client

to transfer generated certificates to clients, first we create a p12 certificate that contains client certificate, client private key, and CA certificate.

# pk12util -o ~/client1.p12 -n "" -d sql:/etc/ipsec.d

change the permission to allow transfer it to clients:

# chmod +r ~/client1.p12

Leave the password and password confirmation blank.
Then copy client1.p12 to your client.

4- Create configuration

put the following content in /etc/ipsec.d/ike.conf:

# vim /etc/ipsec.d/ike.conf
conn ike
        [email protected]

Note1: Mind SPACES in above configuration

Note2: Remember to change “” and “leftcert” with your own IP address and certificate name.

5- Start IPsec Service

now we start ipsec service and check if it has started properly:

# systemctl enable ipsec
# systemctl start ipsec
# systemctl status ipsec

6- Configure Firewall

Here we use firewalld service. we need to allow ipsec, forward internal l2tpc ip range traffic to internet interface and do NAT:

# firewall-cmd --permanent --zone=public --add-masquerade
# firewall-cmd --permanent --add-rich-rule='rule protocol value="esp" accept'
# firewall-cmd --permanent --add-rich-rule='rule protocol value="ah" accept'
# firewall-cmd --permanent --add-port=500/udp 
# firewall-cmd --permanent --add-port=4500/udp 
# firewall-cmd --permanent --add-service="ipsec"
# firewall-cmd --reload

if you use CSF, create a file named /etc/CSF/ and put these lines in it:

# iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
# iptables -A FORWARD -s -j ACCEPT
# iptables -t nat -A POSTROUTING -s -o eno16777728 -j MASQUERADE

then reload CSF.
if you use iptables, just run above command and then save changes with iptables-save.
Note: remember to change eno16777728 with your own interface name

6- Configure SElinux

we prefer to keep selinux enabled. so to allow bringing up vpn interfaces, we should run the following command:

# setsebool -P daemons_use_tty 1

if you have disabled SELinux, simply skip above command.
Finally, consult your client OS (windows, linux, Mac, android,iOS, etc…) to know how to import downloaded certificate and then create a vpn connection with IKE/IKEv2 type.