How to Set Up an IKEv2 VPN Server with StrongSwan on Ubuntu 16.04

Published on February 17, 2017
How to Set Up an IKEv2 VPN Server with StrongSwan on Ubuntu 16.04
Not using Ubuntu 16.04?Choose a different version or distribution.
Ubuntu 16.04


A virtual private network, or VPN, allows you to securely encrypt traffic as it travels through untrusted networks, such as those at the coffee shop, a conference, or an airport.

IKEv2, or Internet Key Exchange v2, is a protocol that allows for direct IPSec tunneling between the server and client. In IKEv2 VPN implementations, IPSec provides encryption for the network traffic. IKEv2 is natively supported on new platforms (OS X 10.11+, iOS 9.1+, and Windows 10) with no additional applications necessary, and it handles client hiccups quite smoothly.

In this tutorial, you’ll set up an IKEv2 VPN server using StrongSwan on an Ubuntu 16.04 server and connect to it from Windows, iOS, and macOS clients.


To complete this tutorial, you will need:

In addition, you should be familiar with IPTables. Review How the Iptables Firewall Works before you proceed.

Step 1 — Installing StrongSwan

First, we’ll install StrongSwan, an open-source IPSec daemon which we’ll configure as our VPN server. We’ll also install the StrongSwan EAP plugin, which allows password authentication for clients, as opposed to certificate-based authentication. We’ll need to create some special firewall rules as part of this configuration, so we’ll also install a utility which allows us to make our new firewall rules persistent.

Execute the following command to install these components:

  1. sudo apt-get install strongswan strongswan-plugin-eap-mschapv2 moreutils iptables-persistent

Note: While installing iptables-persistent, the installer will ask whether or not to save current IPv4 and IPv6 rules. As we want any previous firewall configurations to stay the same, we’ll select yes on both prompts.

Now that everything’s installed, let’s move on to creating our certificates:

Step 2 — Creating a Certificate Authority

An IKEv2 server requires a certificate to identify itself to clients. To help us create the certificate required, StrongSwan comes with a utility to generate a certificate authority and server certificates. To begin, let’s create a directory to store all the stuff we’ll be working on.

  1. mkdir vpn-certs
  2. cd vpn-certs

Now that we have a directory to store everything, let’s generate our root key. This will be a 4096-bit RSA key that will be used to sign our root certificate authority, so it’s very important that we also secure this key by ensuring that only the root user can read it.

Execute these commands to generate and secure the key:

  1. ipsec pki --gen --type rsa --size 4096 --outform pem > server-root-key.pem
  2. chmod 600 server-root-key.pem

Now that we have a key, we can move on to creating our root certificate authority, using the key to sign the root certificate:

  1. ipsec pki --self --ca --lifetime 3650 \
  2. --in server-root-key.pem \
  3. --type rsa --dn "C=US, O=VPN Server, CN=VPN Server Root CA" \
  4. --outform pem > server-root-ca.pem

You can change the distinguished name (DN) values, such as country, organization, and common name, to something else to if you want to. The common name here is just the indicator, so you could even make something up.

Later, we’ll copy the root certificate (server-root-ca.pem) to our client devices so they can verify the authenticity of the server when they connect.

Now that we’ve got our root certificate authority up and running, we can create a certificate that the VPN server will use.

Step 3 — Generating a Certificate for the VPN Server

We’ll now create a certificate and key for the VPN server. This certificate will allow the client to verify the server’s authenticity.

First, create a private key for the VPN server with the following command:

  1. ipsec pki --gen --type rsa --size 4096 --outform pem > vpn-server-key.pem

Then create and sign the VPN server certificate with the certificate authority’s key you created in the previous step. Execute the following command, but change the Common Name (CN) and the Subject Alternate Name (SAN) field to your VPN server’s DNS name or IP address:

  1. ipsec pki --pub --in vpn-server-key.pem \
  2. --type rsa | ipsec pki --issue --lifetime 1825 \
  3. --cacert server-root-ca.pem \
  4. --cakey server-root-key.pem \
  5. --dn "C=US, O=VPN Server, CN=server_name_or_ip" \
  6. --san server_name_or_ip \
  7. --flag serverAuth --flag ikeIntermediate \
  8. --outform pem > vpn-server-cert.pem

Copy the certificates to a path which would allow StrongSwan to read the certificates:

  1. sudo cp ./vpn-server-cert.pem /etc/ipsec.d/certs/vpn-server-cert.pem
  2. sudo cp ./vpn-server-key.pem /etc/ipsec.d/private/vpn-server-key.pem

