How to Set Up IPsec-based VPN with Strongswan on Debian and Ubuntu

strongSwan is an open-source, cross-platform, full-featured and widely-used IPsec-based VPN (Virtual Private Network) implementation that runs on Linux, FreeBSD, OS X, Windows, Android, and iOS. It is primarily a keying daemon that supports the Internet Key Exchange protocols (IKEv1 and IKEv2) to establish security associations (SA) between two peers.

This article describes how to set up a site-to-site IPSec VPN gateways using strongSwan on Ubuntu and Debian servers. By site-to-site we mean each security gateway has a sub-net behind it. Besides, the peers will authenticate each other using a pre-shared key (PSK).

Testing Environment

Remember to replace the following IPs with your real-world IPs to configure your environment.

Site 1 Gateway (tecmint-devgateway)

OS 1: Debian or Ubuntu
Public IP: 10.20.20.1
Private IP: 192.168.0.101/24
Private Subnet: 192.168.0.0/24

Site 2 Gateway (tecmint-prodgateway)

OS 2: Debian or Ubuntu
Public IP:  10.20.20.3
Private IP: 10.0.2.15/24
Private Subnet: 10.0.2.0/24

Step 1: Enabling Kernel Packet Forwarding

1. First, you need to configure the kernel to enable packet forwarding by adding the appropriate system variables in /etc/sysctl.conf configuration file on both security gateways.

$ sudo vim /etc/sysctl.conf

Look for the following lines and uncomment them and set their values as shown (read comments in the file for more information).

net.ipv4.ip_forward = 1 
net.ipv6.conf.all.forwarding = 1 
net.ipv4.conf.all.accept_redirects = 0 
net.ipv4.conf.all.send_redirects = 0 

2. Next, load the new settings by running the following command.

$ sudo sysctl -p
Load Sysctl Kernel Settings
Load Sysctl Kernel Settings

3. If you have a UFW firewall service enabled, you need to add the following rules to the /etc/ufw/before.rules configuration file just before the filter rules in either security gateways.

Site 1 Gateway (tecmint-devgateway)

*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 10.0.2.0/24  -d 192.168.0.0/24 -j MASQUERADE
COMMIT

Site 2 Gateway (tecmint-prodgateway)

*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING  -s 192.168.0.0/24 -d 10.0.2.0/24 -j MASQUERADE
COMMIT

4. Once firewall rules have been added, then apply the new changes by restarting UFW as shown.

$ sudo ufw disable 
$ sudo ufw enable

Step 2: Installing strongSwan in Debian and Ubuntu

5. Update your package cache on both security gateways and install the strongswan package using the APT package manager.

$ sudo apt update
$ sudo apt install strongswan 

6. Once the installation is complete, the installer script will start the strongswan service and enable it to automatically start at system boot. You can check its status and whether it is enabled using the following command.

$ sudo systemctl status strongswan.service
$ sudo systemctl is-enabled strongswan.service

Step 3: Configuring Security Gateways

7. Next, you need to configure the security gateways using the /etc/ipsec.conf configuration file.

Site 1 Gateway (tecmint-devgateway)

$ sudo cp /etc/ipsec.conf /etc/ipsec.conf.orig
$ sudo nano /etc/ipsec.conf 

Copy and paste the following configuration in the file.

config setup
        charondebug="all"
        uniqueids=yes
conn devgateway-to-prodgateway
        type=tunnel
        auto=start
        keyexchange=ikev2
        authby=secret
        left=10.20.20.1
        leftsubnet=192.168.0.101/24
        right=10.20.20.3
        rightsubnet=10.0.2.15/24
        ike=aes256-sha1-modp1024!
        esp=aes256-sha1!
        aggressive=no
        keyingtries=%forever
        ikelifetime=28800s
        lifetime=3600s
        dpddelay=30s
        dpdtimeout=120s
        dpdaction=restart

Site 2 Gateway (tecmint-prodgateway)

$ sudo cp /etc/ipsec.conf /etc/ipsec.conf.orig
$ sudo cp /etc/ipsec.conf 

Copy and paste the following configuration in the file.

config setup
        charondebug="all"
        uniqueids=yes
conn prodgateway-to-devgateway
        type=tunnel
        auto=start
        keyexchange=ikev2
        authby=secret
        left=10.20.20.3
        leftsubnet=10.0.2.15/24
        right=10.20.20.1
        rightsubnet=192.168.0.101/24 
        ike=aes256-sha1-modp1024!
        esp=aes256-sha1!
        aggressive=no
        keyingtries=%forever
        ikelifetime=28800s
        lifetime=3600s
        dpddelay=30s
        dpdtimeout=120s
        dpdaction=restart

Here is the meaning of each configuration parameter:

  • config setup – specifies general configuration information for IPSec which applies to all connections.
  • charondebug – defines how much Charon debugging output should be logged.
  • uniqueids – specifies whether a particular participant ID should be kept unique.
  • conn prodgateway-to-devgateway – defines connection name.
  • type – defines connection type.
  • auto – how to handle connection when IPSec is started or restarted.
  • keyexchange – defines the version of the IKE protocol to use.
  • authby – defines how peers should authenticate each other.
  • left – defines the IP address of the left participant’s public-network interface.
  • leftsubnet – states the private subnet behind the left participant.
  • right – specifies the IP address of the right participant’s public-network interface.
  • rightsubnet – states the private subnet behind the left participant.
  • ike – defines a list of IKE/ISAKMP SA encryption/authentication algorithms to be used. You can add a comma-separated list.
  • esp – defines a list of ESP encryption/authentication algorithms to be used for the connection. You can add a comma-separated list.
  • aggressive – states whether to use Aggressive or Main Mode.
  • keyingtries – states the number of attempts that should be made to negotiate a connection.
  • ikelifetime – states how long the keying channel of a connection should last before being renegotiated.
  • lifetime – defines how long a particular instance of a connection should last, from successful negotiation to expiry.
  • dpddelay – specifies the time interval with which R_U_THERE messages/INFORMATIONAL exchanges are sent to the peer.
  • dpdtimeout – specifies the timeout interval, after which all connections to a peer are deleted in case of inactivity.
  • dpdaction – defines how to use the Dead Peer Detection(DPD) protocol to manage the connection.

