{"id":47,"date":"2024-09-24T08:08:25","date_gmt":"2024-09-24T08:08:25","guid":{"rendered":"http:\/\/192.168.40.2\/?p=47"},"modified":"2025-07-24T08:51:02","modified_gmt":"2025-07-24T08:51:02","slug":"how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/allogman.com\/?p=47","title":{"rendered":"How to Set Up an IKEv2 VPN Server with StrongSwan on Ubuntu 20.04"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\" id=\"introduction\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#introduction\">Introduction<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#introduction\"><\/a><\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Internet_Key_Exchange\" rel=\"noreferrer noopener\" target=\"_blank\">Internet Key Exchange v2<\/a>, or IKEv2, 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 some platforms (OS X 10.11+, iOS 9.1+, and Windows 10) with no additional applications necessary, and it handles client hiccups quite smoothly.<\/p>\n\n\n\n<p>In this tutorial, you\u2019ll set up an IKEv2 VPN server using&nbsp;<a href=\"https:\/\/www.strongswan.org\/\" rel=\"noreferrer noopener\" target=\"_blank\">StrongSwan<\/a>&nbsp;on an Ubuntu 20.04 server. You\u2019ll then learn how to connect to it with Windows, macOS, Ubuntu, iOS, and Android clients.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#prerequisites\">Prerequisites<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#prerequisites\"><\/a><\/h2>\n\n\n\n<p>To complete this tutorial, you will need:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>One Ubuntu 20.04 server configured by following\u00a0<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/initial-server-setup-with-ubuntu-20-04\">the Ubuntu 20.04 initial server setup guide<\/a>, including a\u00a0<code>sudo<\/code>\u00a0non-root user and a firewall.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-1-installing-strongswan\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-1-installing-strongswan\">Step 1 \u2014 Installing StrongSwan<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-1-installing-strongswan\"><\/a><\/h2>\n\n\n\n<p>First, we\u2019ll install StrongSwan, an open-source IPSec daemon which we\u2019ll configure as our VPN server. We\u2019ll also install the public key infrastructure (PKI) component so that we can create a Certificate Authority (CA) to provide credentials for our infrastructure.<\/p>\n\n\n\n<p>Start by updating the local package cache:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\n<\/code><\/pre>\n\n\n\n<p>Then install the software by typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install strongswan strongswan-pki libcharon-extra-plugins libcharon-extauth-plugins libstrongswan-extra-plugins\n<\/code><\/pre>\n\n\n\n<p>The additional&nbsp;<code>libcharon-extauth-plugins<\/code>&nbsp;package is used to ensure that various clients can authenticate to your server using a shared username and passphrase. The&nbsp;<code>libstrongswan-extra-plugins<\/code>&nbsp;package is included so that Strongswan supports elliptic curve cipher suites that use the&nbsp;<code>Curve25519<\/code>&nbsp;cryptography suite.<\/p>\n\n\n\n<p>Now that everything\u2019s installed, let\u2019s move on to creating our certificates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-2-creating-a-certificate-authority\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-2-creating-a-certificate-authority\">Step 2 \u2014 Creating a Certificate Authority<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-2-creating-a-certificate-authority\"><\/a><\/h2>\n\n\n\n<p>An IKEv2 server requires a certificate to identify itself to clients. To help create the required certificate, the&nbsp;<code>strongswan-pki<\/code>&nbsp;package comes with a utility called&nbsp;<code>pki<\/code>&nbsp;to generate a Certificate Authority and server certificates.<\/p>\n\n\n\n<p>To begin, let\u2019s create a few directories to store all the assets we\u2019ll be working on. The directory structure matches some of the directories in&nbsp;<code>\/etc\/ipsec.d<\/code>, where we will eventually move all of the items we create:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir -p ~\/pki\/{cacerts,certs,private}\n<\/code><\/pre>\n\n\n\n<p>Then lock down the permissions so that our private files can\u2019t be seen by other users:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod 700 ~\/pki\n<\/code><\/pre>\n\n\n\n<p>Now that we have a directory structure to store everything, we can generate a root key. This will be a 4096-bit RSA key that will be used to sign our root certificate authority.<\/p>\n\n\n\n<p>Execute these commands to generate the key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pki --gen --type rsa --size 4096 --outform pem &gt; ~\/pki\/private\/ca-key.pem\n<\/code><\/pre>\n\n\n\n<p>Following that we can move on to creating our root certificate authority, using the key that we just generated to sign the root certificate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pki --self --ca --lifetime 3650 --in ~\/pki\/private\/ca-key.pem \\\n    --type rsa --dn \"CN=VPN root CA\" --outform pem &gt; ~\/pki\/cacerts\/ca-cert.pem\n<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>--lifetime 3650<\/code>&nbsp;flag is used to ensure that the certificate authority\u2019s root certificate will be valid for 10 years. The root certificate for an authority does not change typically, since it would have to be redistributed to every server and client that rely on it, so 10 years is a safe default expiry value.<\/p>\n\n\n\n<p>You can change the&nbsp;<em>distinguished name<\/em>&nbsp;(DN) value to something else if you would like. The common name (CN field) here is just the indicator, so it doesn\u2019t have to match anything in your infrastructure.<\/p>\n\n\n\n<p>Now that we\u2019ve got our root certificate authority up and running, we can create a certificate that the VPN server will use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-3-generating-a-certificate-for-the-vpn-server\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-3-generating-a-certificate-for-the-vpn-server\">Step 3 \u2014 Generating a Certificate for the VPN Server<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-3-generating-a-certificate-for-the-vpn-server\"><\/a><\/h2>\n\n\n\n<p>We\u2019ll now create a certificate and key for the VPN server. This certificate will allow the client to verify the server\u2019s authenticity using the CA certificate we just generated.<\/p>\n\n\n\n<p>First, create a private key for the VPN server with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pki --gen --type rsa --size 4096 --outform pem &gt; ~\/pki\/private\/server-key.pem\n<\/code><\/pre>\n\n\n\n<p>Now, create and sign the VPN server certificate with the certificate authority\u2019s 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\u2019s DNS name or IP address:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pki --pub --in ~\/pki\/private\/server-key.pem --type rsa \\\n    | pki --issue --lifetime 1825 \\\n        --cacert ~\/pki\/cacerts\/ca-cert.pem \\\n        --cakey ~\/pki\/private\/ca-key.pem \\\n        --dn \"CN=<mark>server_domain_or_IP<\/mark>\" --san <mark>server_domain_or_IP<\/mark> \\\n        --flag serverAuth --flag ikeIntermediate --outform pem \\\n    &gt;  ~\/pki\/certs\/server-cert.pem\n<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: If you are using an IP address instead of a DNS name, you will need to specify multiple&nbsp;<code>--san<\/code>&nbsp;entries. The line in the previous command block where you specify the distinguished name (<code>--dn ...<\/code>) will need to be modified with the extra entry like the following excerpted line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--dn \"CN=<mark>IP address<\/mark>\" --san @<mark>IP_address<\/mark> --san <mark>IP_address<\/mark> \\\n<\/code><\/pre>\n\n\n\n<p>The reason for this extra&nbsp;<code>--san @<mark>IP_address<\/mark><\/code>&nbsp;entry is that some clients will check whether the TLS certificate has both an DNS entry and an IP Address entry for a server when they verify its identity.<\/p>\n\n\n\n<p>The&nbsp;<code>--flag serverAuth<\/code>&nbsp;option is used to indicate that the certificate will be used explicitly for server authentication, before the encrypted tunnel is established. The&nbsp;<code>--flag ikeIntermediate<\/code>&nbsp;option is used to support older macOS clients.<\/p>\n\n\n\n<p>Now that we\u2019ve generated all of the TLS\/SSL files StrongSwan needs, we can move the files into place in the&nbsp;<code>\/etc\/ipsec.d<\/code>&nbsp;directory by typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo cp -r ~\/pki\/* \/etc\/ipsec.d\/\n<\/code><\/pre>\n\n\n\n<p>In this step, we\u2019ve created a certificate pair that will be used to secure communications between the client and the server. We\u2019ve also signed the certificates with the CA key, so the client will be able to verify the authenticity of the VPN server using the CA certificate. With all of these certificates ready, we\u2019ll move on to configuring the software.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-4-configuring-strongswan\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-4-configuring-strongswan\">Step 4 \u2014 Configuring StrongSwan<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-4-configuring-strongswan\"><\/a><\/h2>\n\n\n\n<p>StrongSwan has a default configuration file with some examples, but we will have to do most of the configuration ourselves. Let\u2019s back up the file for reference before starting from scratch:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mv \/etc\/ipsec.conf{,.original}\n<\/code><\/pre>\n\n\n\n<p>Create and open a new blank configuration file using your preferred text editor. Here, we\u2019ll use&nbsp;<code>nano<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/ipsec.conf\n<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: As you work through this section to configure the server portion of your VPN, you will encounter settings that refer to&nbsp;<em>left<\/em>&nbsp;and&nbsp;<em>right<\/em>&nbsp;sides of a connection. When working with IPSec VPNs, the&nbsp;<em>left<\/em>&nbsp;side by convention refers to the local system that you are configuring, in this case the server. The right side directives in these settings will refer to remote clients, like phones and other computers.<\/p>\n\n\n\n<p>When you move on to configuring clients later in this tutorial, the client configuration files will refer to themselves using various&nbsp;<em>left<\/em>&nbsp;directives, and the server will be referred to using&nbsp;<em>right<\/em>&nbsp;side terminology.<\/p>\n\n\n\n<p>First, we\u2019ll tell StrongSwan to log daemon statuses for debugging and allow duplicate connections. Add these lines to the file:<\/p>\n\n\n\n<p>\/etc\/ipsec.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>config setup\n    charondebug=\"ike 1, knl 1, cfg 0\"\n    uniqueids=no\n<\/code><\/pre>\n\n\n\n<p>Then, we\u2019ll create a configuration section for our VPN. We\u2019ll 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:<\/p>\n\n\n\n<p>\/etc\/ipsec.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. . .\nconn ikev2-vpn\n    auto=add\n    compress=no\n    type=tunnel\n    keyexchange=ikev2\n    fragmentation=yes\n    forceencaps=yes\n<\/code><\/pre>\n\n\n\n<p>We\u2019ll also configure dead-peer detection to clear any \u201cdangling\u201d connections in case the client unexpectedly disconnects. Add these lines:<\/p>\n\n\n\n<p>\/etc\/ipsec.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. . .\nconn ikev2-vpn\n    . . .\n    dpdaction=clear\n    dpddelay=300s\n    rekey=no\n<\/code><\/pre>\n\n\n\n<p>Next, we\u2019ll configure the server\u2019s \u201cleft\u201d side IPSec parameters. Each of the following parameters ensures that the server is configured to accept connections from clients and to identify itself correctly. You\u2019ll add each of these settings to the&nbsp;<code>\/etc\/ipsec.conf<\/code>&nbsp;file once you are familiar with what they are and why they are used:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>left=%any<\/code>\u00a0The\u00a0<code>%any<\/code>\u00a0value ensures that the server will use the network interface where it receives incoming connections for subsequent communication with clients. For example, if you are connecting a client over a private network, the server will use the private IP address where it receives traffic for the rest of the connection.<\/li>\n\n\n\n<li><code>leftid=<mark>@server_domain_or_IP<\/mark><\/code>\u00a0This option controls the name that the server presents to clients. When combined with the next option\u00a0<code>leftcert<\/code>, the\u00a0<code>leftid<\/code>\u00a0option ensures that the server\u2019s configured name and the Distinguished Name (DN) that is contained in the public certificate match.<\/li>\n\n\n\n<li><code>leftcert=server-cert.pem<\/code>\u00a0This option is the path to the public certificate for the server that you configured in Step 3. Without it, the server will not be able to authenticate itself with clients, or finish negotiating the IKEv2 set up.<\/li>\n\n\n\n<li><code>leftsendcert=always<\/code>\u00a0The\u00a0<code>always<\/code>\u00a0value ensures that any client that connects to the server will always receive a copy of the server\u2019s public certificate as part of the initial connection set up.<\/li>\n\n\n\n<li><code>leftsubnet=0.0.0.0\/0<\/code>\u00a0The last \u201cleft\u201d side option that you will add tells clients about the subnets that are reachable behind the server. In this case,\u00a0<code>0.0.0.0\/0<\/code>\u00a0is used to represent the entire set of IPv4 addresses, meaning that the server will tell clients to send all their traffic over the VPN by default.<\/li>\n<\/ul>\n\n\n\n<p>Now that you are familiar with each of the relevant \u201cleft\u201d side options, add them all to the file like this:<\/p>\n\n\n\n<p>\/etc\/ipsec.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. . .\nconn ikev2-vpn\n    . . .\n    left=%any\n    leftid=<mark>@server_domain_or_IP<\/mark>\n    leftcert=server-cert.pem\n    leftsendcert=always\n    leftsubnet=0.0.0.0\/0\n<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: When configuring the server ID (<code>leftid<\/code>), only include the&nbsp;<code>@<\/code>&nbsp;character if your VPN server will be identified by a domain name:<\/p>\n\n\n\n<p>\/etc\/ipsec.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    . . .    leftid=<mark>@vpn.example.com<\/mark>\n    . . .\n<\/code><\/pre>\n\n\n\n<p>If the server will be identified by its IP address, just put the IP address in:<\/p>\n\n\n\n<p>\/etc\/ipsec.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    . . .\n    leftid=<mark>your_server_ip<\/mark>\n    . . .\n<\/code><\/pre>\n\n\n\n<p>Next, we can configure the client\u2019s \u201cright\u201d side IPSec parameters. Each of the following parameters tells the server how to accept connections from clients, how clients should authenticate to the server, and the private IP address ranges and DNS servers that clients will use. Add each of these settings to the&nbsp;<code>\/etc\/ipsec.conf<\/code>&nbsp;file once you are familiar with what they are and why they are used:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>right=%any<\/code>\u00a0The\u00a0<code>%any<\/code>\u00a0option for the\u00a0<code>right<\/code>\u00a0side of the connection instructs the server to accept incoming connections from any remote client.<\/li>\n\n\n\n<li><code>rightid=%any<\/code>\u00a0This option ensures that the server will not reject connections from clients that provide an identity before the encrypted tunnel is established.<\/li>\n\n\n\n<li><code>rightauth=eap-mschapv2<\/code>\u00a0This option configures the authentication method that clients will use to authenticate to the server.\u00a0<code>eap-mschapv2<\/code>\u00a0is used here for broad compatibility to support clients like Windows, macOS, and Android devices.<\/li>\n\n\n\n<li><code>rightsourceip=10.10.10.0\/24<\/code>\u00a0This option instructs the server to assign private IP addresses to clients from the specified\u00a0<code>10.10.10.0\/24<\/code>\u00a0pool of IPs.<\/li>\n\n\n\n<li><code>rightdns=8.8.8.8,8.8.4.4<\/code>\u00a0These IP addresses are Google\u2019s public DNS resolvers. They can be changed to use other public resolvers, the VPN server\u2019s resolvers, or any other resolver that clients can reach.<\/li>\n\n\n\n<li><code>rightsendcert=never<\/code>\u00a0This option instructs the server that clients do not need to send a certificate to authenticate themselves.<\/li>\n<\/ul>\n\n\n\n<p>Now that you are familiar with the required \u201cright\u201d side options for the VPN, add the following lines to&nbsp;<code>\/etc\/ipsec.conf<\/code>:<\/p>\n\n\n\n<p>\/etc\/ipsec.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. . .\nconn ikev2-vpn\n    . . .\n    right=%any\n    rightid=%any\n    rightauth=eap-mschapv2\n    rightsourceip=10.10.10.0\/24\n    rightdns=8.8.8.8,8.8.4.4\n    rightsendcert=never\n<\/code><\/pre>\n\n\n\n<p>Now we\u2019ll tell StrongSwan to ask the client for user credentials when they connect:<\/p>\n\n\n\n<p>\/etc\/ipsec.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. . .\nconn ikev2-vpn\n    . . .\n    eap_identity=%identity\n<\/code><\/pre>\n\n\n\n<p>Finally, add the following lines to support Linux, Windows, macOS, iOS, and Android clients. These lines specify the various key exchange, hashing, authentication, and encryption algorithms (commonly referred to as&nbsp;<em>Cipher Suites<\/em>) that StrongSwan will allow different clients to use:<\/p>\n\n\n\n<p>\/etc\/ipsec.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. . .\nconn ikev2-vpn\n    . . .\n    ike=chacha20poly1305-sha512-curve25519-prfsha512,aes256gcm16-sha384-prfsha384-ecp384,aes256-sha1-modp1024,aes128-sha1-modp1024,3des-sha1-modp1024!\n    esp=chacha20poly1305-sha512,aes256gcm16-ecp384,aes256-sha256,aes256-sha1,3des-sha1!\n<\/code><\/pre>\n\n\n\n<p>Each supported cipher suite is delineated from the others by a comma. For example&nbsp;<code>chacha20poly1305-sha512-curve25519-prfsha512<\/code>&nbsp;is one suite, and&nbsp;<code>aes256gcm16-sha384-prfsha384-ecp384<\/code>&nbsp;is another. The cipher suites that are listed here are selected to ensure the widest range of compatibility across Windows, macOS, iOS, Android, and Linux clients.<\/p>\n\n\n\n<p>The complete configuration file should look like this:<\/p>\n\n\n\n<p>\/etc\/ipsec.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>config setup\n    charondebug=\"ike 1, knl 1, cfg 0\"\n    uniqueids=no\n\nconn ikev2-vpn\n    auto=add\n    compress=no\n    type=tunnel\n    keyexchange=ikev2\n    fragmentation=yes\n    forceencaps=yes\n    dpdaction=clear\n    dpddelay=300s\n    rekey=no\n    left=%any\n    leftid=<mark>@server_domain_or_IP<\/mark>\n    leftcert=server-cert.pem\n    leftsendcert=always\n    leftsubnet=0.0.0.0\/0\n    right=%any\n    rightid=%any\n    rightauth=eap-mschapv2\n    rightsourceip=10.10.10.0\/24\n    rightdns=8.8.8.8,8.8.4.4\n    rightsendcert=never\n    eap_identity=%identity\n    ike=chacha20poly1305-sha512-curve25519-prfsha512,aes256gcm16-sha384-prfsha384-ecp384,aes256-sha1-modp1024,aes128-sha1-modp1024,3des-sha1-modp1024!\n    esp=chacha20poly1305-sha512,aes256gcm16-ecp384,aes256-sha256,aes256-sha1,3des-sha1!\n<\/code><\/pre>\n\n\n\n<p>Save and close the file once you\u2019ve verified that you\u2019ve added each line correctly. If you used&nbsp;<code>nano<\/code>, do so by pressing&nbsp;<code>CTRL + X<\/code>,&nbsp;<code>Y<\/code>, then&nbsp;<code>ENTER<\/code>.<\/p>\n\n\n\n<p>Now that we\u2019ve configured the VPN parameters, let\u2019s move on to creating an account so our users can connect to the server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-5-configuring-vpn-authentication\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-5-configuring-vpn-authentication\">Step 5 \u2014 Configuring VPN Authentication<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-5-configuring-vpn-authentication\"><\/a><\/h2>\n\n\n\n<p>Our VPN server is now configured to accept client connections, but we don\u2019t have any credentials configured yet. We\u2019ll need to configure a couple things in a special configuration file called&nbsp;<code>ipsec.secrets<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We need to tell StrongSwan where to find the private key for our server certificate, so the server will be able to authenticate to clients.<\/li>\n\n\n\n<li>We also need to set up a list of users that will be allowed to connect to the VPN.<\/li>\n<\/ul>\n\n\n\n<p>Let\u2019s open the secrets file for editing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/ipsec.secrets\n<\/code><\/pre>\n\n\n\n<p>First, we\u2019ll tell StrongSwan where to find our private key and how to parse it.<\/p>\n\n\n\n<p>\/etc\/ipsec.secrets<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>: RSA \"server-key.pem\"\n<\/code><\/pre>\n\n\n\n<p>Make sure that the line begins with the&nbsp;<code>:<\/code>&nbsp;character and that there is a space after it so that the entire line reads&nbsp;<code>: RSA \"server-key.pem\"<\/code>.<\/p>\n\n\n\n<p>Then, we\u2019ll define the user credentials. You can make up any username or password combination that you like:<\/p>\n\n\n\n<p>\/etc\/ipsec.secrets<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark>your_username<\/mark> : EAP <mark>\"your_password\"<\/mark>\n<\/code><\/pre>\n\n\n\n<p>Save and close the file. Now that we\u2019ve finished working with the VPN parameters, we\u2019ll restart the VPN service so that our configuration is applied:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart strongswan-starter\n<\/code><\/pre>\n\n\n\n<p>Now that the VPN server has been fully configured with both server options and user credentials, it\u2019s time to move on to configuring the most important part: the firewall.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-6-configuring-the-firewall-kernel-ip-forwarding\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-6-configuring-the-firewall-kernel-ip-forwarding\">Step 6 \u2014 Configuring the Firewall &amp; Kernel IP Forwarding<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-6-configuring-the-firewall-kernel-ip-forwarding\"><\/a><\/h2>\n\n\n\n<p>With the StrongSwan configuration complete, we need to configure the firewall to allow VPN traffic through and forward it.<\/p>\n\n\n\n<p>If you followed the prerequisite initial server setup tutorial, you should have a UFW firewall enabled. If you don\u2019t yet have UFW configured, you should start by adding a rule to allow SSH connections through the firewall so your current session doesn\u2019t close when you enable UFW:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw allow OpenSSH\n<\/code><\/pre>\n\n\n\n<p>Then enable the firewall by typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw enable\n<\/code><\/pre>\n\n\n\n<p>Then, add a rule to allow UDP traffic to the standard IPSec ports,&nbsp;<code>500<\/code>&nbsp;and&nbsp;<code>4500<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw allow 500,4500\/udp\n<\/code><\/pre>\n\n\n\n<p>Next, we will open up one of UFW\u2019s configuration files to add a few low-level policies for routing and forwarding IPSec packets. Before we we can do this, though, we need to find which network interface on our server is used for internet access. Find this interface by querying for the device associated with the default route:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip route show default\n<\/code><\/pre>\n\n\n\n<p>Your public interface should follow the word \u201cdev\u201d. For example, this result shows the interface named&nbsp;<code>eth0<\/code>, which is highlighted in the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Outputdefault via <mark>your_server_ip<\/mark> dev <mark>eth0<\/mark> proto static\n<\/code><\/pre>\n\n\n\n<p>When you have your public network interface, open the&nbsp;<code>\/etc\/ufw\/before.rules<\/code>&nbsp;file in your text editor. The rules in this file are added to the firewall before the rest of the usual input and output rules. They are used to configure network address translation (NAT) so that the server can correctly route connections to and from clients and the Internet.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/ufw\/before.rules\n<\/code><\/pre>\n\n\n\n<p>Near the top of the file (before the&nbsp;<code>*filter<\/code>&nbsp;line), add the following configuration block. Change each instance of&nbsp;<code>eth0<\/code>&nbsp;in the above configuration to match the interface name you found with&nbsp;<code>ip route<\/code>. The&nbsp;<code>*nat<\/code>&nbsp;lines create rules so that the firewall can correctly route and manipulate traffic between the VPN clients and the internet. The&nbsp;<code>*mangle<\/code>&nbsp;line adjusts the maximum packet segment size to prevent potential issues with certain VPN clients:<\/p>\n\n\n\n<p>\/etc\/ufw\/before.rules<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark>*nat<\/mark>\n<mark>-A POSTROUTING -s 10.10.10.0\/24 -o eth0 -m policy --pol ipsec --dir out -j ACCEPT<\/mark>\n<mark>-A POSTROUTING -s 10.10.10.0\/24 -o eth0 -j MASQUERADE<\/mark>\n<mark>COMMIT<\/mark>\n\n<mark>*mangle<\/mark>\n<mark>-A FORWARD --match policy --pol ipsec --dir in -s 10.10.10.0\/24 -o eth0 -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1361:1536 -j TCPMSS --set-mss 1360<\/mark>\n<mark>COMMIT<\/mark>\n\n*filter\n:ufw-before-input - &#91;0:0]\n:ufw-before-output - &#91;0:0]\n:ufw-before-forward - &#91;0:0]\n:ufw-not-local - &#91;0:0]\n. . .\n<\/code><\/pre>\n\n\n\n<p>Next, after the&nbsp;<code>*filter<\/code>&nbsp;and chain definition lines, add one more block of configuration:<\/p>\n\n\n\n<p>\/etc\/ufw\/before.rules<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. . .\n*filter\n:ufw-before-input - &#91;0:0]\n:ufw-before-output - &#91;0:0]\n:ufw-before-forward - &#91;0:0]\n:ufw-not-local - &#91;0:0]\n\n<mark>-A ufw-before-forward --match policy --pol ipsec --dir in --proto esp -s 10.10.10.0\/24 -j ACCEPT<\/mark>\n<mark>-A ufw-before-forward --match policy --pol ipsec --dir out --proto esp -d 10.10.10.0\/24 -j ACCEPT<\/mark>\n<\/code><\/pre>\n\n\n\n<p>These lines tell the firewall to forward&nbsp;<a href=\"https:\/\/wiki.wireshark.org\/ESP\" rel=\"noreferrer noopener\" target=\"_blank\">ESP<\/a>&nbsp;(Encapsulating Security Payload) traffic so the VPN clients will be able to connect. ESP provides additional security for our VPN packets as they\u2019re traversing untrusted networks.<\/p>\n\n\n\n<p>When you\u2019re finished, ave and close the file once you\u2019ve verified that you\u2019ve added each line correctly. If you used&nbsp;<code>nano<\/code>, do so by pressing&nbsp;<code>CTRL + X<\/code>,&nbsp;<code>Y<\/code>, then&nbsp;<code>ENTER<\/code>.<\/p>\n\n\n\n<p>Before restarting the firewall, we\u2019ll change some network kernel parameters to allow routing from one interface to another. The file that controls these settings is called&nbsp;<code>\/etc\/ufw\/sysctl.conf<\/code>. We\u2019ll need to configure a few things in the file.<\/p>\n\n\n\n<p>First IPv4 packet forwarding needs to be turned on so that traffic can move between the VPN and public facing network interfaces on the server. Next we\u2019ll disable Path MTU discovery to prevent packet fragmentation problems. Finally we will not accept ICMP redirects nor send ICMP redirects to prevent&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Man-in-the-middle_attack\" rel=\"noreferrer noopener\" target=\"_blank\">man-in-the-middle<\/a>&nbsp;attacks.<\/p>\n\n\n\n<p>Open UFW\u2019s kernel parameters configuration file using&nbsp;<code>nano<\/code>&nbsp;or your preferred text editor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/ufw\/sysctl.conf\n<\/code><\/pre>\n\n\n\n<p>Now add the following&nbsp;<code>net\/ipv4\/ip_forward=1<\/code>&nbsp;setting at the end of the file to enable forwarding packets between interfaces:<\/p>\n\n\n\n<p>\/etc\/ufw\/sysctl.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. . .\n<mark>net\/ipv4\/ip_forward=1<\/mark>\n<\/code><\/pre>\n\n\n\n<p>Next block sending and receiving ICMP redirect packets by adding the following lines to the end of the file:<\/p>\n\n\n\n<p>\/etc\/ufw\/sysctl.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. . .\n<mark>net\/ipv4\/conf\/all\/accept_redirects=0<\/mark>\n<mark>net\/ipv4\/conf\/all\/send_redirects=0<\/mark>\n<\/code><\/pre>\n\n\n\n<p>Finally, turn off Path MTU discovery by adding this line to the end of the file:<\/p>\n\n\n\n<p>\/etc\/ufw\/sysctl.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. . .\n<mark>net\/ipv4\/ip_no_pmtu_disc=1<\/mark>\n<\/code><\/pre>\n\n\n\n<p>Save the file when you are finished. Now we can enable all of our changes by disabling and re-enabling the firewall, since UFW applies these settings any time that it restarts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw disable\nsudo ufw enable\n<\/code><\/pre>\n\n\n\n<p>You\u2019ll be prompted to confirm the process. Type&nbsp;<code>Y<\/code>&nbsp;to enable UFW again with the new settings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-7-testing-the-vpn-connection-on-windows-macos-ubuntu-ios-and-android\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-7-testing-the-vpn-connection-on-windows-macos-ubuntu-ios-and-android\">Step 7 \u2014 Testing the VPN Connection on Windows, macOS, Ubuntu, iOS, and Android<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#step-7-testing-the-vpn-connection-on-windows-macos-ubuntu-ios-and-android\"><\/a><\/h2>\n\n\n\n<p>Now that you have everything set up, it\u2019s time to try it out. First, you\u2019ll need to copy the CA 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 output the contents of the certificate file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/ipsec.d\/cacerts\/ca-cert.pem\n<\/code><\/pre>\n\n\n\n<p>You\u2019ll see output similar to this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Output-----BEGIN CERTIFICATE-----\nMIIFNDCCAxygAwIBAgIIHCsidG5mXzgwDQYJKoZIhvcNAQEMBQAwODELMAkGA1UE\n\n. . .\n\nH2YUdz8XNHrJHvMQKWFpi0rlEcMs+MSXPFWE3Q7UbaZJ\/h8wpSldSUbQRUlphExJ\ndJ4PX+MUJO\/vjG1\/ie6Kh25xbBAc3qNq8siiJZDwrg6vjEK7eiZ1rA==\n-----END CERTIFICATE-----\n<\/code><\/pre>\n\n\n\n<p>Copy this output to your computer, including the&nbsp;<code>-----BEGIN CERTIFICATE-----<\/code>&nbsp;and&nbsp;<code>-----END CERTIFICATE-----<\/code>&nbsp;lines, and save it to a file with a recognizable name, such as&nbsp;<code>ca-cert.pem<\/code>. Ensure the file you create has the&nbsp;<code>.pem<\/code>&nbsp;extension.<\/p>\n\n\n\n<p>Alternatively,&nbsp;<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-use-sftp-to-securely-transfer-files-with-a-remote-server\">use SFTP to transfer the file to your computer<\/a>.<\/p>\n\n\n\n<p>Once you have the&nbsp;<code>ca-cert.pem<\/code>&nbsp;file downloaded to your computer, you can set up the connection to the VPN.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"connecting-from-windows\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#connecting-from-windows\">Connecting from Windows<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#connecting-from-windows\"><\/a><\/h3>\n\n\n\n<p>There are multiple ways to import the root certificate and configure Windows to connect to a VPN. The first method uses graphical tools for each step. The second method uses PowerShell commands, which can be scripted and modified to suit your VPN configuration.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;These instructions have been tested on Windows 10 installations running versions 1903 and 1909.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"configuring-windows-with-graphical-tools\">Configuring Windows with Graphical Tools<\/h4>\n\n\n\n<p>First, import the root certificate by following these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Press\u00a0<code>WINDOWS+R<\/code>\u00a0to bring up the\u00a0<strong>Run<\/strong>\u00a0dialog, and enter\u00a0<code>mmc.exe<\/code>\u00a0to launch the Windows Management Console.<\/li>\n\n\n\n<li>From the\u00a0<strong>File<\/strong>\u00a0menu, navigate to\u00a0<strong>Add or Remove Snap-in<\/strong>, select\u00a0<strong>Certificates<\/strong>\u00a0from the list of available snap-ins, and click\u00a0<strong>Add<\/strong>.<\/li>\n\n\n\n<li>We want the VPN to work with any user, so select\u00a0<strong>Computer Account<\/strong>\u00a0and click\u00a0<strong>Next<\/strong>.<\/li>\n\n\n\n<li>We\u2019re configuring things on the local computer, so select\u00a0<strong>Local Computer<\/strong>, then click\u00a0<strong>Finish<\/strong>.<\/li>\n\n\n\n<li>Under the\u00a0<strong>Console Root<\/strong>\u00a0node, expand the\u00a0<strong>Certificates (Local Computer)<\/strong>\u00a0entry, expand\u00a0<strong>Trusted Root Certification Authorities<\/strong>, and then select the\u00a0<strong>Certificates<\/strong>\u00a0entry:<img decoding=\"async\" src=\"http:\/\/192.168.40.2\/wp-content\/uploads\/2025\/07\/4PN0vT6.png\" alt=\"Certificates view\"><\/li>\n\n\n\n<li>From the\u00a0<strong>Action<\/strong>\u00a0menu, select\u00a0<strong>All Tasks<\/strong>\u00a0and click\u00a0<strong>Import<\/strong>\u00a0to display the Certificate Import Wizard. Click\u00a0<strong>Next<\/strong>\u00a0to move past the introduction.<\/li>\n\n\n\n<li>On the\u00a0<strong>File to Import<\/strong>\u00a0screen, press the\u00a0<strong>Browse<\/strong>\u00a0button, ensure that you change the file type from \u201cX.509 Certificate (<em>.cer;<\/em>.crt)\u201d to \u201cAll Files (<em>.<\/em>)\u201d, and select the\u00a0<code>ca-cert.pem<\/code>\u00a0file that you\u2019ve saved. Then click\u00a0<strong>Next<\/strong>.<\/li>\n\n\n\n<li>Ensure that the\u00a0<strong>Certificate Store<\/strong>\u00a0is set to\u00a0<strong>Trusted Root Certification Authorities<\/strong>, and click\u00a0<strong>Next<\/strong>.<\/li>\n\n\n\n<li>Click\u00a0<strong>Finish<\/strong>\u00a0to import the certificate.<\/li>\n<\/ol>\n\n\n\n<p>Then configure the VPN with these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Launch\u00a0<strong>Control Panel<\/strong>, then navigate to the\u00a0<strong>Network and Sharing Center<\/strong>.<\/li>\n\n\n\n<li>Click on\u00a0<strong>Set up a new connection or network<\/strong>, then select\u00a0<strong>Connect to a workplace<\/strong>.<\/li>\n\n\n\n<li>Select\u00a0<strong>Use my Internet connection (VPN)<\/strong>.<\/li>\n\n\n\n<li>Enter the VPN server details. Enter the server\u2019s domain name or IP address in the\u00a0<strong>Internet address<\/strong>\u00a0field, then fill in\u00a0<strong>Destination name<\/strong>\u00a0with something that describes your VPN connection. Then click\u00a0<strong>Done<\/strong>.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"configuring-windows-using-powershell\">Configuring Windows using PowerShell<\/h4>\n\n\n\n<p>To import the root CA certificate using PowerShell, first open a PowerShell prompt with administrator privileges. To do so, right click the Start menu icon and select&nbsp;<code>Windows PowerShell (Admin)<\/code>. You can also open a command prompt as administrator and type&nbsp;<code>powershell<\/code>.<\/p>\n\n\n\n<p>Next we\u2019ll import the certificate using the&nbsp;<code>Import-Certificate<\/code>&nbsp;PowerShell cmdlet. In the following command, the first&nbsp;<code>-CertStoreLocation<\/code>&nbsp;argument will ensure that the certificate is imported into the computer\u2019s&nbsp;<strong>Trusted Root Certification Authorities<\/strong>&nbsp;store so that all programs and users will be able to verify the VPN server\u2019s certificate. The&nbsp;<code>-FilePath<\/code>&nbsp;argument should point to the location where you copied the certificate. In the following example the path is&nbsp;<code>C:\\Users\\sammy\\Documents\\ca-cert.pem<\/code>. Ensure that you edit the command to match the location that you used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Import-Certificate `\n    -CertStoreLocation cert:\\LocalMachine\\Root\\ `\n    -FilePath <mark>C:\\users\\sammy\\Documents\\ca-cert.pem<\/mark>\n<\/code><\/pre>\n\n\n\n<p>The command will output something like the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Output   PSParentPath: Microsoft.PowerShell.Security\\Certificate::LocalMachine\\Root\n\nThumbprint                                Subject\n----------                                -------\nDB00813B4087E9367861E8463A60CEA0ADC5F002  CN=VPN root CA\n<\/code><\/pre>\n\n\n\n<p>Now to configure the VPN using PowerShell, run the following command. Substitute your server\u2019s DNS name or IP address on the&nbsp;<code>-ServerAddress<\/code>&nbsp;line. The various flags will ensure that Windows is correctly configured with the appropriate security parameters that match the options that you set in&nbsp;<code>\/etc\/ipsec.conf<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Add-VpnConnection -Name \"VPN Connection\" `\n    -ServerAddress \"<mark>server_domain_or_IP<\/mark>\" `\n    -TunnelType \"IKEv2\" `\n    -AuthenticationMethod \"EAP\" `\n    -EncryptionLevel \"Maximum\" `\n    -RememberCredential `\n<\/code><\/pre>\n\n\n\n<p>If the command is successful there will not be any output. To confirm the VPN is configured correctly, use the&nbsp;<code>Get-VPNConnection<\/code>&nbsp;cmdlet:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-VpnConnection -Name \"VPN Connection\"\n<\/code><\/pre>\n\n\n\n<p>You will receive output like the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OutputName                  : VPN Connection\nServerAddress         : <mark>your_server_ip<\/mark>\nAllUserConnection     : False\nGuid                  : {B055A1AB-175C-4028-B4A8-D34309A2B20E}\nTunnelType            : Ikev2\nAuthenticationMethod  : {Eap}\nEncryptionLevel       : Maximum\nL2tpIPsecAuth         :\nUseWinlogonCredential : False\nEapConfigXmlStream    : #document\nConnectionStatus      : Disconnected\nRememberCredential    : True\nSplitTunneling        : False\nDnsSuffix             :\nIdleDisconnectSeconds : 0\n<\/code><\/pre>\n\n\n\n<p>By default Windows chooses older and slower algorithms. Run the&nbsp;<code>Set-VpnConnectionIPsecConfiguration<\/code>&nbsp;cmdlet to upgrade the encryption parameters that Windows will use for the IKEv2 key exchange, and to encrypt packets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Set-VpnConnectionIPsecConfiguration -Name \"VPN Connection\" `\n    -AuthenticationTransformConstants GCMAES256 `\n    -CipherTransformConstants GCMAES256 `\n    -DHGroup ECP384 `\n    -IntegrityCheckMethod SHA384 `\n    -PfsGroup ECP384 `\n    -EncryptionMethod GCMAES256\n<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: If you would like to delete the VPN connection and reconfigure it with different options, you can run the&nbsp;<code>Remove-VpnConnection<\/code>&nbsp;cmdlet.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Remove-VpnConnection -Name \"VPN Connection\" -Force\n<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>-Force<\/code>&nbsp;flag will skip prompting you to confirm the removal. You must be disconnected from the VPN if you attempt to remove it using this command.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"connecting-to-the-vpn\">Connecting to the VPN<\/h4>\n\n\n\n<p>Once you have the certificate imported and the VPN configured using either method, your new VPN connection will be visible under the list of networks. Select the VPN and click&nbsp;<strong>Connect<\/strong>. You\u2019ll be prompted for your username and password. Type them in, click&nbsp;<strong>OK<\/strong>, and you\u2019ll be connected.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"connecting-from-macos\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#connecting-from-macos\">Connecting from macOS<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#connecting-from-macos\"><\/a><\/h3>\n\n\n\n<p>Follow these steps to import the certificate:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Double-click the certificate file.\u00a0<strong>Keychain Access<\/strong>\u00a0will pop up with a dialog that says \u201cKeychain Access is trying to modify the system keychain. Enter your password to allow this.\u201d<\/li>\n\n\n\n<li>Enter your password, then click on\u00a0<strong>Modify Keychain<\/strong><\/li>\n\n\n\n<li>Double-click the newly imported VPN certificate. This brings up a small properties window where you can specify the trust levels. Set\u00a0<strong>IP Security (IPSec)<\/strong>\u00a0to\u00a0<strong>Always Trust<\/strong>\u00a0and you\u2019ll be prompted for your password again. This setting saves automatically after entering the password.<\/li>\n<\/ol>\n\n\n\n<p>Now that the certificate is imported and trusted, configure the VPN connection with these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to\u00a0<strong>System Preferences<\/strong>\u00a0and choose\u00a0<strong>Network<\/strong>.<\/li>\n\n\n\n<li>Click on the small \u201cplus\u201d button on the lower-left of the list of networks.<\/li>\n\n\n\n<li>In the popup that appears, set\u00a0<strong>Interface<\/strong>\u00a0to\u00a0<strong>VPN<\/strong>, set the\u00a0<strong>VPN Type<\/strong>\u00a0to\u00a0<strong>IKEv2<\/strong>, and give the connection a name.<\/li>\n\n\n\n<li>In the\u00a0<strong>Server<\/strong>\u00a0and\u00a0<strong>Remote ID<\/strong>\u00a0field, enter the server\u2019s domain name or IP address. Leave the\u00a0<strong>Local ID<\/strong>\u00a0blank.<\/li>\n\n\n\n<li>Click on\u00a0<strong>Authentication Settings<\/strong>, select\u00a0<strong>Username<\/strong>, and enter your username and password you configured for your VPN user. Then click\u00a0<strong>OK<\/strong>.<\/li>\n<\/ol>\n\n\n\n<p>Finally, click on&nbsp;<strong>Connect<\/strong>&nbsp;to connect to the VPN. You should now be connected to the VPN.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"connecting-from-ubuntu\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#connecting-from-ubuntu\">Connecting from Ubuntu<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#connecting-from-ubuntu\"><\/a><\/h3>\n\n\n\n<p>To connect from an Ubuntu machine, you can set up and manage StrongSwan as a service or use a one-off command every time you wish to connect. Instructions are provided for both.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"managing-strongswan-as-a-service\">Managing StrongSwan as a Service<\/h4>\n\n\n\n<p>To manage StrongSwan as a service, you will need to perform the following configuration steps.<\/p>\n\n\n\n<p>First, update your local package cache using&nbsp;<code>apt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> sudo apt update\n<\/code><\/pre>\n\n\n\n<p>Next, install StrongSwan and the required plugins for authentication:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install strongswan libcharon-extra-plugins\n<\/code><\/pre>\n\n\n\n<p>Now you\u2019ll need a copy of the CA certificate in the&nbsp;<code>\/etc\/ipsec.d\/cacerts<\/code>&nbsp;directory so that your client can verify the server\u2019s identity. Run the following command to copy the&nbsp;<code>ca-cert.pem<\/code>&nbsp;file into place:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo cp <mark>\/tmp\/ca-cert.pem<\/mark> \/etc\/ipsec.d\/cacerts\n<\/code><\/pre>\n\n\n\n<p>To ensure the VPN only runs on demand, use&nbsp;<code>systemctl<\/code>&nbsp;to disable StrongSwan from running automatically:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl disable --now strongswan-starter\n<\/code><\/pre>\n\n\n\n<p>Next configure the username and password that you will use to authenticate to the VPN server. Edit&nbsp;<code>\/etc\/ipsec.secrets<\/code>&nbsp;using nano or your preferred editor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/ipsec.secrets\n<\/code><\/pre>\n\n\n\n<p>Add the following line, editing the highlighted username and password values to match the ones that you configured on the server:<\/p>\n\n\n\n<p>\/etc\/ipsec.secrets<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark>your_username<\/mark> : EAP <mark>\"your_password\"<\/mark>\n<\/code><\/pre>\n\n\n\n<p>Finally, edit the&nbsp;<code>\/etc\/ipsec.conf<\/code>&nbsp;file to configure your client to match the server\u2019s configuration:<\/p>\n\n\n\n<p>\/etc\/ipsec.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>config setup\n\nconn ikev2-rw\n    right=<mark>server_domain_or_IP<\/mark>\n    # This should match the `leftid` value on your server's configuration\n    rightid=<mark>server_domain_or_IP<\/mark>\n    rightsubnet=0.0.0.0\/0\n    rightauth=pubkey\n    leftsourceip=%config\n    leftid=<mark>username<\/mark>\n    leftauth=eap-mschapv2\n    eap_identity=%identity\n    auto=start\n<\/code><\/pre>\n\n\n\n<p>To connect to the VPN, type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl start strongswan-starter\n<\/code><\/pre>\n\n\n\n<p>To disconnect again, type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl stop strongswan-starter\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"using-the-charon-cmd-client-for-one-off-connections\">Using the&nbsp;<code>charon-cmd<\/code>&nbsp;Client for One-Off Connections<\/h4>\n\n\n\n<p>To manage StrongSwan as a service, you will need to perform the following configuration steps.<\/p>\n\n\n\n<p>First, update your local package cache using&nbsp;<code>apt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\n<\/code><\/pre>\n\n\n\n<p>Next, install StrongSwan and the required plugins for authentication:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install strongswan libcharon-extra-plugins\n<\/code><\/pre>\n\n\n\n<p>Now you\u2019ll need a copy of the CA certificate in the&nbsp;<code>\/etc\/ipsec.d\/cacerts<\/code>&nbsp;directory so that your client can verify the server\u2019s identity. Run the following command to copy the&nbsp;<code>ca-cert.pem<\/code>&nbsp;file into place:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo cp <mark>\/tmp\/ca-cert.pem<\/mark> \/etc\/ipsec.d\/cacerts\n<\/code><\/pre>\n\n\n\n<p>At this point you can connect to the VPN server with&nbsp;<code>charon-cmd<\/code>&nbsp;using the server\u2019s CA certificate, the VPN server\u2019s IP address, and the username you configured.<\/p>\n\n\n\n<p>Run the following command whenever you want to connect to the VPN:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo charon-cmd --cert ca-cert.pem --host <mark>vpn_domain_or_IP<\/mark> --identity <mark>your_username<\/mark>\n<\/code><\/pre>\n\n\n\n<p>When prompted, provide the VPN user\u2019s password and you will be connected to the VPN. To disconnect, press&nbsp;<code>CTRL+C<\/code>&nbsp;in the terminal and wait for the connection to close.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"connecting-from-ios\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#connecting-from-ios\">Connecting from iOS<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#connecting-from-ios\"><\/a><\/h3>\n\n\n\n<p>To configure the VPN connection on an iOS device, follow these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Send yourself an email with the root certificate attached.<\/li>\n\n\n\n<li>Open the email on your iOS device and tap on the attached certificate file, then tap\u00a0<strong>Install<\/strong>\u00a0and enter your passcode. Once it installs, tap\u00a0<strong>Done<\/strong>.<\/li>\n\n\n\n<li>Go to\u00a0<strong>Settings<\/strong>,\u00a0<strong>General<\/strong>,\u00a0<strong>VPN<\/strong>\u00a0and tap\u00a0<strong>Add VPN Configuration<\/strong>. This will bring up the VPN connection configuration screen.<\/li>\n\n\n\n<li>Tap on\u00a0<strong>Type<\/strong>\u00a0and select\u00a0<strong>IKEv2<\/strong>.<\/li>\n\n\n\n<li>In the\u00a0<strong>Description<\/strong>\u00a0field, enter a short name for the VPN connection. This could be anything you like.<\/li>\n\n\n\n<li>In the\u00a0<strong>Server<\/strong>\u00a0and\u00a0<strong>Remote ID<\/strong>\u00a0field, enter the server\u2019s domain name or IP address. The\u00a0<strong>Local ID<\/strong>\u00a0field can be left blank.<\/li>\n\n\n\n<li>Enter your username and password in the\u00a0<strong>Authentication<\/strong>\u00a0section, then tap\u00a0<strong>Done<\/strong>.<\/li>\n\n\n\n<li>Select the VPN connection that you just created, tap the switch on the top of the page, and you\u2019ll be connected.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"connecting-from-android\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#connecting-from-android\">Connecting from Android<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#connecting-from-android\"><\/a><\/h3>\n\n\n\n<p>Follow these steps to import the certificate:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Send yourself an email with the CA certificate attached. Save the CA certificate to your downloads folder.<\/li>\n\n\n\n<li>Download the\u00a0<a href=\"https:\/\/play.google.com\/store\/apps\/details?id=org.strongswan.android&amp;hl=en_US\" target=\"_blank\" rel=\"noreferrer noopener\">StrongSwan VPN client<\/a>\u00a0from the Play Store.<\/li>\n\n\n\n<li>Open the app. Tap the \u201cmore\u201d icon (<strong>. . .<\/strong>) in the upper-right corner and select\u00a0<strong>CA certificates<\/strong>.<\/li>\n\n\n\n<li>Tap the \u201cmore\u201d icon (<strong>. . .<\/strong>) in the upper-right corner again. Select\u00a0<strong>Import certificate<\/strong>.<\/li>\n\n\n\n<li>Browse to the CA certificate file in your downloads folder and select it to import it into the app.<\/li>\n<\/ol>\n\n\n\n<p>Now that the certificate is imported into the StrongSwan app, you can configure the VPN connection with these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>In the app, tap\u00a0<strong>ADD VPN PROFILE<\/strong>\u00a0at the top.<\/li>\n\n\n\n<li>Fill out the\u00a0<strong>Server<\/strong>\u00a0with your VPN server\u2019s domain name or public IP address.<\/li>\n\n\n\n<li>Make sure\u00a0<strong>IKEv2 EAP (Username\/Password)<\/strong>\u00a0is selected as the VPN Type.<\/li>\n\n\n\n<li>Fill out the\u00a0<strong>Username<\/strong>\u00a0and\u00a0<strong>Password<\/strong>\u00a0with the credentials you defined on the server.<\/li>\n\n\n\n<li>Deselect\u00a0<strong>Select automatically<\/strong>\u00a0in the\u00a0<strong>CA certificate<\/strong>\u00a0section and click\u00a0<strong>Select CA certificate<\/strong>.<\/li>\n\n\n\n<li>Tap the\u00a0<strong>IMPORTED<\/strong>\u00a0tab at the top of the screen and choose the CA you imported (it will be named \u201cVPN root CA\u201d if you didn\u2019t change the \u201cDN\u201d earlier).<\/li>\n\n\n\n<li>If you\u2019d like, fill out\u00a0<strong>Profile name (optional)<\/strong>\u00a0with a more descriptive name.<\/li>\n<\/ol>\n\n\n\n<p>When you wish to connect to the VPN, click on the profile you just created in the StrongSwan application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"troubleshooting-connections\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#troubleshooting-connections\">Troubleshooting Connections<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#troubleshooting-connections\"><\/a><\/h3>\n\n\n\n<p>If you are unable to import the certificate, ensure the file has the&nbsp;<code>.pem<\/code>&nbsp;extension, and not&nbsp;<code>.pem.txt<\/code>.<\/p>\n\n\n\n<p>If you\u2019re unable to connect to the VPN, check the server name or IP address you used. The server\u2019s domain name or IP address must match what you\u2019ve configured as the common name (CN) while creating the certificate. If they don\u2019t match, the VPN connection won\u2019t work. For example, if you set up a certificate with the CN of&nbsp;<code>vpn.example.com<\/code>, you&nbsp;<em>must<\/em>&nbsp;use&nbsp;<code>vpn.example.com<\/code>&nbsp;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.<\/p>\n\n\n\n<p>Finally, double-check the VPN configuration to ensure the&nbsp;<code>leftid<\/code>&nbsp;value is configured with the&nbsp;<code>@<\/code>&nbsp;symbol if you\u2019re using a domain name:<\/p>\n\n\n\n<p>\/etc\/ipsec.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    leftid=<mark>@<\/mark>vpn.example.com\n<\/code><\/pre>\n\n\n\n<p>If you\u2019re using an IP address, ensure that the&nbsp;<code>@<\/code>&nbsp;symbol is omitted. Also make sure that when you generated the&nbsp;<code>server-cert.pem<\/code>&nbsp;file that you included both&nbsp;<code>--san @<mark>IP_address<\/mark><\/code>&nbsp;and&nbsp;<code>--san&nbsp;<mark>IP_address<\/mark><\/code>&nbsp;flags.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#conclusion\">Conclusion<\/a><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-20-04#conclusion\"><\/a><\/h2>\n\n\n\n<p>In this tutorial, you\u2019ve built a VPN server that uses the IKEv2 protocol. You learned about the directives that control the&nbsp;<code>left<\/code>&nbsp;and&nbsp;<code>right<\/code>&nbsp;sides of a connection on both server and clients. You also configured a Windows, macOS, iOS, Android, or Linux client to connect to the VPN.<\/p>\n\n\n\n<p>To add or remove users, skip to Step 5 again. Each line in&nbsp;<code>\/etc\/ipsec.secrets<\/code>&nbsp;is for one user, so adding or removing users, or changing passwords just requires editing the file.<\/p>\n\n\n\n<p>Now you can be assured that your online activities will remain secure wherever you go and with any device that you use to access the internet.<\/p>\n\n\n\n<p>Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction 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<\/p>\n<p><a href=\"https:\/\/allogman.com\/?p=47\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\">How to Set Up an IKEv2 VPN Server with StrongSwan on Ubuntu 20.04<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-network-security"],"_links":{"self":[{"href":"https:\/\/allogman.com\/index.php?rest_route=\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/allogman.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/allogman.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/allogman.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/allogman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=47"}],"version-history":[{"count":1,"href":"https:\/\/allogman.com\/index.php?rest_route=\/wp\/v2\/posts\/47\/revisions"}],"predecessor-version":[{"id":49,"href":"https:\/\/allogman.com\/index.php?rest_route=\/wp\/v2\/posts\/47\/revisions\/49"}],"wp:attachment":[{"href":"https:\/\/allogman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/allogman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/allogman.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}