Finally, secure the keys so they can only be read by the root user.

  1. sudo chown root /etc/ipsec.d/private/vpn-server-key.pem
  2. sudo chgrp root /etc/ipsec.d/private/vpn-server-key.pem
  3. sudo chmod 600 /etc/ipsec.d/private/vpn-server-key.pem

In this step, we’ve created a certificate pair that would be used to secure communications between the client and the server. We’ve also signed the certificates with our root key, so the client will be able to verify the authenticity of the VPN server. Now that we’ve got all the certificates ready, we’ll move on to configuring the software.

Step 4 — Configuring StrongSwan

We’ve already created all the certificates that we need, so it’s time to configure StrongSwan itself.

StrongSwan has a default configuration file, but before we make any changes, let’s back it up first so that we’ll have a reference file just in case something goes wrong:

  1. sudo cp /etc/ipsec.conf /etc/ipsec.conf.original

The example file is quite long, so to prevent misconfiguration, we’ll clear the default configuration file and write our own configuration from scratch. First, clear out the original configuration:

  1. echo '' | sudo tee /etc/ipsec.conf

Then open the file in your text editor:

  1. sudo nano /etc/ipsec.conf

First, we’ll tell StrongSwan to log daemon statuses for debugging and allow duplicate connections. Add these lines to the file:

config setup
  charondebug="ike 1, knl 1, cfg 0"

Then, we’ll create a configuration section for our VPN. We’ll also tell StrongSwan to create IKEv2 VPN Tunnels and to automatically load this configuration section when it starts up. Append the following lines to the file:

conn ikev2-vpn

Next, we’ll tell StrongSwan which encryption algorithms to use for the VPN. Append these lines:


We’ll also configure dead-peer detection to clear any “dangling” connections in case the client unexpectedly disconnects. Add these lines:


Then we’ll configure the server (left) side IPSec parameters. Add this to the file:


Note: When configuring the server ID (leftid), only include the @ character if your VPN server will be identified by a domain name:


If the server will be identified by its IP address, just put the IP address in:


Then we configure the client (right) side IPSec parameters, like the private IP address ranges and DNS servers to use:


Finally, we’ll tell StrongSwan to ask the client for user credentials when they connect:


The configuration file should look like this:

config setup
    charondebug="ike 1, knl 1, cfg 0"

conn ikev2-vpn

Save and close the file once you’ve verified that you’ve configured things as shown.

Now that we’ve configured the VPN parameters, let’s move on to creating an account so our users can connect to the server.

Step 5 — Configuring VPN Authentication

Our VPN server is now configured to accept client connections, but we don’t have any credentials configured yet, so we’ll need to configure a couple things in a special configuration file called ipsec.secrets:

  • We need to tell StrongSwan where to find the private key for our server certificate, so the server will be able to encrypt and decrypt data.
  • We also need to set up a list of users that will be allowed to connect to the VPN.

Let’s open the secrets file for editing:

  1. sudo nano /etc/ipsec.secrets

First, we’ll tell StrongSwan where to find our private key.

server_name_or_ip : RSA "/etc/ipsec.d/private/vpn-server-key.pem"

Then we’ll create the user credentials. You can make up any username or password combination that you like, but we have to tell StrongSwan to allow this user to connect from anywhere:

your_username %any% : EAP "your_password"

Save and close the file. Now that we’ve finished working with the VPN parameters, we’ll reload the VPN service so that our configuration would be applied:

  1. sudo ipsec reload

Now that the VPN server has been fully configured with both server options and user credentials, it’s time to move on to configuring the most important part: the firewall.

Step 6 — Configuring the Firewall & Kernel IP Forwarding

Now that we’ve got the VPN server configured, we need to configure the firewall to forward and allow VPN traffic through. We’ll use IPTables for this.

First, disable UFW if you’ve set it up, as it can conflict with the rules we need to configure:

  1. sudo ufw disable

Then remove any remaining firewall rules created by UFW:

  1. iptables -P INPUT ACCEPT
  2. iptables -P FORWARD ACCEPT
  3. iptables -F
  4. iptables -Z

To prevent us from being locked out of the SSH session, we’ll accept connections that are already accepted. We’ll also open port 22 (or whichever port you’ve configured) for future SSH connections to the server. Execute these commands:

  1. sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  2. sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

We’ll also need to accept connections on the local loopback interface:

  1. sudo iptables -A INPUT -i lo -j ACCEPT

Then we’ll tell IPTables to accept IPSec connections:

  1. sudo iptables -A INPUT -p udp --dport 500 -j ACCEPT
  2. sudo iptables -A INPUT -p udp --dport 4500 -j ACCEPT