For more information about the above configuration parameters, read the ipsec.conf man page by running the command.

$ man ipsec.conf

Step 4: Configuring PSK for Peer-to-Peer Authentication

8. After configuring both security gateways, generate a secure PSK to be used by the peers using the following command.

$ head -c 24 /dev/urandom | base64
Generate PSK Key
Generate PSK Key

9. Next, add the PSK in the /etc/ipsec.secrets file on both gateways.

$ sudo vim /etc/ipsec.secrets

Copy and paste the following line.

------- Site 1 Gateway (tecmint-devgateway) ------- 

10.20.20.1 10.20.20.3 : PSK "qLGLTVQOfqvGLsWP75FEtLGtwN3Hu0ku6C5HItKo6ac="

------- Site 2 Gateway (tecmint-prodgateway) -------

10.20.20.3  10.20.20.1 : PSK "qLGLTVQOfqvGLsWP75FEtLGtwN3Hu0ku6C5HItKo6ac="

10. Restart the IPSec program and check its status to view connections.

$ sudo ipsec restart
$ sudo ipsec status
View IPSec Connection Status
View IPSec Connection Status

11. Finally, verify that you can access the private sub-nets from either security gateways by running a ping command.

$ ping 192.168.0.101
$ ping 10.0.2.15
Verify Site-to-Site VPN Setup
Verify Site-to-Site VPN Setup

12. Besides, you can stop and start IPSec as shown.

$ sudo ipsec stop
$ sudo ipsec start

13. To know more about IPSec commands to manually bring up connections and more, see the IPSec help page.

$ ipsec --help

That’s all! In this article, we have described how to set up a site-to-site IPSec VPN using strongSwan on Ubuntu and Debian servers, where both security gateways were configured to authenticate each other using a PSK. If you have any questions or thoughts to share, reach us via the feedback form below.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

13 thoughts on “How to Set Up IPsec-based VPN with Strongswan on Debian and Ubuntu”

  1. Try to create a VPN with IPsec between 2 Linux configured each one in a different house in both the same configuration was done in left and right the public IP and left subnet and right subnet the private addresses, in ipsec.secrets we have the same psk key and not the service throws no error, but when reviewing the status it only appears “0 up, 1 connecting” and it no longer happens.

    We mapped ports 500 and 4500 on the router, but it stayed the same

    Reply
  2. Once the ipsec is configured, I want to communicate from an IP 192.168.0.110/24 to IP 10.0.2.20/24, how do I do it?

    Reply
    • @Henry

      If the VPN is up and running, then you can communicate normally, using ssh or any other remote communication application like VNC. The purpose of the VPN is to protect your communications.

      Reply
  3. Hi,

    Can I establish IPSec between two devices on the same network i.e they share the public IP address and the private subnet?

    What changes will I have to make in the configuration tutorial shared above?

    I have created two VMs on my laptop and trying to establish IPSec between them. This is a proof of concept after which I will establish IPSec between two Lanner 7525 devices, again sharing the public IP and private subnet.

    Thank you

    Reply
    • @Faizan

      Your setup is between two single hosts that don’t have a subnet behind them. In this case, you have to configure a host-to-host VPN. So simply use the LAN IPs as the right and left parameters, to connect them. Do not use the right and left subnet parameters.

      Reply
      • Thank you, dear. I think I have configured everything right now. After IPSec restart and status show, it shows Connecting, but SA is not established. After a while, connecting status also goes away. Any advice in this regard, please.

        Reply
      • Security Associations (0 up, 0 connecting).

        Does this mean that IPSec has been successfully configured?

        How then, can I check SA 1up?

        Reply
  4. Hi,

    I want to use the MySQL database for EAP credentials, just get user and pass from database.

    I enable MySQL plugin and setup it with this schema – https://wiki.strongswan.org/projects/strongswan/repository/revisions/master/entry/src/pool/mysql.sql

    and fill these table with user ‘test‘ and pass ‘test

    INSERT INTO identities (type, data) VALUES (2, X'74657374');
     INSERT INTO shared_secrets (type, data) VALUES (2, X'74657374'); 
    INSERT INTO shared_secret_identity (shared_secret, identity) VALUES (1, 1);
    

    then I try to connect, but can’t

    charon log:
    07[IKE] no EAP key found for hosts 'CN=ikv.my-example-domain.com' - 'test'
    ()
    

    any advice?

    thank you.

    Reply
    • @maid

      Ensure that you have done the setup right. You can refer to the documentation of the MySQL plugin for more information on how to solve the error. The strongswan service is probably not reading the required parameter displayed in the error.

      Reply
  5. Great tutorial! Worked great but I keep getting AUTHENTICATION_FAILED. Maybe is because one of my servers is behind NAT? Any suggestions to get that working?

    Reply

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.