Next, we’ll tell IPTables to forward ESP (Encapsulating Security Payload) traffic so the VPN clients will be able to connect. ESP provides additional security for our VPN packets as they’re traversing untrusted networks:

  1. sudo iptables -A FORWARD --match policy --pol ipsec --dir in --proto esp -s -j ACCEPT
  2. sudo iptables -A FORWARD --match policy --pol ipsec --dir out --proto esp -d -j ACCEPT

Our VPN server will act as a gateway between the VPN clients and the internet. Since the VPN server will only have a single public IP address, we will need to configure masquerading to allow the server to request data from the internet on behalf of the clients; this will allow traffic to flow from the VPN clients to the internet, and vice-versa:

  1. sudo iptables -t nat -A POSTROUTING -s -o eth0 -m policy --pol ipsec --dir out -j ACCEPT
  2. sudo iptables -t nat -A POSTROUTING -s -o eth0 -j MASQUERADE

To prevent IP packet fragmentation on some clients, we’ll tell IPTables to reduce the size of packets by adjusting the packets’ maximum segment size. This prevents issues with some VPN clients.

  1. sudo iptables -t mangle -A FORWARD --match policy --pol ipsec --dir in -s -o eth0 -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1361:1536 -j TCPMSS --set-mss 1360

For better security, we’ll drop everything else that does not match the rules we’ve configured:

  1. sudo iptables -A INPUT -j DROP
  2. sudo iptables -A FORWARD -j DROP

Now we’ll make the firewall configuration persistent, so that all our configuration work won’t get wiped on reboot:

  1. sudo netfilter-persistent save
  2. sudo netfilter-persistent reload

Finally, we’ll enable packet forwarding on the server. Packet forwarding is what makes it possible for our server to “route” data from one IP address to the other. Essentially, we’re making our server act like a router.

Edit the file /etc/sysctl.conf:

  1. sudo nano /etc/sysctl.conf

We’ll need to configure a few things here:

  • First, we’ll enable IPv4 packet forwarding.
  • We’ll disable Path MTU discovery to prevent packet fragmentation problems.
  • We also won’t accept ICMP redirects nor send ICMP redirects to prevent man-in-the-middle attacks.

The changes you need to make to the file are highlighted in the following code:


. . .

# Uncomment the next line to enable packet forwarding for IPv4

. . .

# Do not accept ICMP redirects (prevent MITM attacks)
net.ipv4.conf.all.accept_redirects = 0
# Do not send ICMP redirects (we are not a router)
net.ipv4.conf.all.send_redirects = 0

. . .

net.ipv4.ip_no_pmtu_disc = 1

Make those changes, save the file, and exit the editor. Then restart the server:

  1. sudo reboot

You’ll get disconnected from the server as it reboots, but that’s expected. After the server reboots, log back in to the server as the sudo, non-root user. You’re ready to test the connection on a client.

Step 7 – Testing the VPN Connection on Windows, iOS, and macOS

Now that you have everything set up, it’s time to try it out. First, you’ll need to copy the root certificate you created and install it on your client device(s) that will connect to the VPN. The easiest way to do this is to log into your server and execute this command to display the contents of the certificate file:

  1. cat ~/vpn-certs/server-root-ca.pem

You’ll see output similar to this:


Copy this output to your computer, including the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- lines, and save it to a file with a recognizable name, such as vpn_root_certificate.pem. Ensure the file you create has the .pem extension.

Alternatively, use SFTP to transfer the file to your computer.

Once you have the vpn_root_certificate.pem file downloaded to your computer, you can set up the connection to the VPN.

Connecting from Windows

First, import the root certificate by following these steps:

  1. Press WINDOWS+R to bring up the Run dialog, and enter mmc.exe to launch the Windows Management Console.

  2. From the File menu, navigate to Add or Remove Snap-in, select Certificates from the list of available snap-ins, and click Add.

  3. We want the VPN to work with any user, so select Computer Account and click Next.

  4. We’re configuring things on the local computer, so select Local Computer, then click Finish.

  5. Under the Console Root node, expand the Certificates (Local Computer) entry, expand Trusted Root Certification Authorities, and then select the Certificates entry: Certificates view

  6. From the Action menu, select All Tasks and click Import to display the Certificate Import Wizard. Click Next to move past the introduction.

  7. On the File to Import screen, press the Browse button and select the certificate file that you’ve saved. Then click Next.

  8. Ensure that the Certificate Store is set to Trusted Root Certification Authorities, and click Next.

  9. Click Finish to import the certificate.

Then configure the VPN with these steps:

  1. Launch Control Panel, then navigate to the Network and Sharing Center.
  2. Click on Set up a new connection or network, then select Connect to a workplace.
  3. Select Use my Internet connection (VPN).
  4. Enter the VPN server details. Enter the server’s domain name or IP address in the Internet address field, then fill in Destination name with something that describes your VPN connection. Then click Done.

Your new VPN connection will be visible under the list of networks. Select the VPN and click Connect. You’ll be prompted for your username and password. Type them in, click OK, and you’ll be connected.

Connecting from iOS

To configure the VPN connection on an iOS device, follow these steps:

  1. Send yourself an email with the root certificate attached.
  2. Open the email on your iOS device and tap on the attached certificate file, then tap Install and enter your passcode. Once it installs, tap Done.
  3. Go to Settings, General, VPN and tap Add VPN Configuration. This will bring up the VPN connection configuration screen.
  4. Tap on Type and select IKEv2.
  5. In the Description field, enter a short name for the VPN connection. This could be anything you like.
  6. In the Server and Remote ID field, enter the server’s domain name or IP address. The Local ID field can be left blank.
  7. Enter your username and password in the Authentication section, then tap Done.
  8. Select the VPN connection that you just created, tap the switch on the top of the page, and you’ll be connected.

Connecting from macOS

Follow these steps to import the certificate:

  1. Double-click the certificate file. Keychain Access will pop up with a dialog that says “Keychain Access is trying to modify the system keychain. Enter your password to allow this.”
  2. Enter your password, then click on Modify Keychain
  3. Double-click the newly imported VPN certificate. This brings up a small properties window where you can specify the trust levels. Set IP Security (IPSec) to Always Trust and you’ll be prompted for your password again. This setting saves automatically after entering the password.

Now that the certificate is important and trusted, configure the VPN connection with these steps:

  1. Go to System Preferences and choose Network.
  2. Click on the small “plus” button on the lower-left of the list of networks.
  3. In the popup that appears, Set Interface to VPN, set the VPN Type to IKEv2, and give the connection a name.
  4. In the Server and Remote ID field, enter the server’s domain name or IP address. Leave the Local ID blank.
  5. Click on Authentication Settings, select Username, and enter your username and password you configured for your VPN user. Then click OK.

Finally, click on Connect to connect to the VPN. You should now be connected to the VPN.

Troubleshooting Connections

If you are unable to import the certificate, ensure the file has the .pem extention, and not .pem.txt.

If you’re unable to connect to the VPN, check the server name or IP address you used. The server’s domain name or IP address must match what you’ve configured as the common name (CN) while creating the certificate. If they don’t match, the VPN connection won’t work. If you set up a certificate with the CN of vpn.example.com, you must use vpn.example.com when you enter the VPN server details. Double-check the command you used to generate the certificate, and the values you used when creating your VPN connection.

Finally, double-check the VPN configuration to ensure the leftid value is configured with the @ symbol if you’re using a domain name:


And if you’re using an IP address, ensure that the @ symbol is omitted.


In this tutorial, you’ve built a VPN server that uses the IKEv2 protocol. Now you can be assured that your online activities will remain secure wherever you go!

To add or remove users, just take a look at Step 5 again. Each line is for one user, so adding or removing users is as simple as editing the file.

From here, you might want to look into setting up a log file analyzer, because StrongSwan dumps its logs into syslog. The tutorial  How To Install and Use Logwatch Log Analyzer and Reporter on a VPS has more information on setting that up.

You might also be interested in this guide from the EFF about online privacy.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Five times I install this truly won’t working. Don’t waste your time with this tutorial.

$ sudo apt-get install strongswan strongswan-plugin-eap-mschapv2 moreutils iptables-persistent Reading package lists… Done Building dependency tree
Reading state information… Done E: Unable to locate package moreutils E: Unable to locate package iptables-persistent

What I should do next ?

Is there a similar guide where LetsEncrypt certificate is used instead of a self-signed one?

Sounds good, doesn’t work.

This comment has been deleted

    Hi I did try with this tutorial but no luck nothing is working for me in ubuntu it is not showing any error two times formatted server to start from scratch but no luck what I am missing don’t know spent a lot of my time but not succeed. Can someone help me to configure it out?

    I can connect to the VPN i set up,but i can’t connect to internet when I connected to my VPN,could you tell me what is wrong?

    I’m trying to build a .mobileconfig file to put on my iphone for this setup and enable on demand connections – like this:


    But I can’t seem to get it to work. Installing the profile gives me various errors. Can anyone help me build a valid .mobileconfig file that works for this setup?


    Any chances to have it using (instead of disabling) ufw?

    This was really helpful but one problem is the security is configured for iOS however on Android which uses StrongSwan, you need to have a higher level of security. Change the ipsec.conf file to use the following:

    ike=aes256gcm16-sha256-ecp521,aes256-sha256-ecp384!,aes256-sha1-modp1024,3des-sha1-modp1024! esp=aes256gcm16-sha256!,aes256-sha1,3des-sha1!

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Featured on Community

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel