{"id":5034,"date":"2020-02-26T19:26:43","date_gmt":"2020-02-26T16:26:43","guid":{"rendered":"https:\/\/kifarunix.com\/?p=5034"},"modified":"2024-03-14T19:25:45","modified_gmt":"2024-03-14T16:25:45","slug":"setup-ipsec-vpn-using-strongswan-on-debian-10","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/setup-ipsec-vpn-using-strongswan-on-debian-10\/","title":{"rendered":"Setup IPSEC VPN using StrongSwan on Debian 10"},"content":{"rendered":"\n<p>In this guide, we are going to learn how to setup IPSec VPN using StrongSwan on Debian 10. <a rel=\"noreferrer noopener\" href=\"https:\/\/www.strongswan.org\/\" target=\"_blank\">StrongSwan<\/a> is an opensource VPN software for Linux that implements IPSec. It supports various IPsec protocols and extensions such IKE, X.509 Digital Certificates, NAT Traversal\u2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up IPSEC VPN using StrongSwan on Debian<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Run System Update<\/h3>\n\n\n\n<p>To update your Debian 10 system packages, run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt update<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Install strongSwan on Debian 10 Buster<\/h3>\n\n\n\n<p>strongSwan is available on the default Debian 10 Buster repositories. Thus, the command below can be executed to install it and other required tools;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install strongswan strongswan-pki libcharon-extra-plugins<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Generate VPN Certificate and Key<\/h3>\n\n\n\n<p>For VPN clients to verify the authenticity of the VPN server, you need to generate the VPN server certificate and key and sign them using your CA.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Generate Local CA Certificate<\/h4>\n\n\n\n<p>In this demo, we will be singing our VPN Certificates with a self-signed CA. Thus the local CA can be generated using the strongSwan PKI utility installed above.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Generate a Private Key<\/h4>\n\n\n\n<p>The first step is to generate a private key for creating the self-signed CA certificate.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ipsec pki --gen --size 4096 --type rsa --outform pem &gt; vpn_ca_key.pem<\/code><\/pre>\n\n\n\n<p>Keep the key as private as possible.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Generate the self-signed CA Certificate<\/h4>\n\n\n\n<p>Next, generate the VPN server CA and self-sign with the key generated above.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ipsec pki --self --in vpn_ca_key.pem --type rsa \\\n--dn \"C=US, O=Kifarunix-Demo, CN=Kifarunix VPN Server Root CA\" \\\n--ca --lifetime 365 --outform pem &gt; vpn_ca_cert.pem<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Generate VPN server private key<\/h4>\n\n\n\n<p>Next, generate your VPN server private key by running the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ipsec pki --gen --size 4096 --type rsa --outform pem &gt; vpn_server_key.pem<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Generate VPN server Certificate<\/h4>\n\n\n\n<p>To generate the VPN Certificate, you need to extract the public key from the VPN private generated above. The public key will be included in the certificate to be generated.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ipsec pki --pub --in vpn_server_key.pem --type rsa &gt; vpn_server_pub_key.pem<\/code><\/pre>\n\n\n\n<p>Next, generate the certificate.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ipsec pki --issue --in vpn_server_pub_key.pem --lifetime 365 \\\n--cacert vpn_ca_cert.pem \\\n--cakey vpn_ca_key.pem \\\n--dn \"CN=vpnsvr.kifarunix-demo.com\" \\\n--san=\"vpnsvr.kifarunix-demo.com\" \\\n--flag serverAuth --flag ikeIntermediate --outform pem &gt; vpn_server_cert.pem<\/code><\/pre>\n\n\n\n<p>Adjust the distinguished name (dn), subjectAltName(san) and flags accordingly. Refer to <strong>man pki &#8211;issue<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install the VPN Certificates and Keys<\/h2>\n\n\n\n<p>Install the VPN certificates and keys in their respective IPSec directory locations.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mv vpn_ca_cert.pem \/etc\/ipsec.d\/cacerts\/\nmv vpn_server_cert.pem \/etc\/ipsec.d\/certs\/\nmv {vpn_ca_key.pem,vpn_server_key.pem} \/etc\/ipsec.d\/private\/<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Configure strongSwan on Debian 10<\/h2>\n\n\n\n<p>The next step is to configure strongSwan on Debian 10. The main default configuration file is <code>\/etc\/ipsec.conf<\/code>. Edit this file and make any relevant changes based on your environment setup.<\/p>\n\n\n\n<p>Create a backup copy of the original file before you can proceed.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp \/etc\/ipsec.conf \/etc\/ipsec.conf.old<\/code><\/pre>\n\n\n\n<p>Open the configuration file for editing;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vi \/etc\/ipsec.conf<\/code><\/pre>\n\n\n\n<p>The configuration file contains three sections;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>CONFIG SECTIONS<\/code> (config setup)<\/strong><br>\u2013 Defines general configuration parameters<\/li>\n\n\n\n<li><strong><code>CONN SECTIONS<\/code> (conn &lt;name&gt;)<\/strong><br>\u2013 Contains a connection specification, defining a network connection to be made using IPsec.<\/li>\n\n\n\n<li><strong><code>CA SECTION<\/code> (ca &lt;name&gt;)<\/strong><br>\u2013 Defines a certification authority.<\/li>\n<\/ul>\n\n\n\n<p>Define the CONFIGURATION parameters;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>config setup\n        charondebug=\"ike 2, knl 2, cfg 2, net 2, esp 2, dmn 2, mgr 2\"\n        strictcrlpolicy=no\n        uniqueids=yes\n        cachecrls=no<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The&nbsp;<code>charondebug = &lt;debug list&gt;<\/code>&nbsp; parameter defines the charon debug loggin where the debug list can be dmn, mgr, ike, chd, job, cfg, knl, net, asn, enc, lib, esp, tls, tnc, imc, imv, pts. The logging levels can one of -1, 0, 1, 2, 3, 4 (for silent, audit, control, controlmore, raw, private). By default, the level is set to 1 for all types. For a description of the debug lists, check the LOGGER CONFIGURATION section on&nbsp;<code>strongswan.conf(5)<\/code>.<\/li>\n\n\n\n<li><code>strictcrlpolicy<\/code>&nbsp;parameter defines if a fresh CRL must be available in order for the peer authentication based on RSA signatures to succeed.<\/li>\n\n\n\n<li><code>uniqueids<\/code>&nbsp;defines whether a particular participant ID should be kept unique<\/li>\n\n\n\n<li><code>cachecrls<\/code>&nbsp;defines whether to or not cache the certificate revocation lists (CRLs) fetched via HTTP or LDAP.<\/li>\n<\/ul>\n\n\n\n<p>Define the CONNECTION parameters;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>conn ipsec-ikev2-vpn\n      auto=add\n      compress=no\n      type=tunnel  # defines the type of connection, tunnel.\n      keyexchange=ikev2\n      fragmentation=yes\n      forceencaps=yes\n      dpdaction=clear\n      dpddelay=300s\n      rekey=no\n      left=%any\n      leftid=@vpnsvr.kifarunix-demo.com    <strong>#If using IP, define it without the @ sign<\/strong>\n      leftcert=vpn_server_cert.pem  <strong>#Reads the VPN server cert in \/etc\/ipsec.d\/certs<\/strong>\n      leftsendcert=always\n      leftsubnet=0.0.0.0\/0\n      right=%any\n      rightid=%any\n      rightauth=eap-mschapv2\n      rightsourceip=172.16.7.0\/24  <strong>#IP address Pool to be assigned to the clients<\/strong>\n      rightdns=8.8.8.8  # DNS to be assigned to clients\n      rightsendcert=never\n      eap_identity=%identity  <strong>#Defines the identity the client uses to reply to an EAP Identity request.<\/strong><\/code><\/pre>\n\n\n\n<p>To see a comprehensive description of the connection parameters and the values used in the above configuration, see&nbsp;<code><strong>man ipsec.conf<\/strong><\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setup Secrets for IKE\/IPsec authentication<\/h3>\n\n\n\n<p>Next, setup the secrets to be used by the strongSwan Internet Key Exchange (IKE) daemons to authenticate other hosts. These credentials&nbsp;are set in the&nbsp;<code>\/etc\/ipsec.secrets<\/code>&nbsp;configuration file.<\/p>\n\n\n\n<p>Thus open this file and define the RSA private keys for authentication. You can also setup the EAP user credentials by defining a random username and its password.&nbsp;<strong><code>Note the Spacing<\/code><\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/ipsec.secrets<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code># This file holds shared secrets or RSA private keys for authentication.\n\n# RSA private key for this host, authenticating it to any other host which knows the public part.\n\n<strong>: RSA vpn_server_key.pem ## Specify the VPN Server Key<\/strong>\n\n# Define the list of IDs followed by a secret for authentication\n<strong># user id : EAP secret<\/strong><user id=\"\"><secret>\n<strong>vpnsecure : EAP \"P@sSw0Rd\"   # Random<\/strong><\/secret><\/user>\n<strong>koromicha : EAP \"mypassword\"<\/strong><user id=\"\"><secret>\n\n# this file is managed with debconf and will contain the automatically created private key\ninclude \/var\/lib\/strongswan\/ipsec.secrets.inc<\/secret><\/user><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Running strongSwan<\/h4>\n\n\n\n<p>Save the configuration file above and restart strongswan for the changes above to take effect.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart strongswan<\/code><\/pre>\n\n\n\n<p>To check the status;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl status strongswan<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u25cf strongswan.service - strongSwan IPsec IKEv1\/IKEv2 daemon using ipsec.conf\n   Loaded: loaded (\/lib\/systemd\/system\/strongswan.service; enabled; vendor preset: enabled)\n   Active: active (running) since Mon 2020-02-24 13:37:50 EST; 1min 11s ago\n Main PID: 2667 (starter)\n    Tasks: 18 (limit: 1149)\n   Memory: 3.0M\n   CGroup: \/system.slice\/strongswan.service\n           \u251c\u25002667 \/usr\/lib\/ipsec\/starter --daemon charon --nofork\n           \u2514\u25002681 \/usr\/lib\/ipsec\/charon --debug-ike 2 --debug-knl 2 --debug-cfg 2 --debug-net 2 --debug-esp 2 --debug-dmn 2 --debug-mgr 2\n\nFeb 24 13:37:50 debian charon&#91;2681]: 05&#91;CFG]   eap_identity=%identity\nFeb 24 13:37:50 debian charon&#91;2681]: 05&#91;CFG]   dpddelay=300\nFeb 24 13:37:50 debian charon&#91;2681]: 05&#91;CFG]   dpdtimeout=150\nFeb 24 13:37:50 debian charon&#91;2681]: 05&#91;CFG]   dpdaction=1\nFeb 24 13:37:50 debian charon&#91;2681]: 05&#91;CFG]   sha256_96=no\nFeb 24 13:37:50 debian charon&#91;2681]: 05&#91;CFG]   mediation=no\nFeb 24 13:37:50 debian charon&#91;2681]: 05&#91;CFG]   keyexchange=ikev2\nFeb 24 13:37:50 debian charon&#91;2681]: 05&#91;CFG] adding virtual IP address pool 172.16.7.0\/24\nFeb 24 13:37:50 debian charon&#91;2681]: 05&#91;CFG]   loaded certificate \"CN=vpnsvr.kifarunix-demo.com\" from 'vpn_server_cert.pem'\nFeb 24 13:37:50 debian charon&#91;2681]: 05&#91;CFG] added configuration 'ipsec-ikev2-vpn'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Configure VPN Server Firewall and Routing<\/h3>\n\n\n\n<p>If UFW is enabled and running, configure it to allow and forward the VPN traffic. For IPsec to work through firewall, you need to open UDP ports 500 and 4500.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>500\/udp<\/strong>  &#8211; Allows Internet Security Association and Key Management Protocol (ISAKMP) traffic to be forwarded<\/li>\n\n\n\n<li><strong>4500\/udp<\/strong> &#8211; Allows handling of IPsec between natted devices<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ufw allow 500\/udp<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ufw allow 4500\/udp<\/code><\/pre>\n\n\n\n<p>Next, find your server default route interface.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><code>ip route show default<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>default via 10.0.2.2 dev <strong>enp0s3 <\/strong> <\/code><\/pre>\n\n\n\n<p>In this case, our interface for routing traffic is <strong>enp0s3<\/strong>. This interface will be used next while configuring UFW routing as shown below.<\/p>\n\n\n\n<p>Edit the UFW before rules file, <strong><code>\/etc\/ufw\/before.rules<\/code><\/strong> and add the lines highlighted below immediately before and after the&nbsp;<code>*filter<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim <code>\/etc\/ufw\/before.rules<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>...\n####\n<strong>*nat<\/strong>\n<strong>-A POSTROUTING -s 172.16.7.0\/24 -o enp0s3 -m policy --pol ipsec --dir out -j ACCEPT\n-A POSTROUTING -s 172.16.7.0\/24 -o enp0s3 -j MASQUERADE\nCOMMIT\n*mangle\n-A FORWARD --match policy --pol ipsec --dir in -s 172.16.7.0\/24 -o enp0s3 -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1361:1536 -j TCPMSS --set-mss 1360\nCOMMIT<\/strong>\n\n# Don't delete these required lines, otherwise there will be errors\n*filter\n:ufw-before-input - [0:0]\n:ufw-before-output - [0:0]\n:ufw-before-forward - [0:0]\n:ufw-not-local - [0:0]\n# End required lines\n<strong>-A ufw-before-forward --match policy --pol ipsec --dir in --proto esp -s 172.16.7.0\/24 -j ACCEPT\n-A ufw-before-forward --match policy --pol ipsec --dir out --proto esp -d 172.16.7.0\/24 -j ACCEPT<\/strong>\n...<\/code><\/pre>\n\n\n\n<p>Disable and enable ufw for the changes to take effect.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ufw disable &amp;&amp; ufw enable<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configure Kernel IP forwarding<\/h4>\n\n\n\n<p>Next, you need to configure routing by enabling Kernel IP forwarding by editing the file, <code>\/etc\/ufw\/sysctl.conf<\/code> and making the changes as highlighted below. If the lines do not exist, add them.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/ufw\/sysctl.conf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code># Uncomment the next line to enable packet forwarding for IPv4\n<strong>net\/ipv4\/ip_forward=1<\/strong>\n...\n# Do not accept ICMP redirects (prevent MITM attacks)\n<strong>net\/ipv4\/conf\/all\/accept_redirects = 0<\/strong>\n...\n# Do not send ICMP redirects (we are not a router)\n<strong>net\/ipv4\/conf\/all\/send_redirects = 0<\/strong>\n...\nDisable Path MTU discovery to prevent packet fragmentation by adding the line below\n<strong>net\/ipv4\/ip_no_pmtu_disc=1<\/strong><\/code><\/pre>\n\n\n\n<p>Save the configuration and reload the kernel runtime parameters.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sysctl -p<\/code><\/pre>\n\n\n\n<p>Restart strongSwan;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart strongswan<\/code><\/pre>\n\n\n\n<p>You can check that status by using the command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ipsec statusall<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Status of IKE charon daemon (strongSwan 5.7.2, Linux 4.19.0-8-amd64, x86_64):\n  uptime: 3 minutes, since Feb 24 14:08:54 2020\n  malloc: sbrk 1748992, mmap 0, used 527984, free 1221008\n  worker threads: 11 of 16 idle, 5\/0\/0\/0 working, job queue: 0\/0\/0\/0, scheduled: 0\n  loaded plugins: charon aesni aes rc2 sha2 sha1 md5 mgf1 random nonce x509 revocation constraints pubkey pkcs1 pkcs7 pkcs8 pkcs12 pgp dnskey sshkey pem openssl fips-prf gmp agent xcbc hmac gcm attr kernel-netlink resolve socket-default connmark stroke updown counters\nVirtual IP pools (size\/online\/offline):\n  172.16.7.0\/24: 254\/0\/0\nListening IP addresses:\n  10.0.2.15\n  192.168.56.174\nConnections:\nipsec-ikev2-vpn:  %any...%any  IKEv2, dpddelay=300s\nipsec-ikev2-vpn:   local:  &#91;vpnsvr.kifarunix-demo.com] uses public key authentication\nipsec-ikev2-vpn:    cert:  \"CN=vpnsvr.kifarunix-demo.com\"\nipsec-ikev2-vpn:   remote: uses EAP_MSCHAPV2 authentication with EAP identity '%any'\nipsec-ikev2-vpn:   child:  0.0.0.0\/0 === dynamic TUNNEL, dpdaction=clear\nSecurity Associations (0 up, 0 connecting):\n  none<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing strongSwan VPN Connection<\/h3>\n\n\n\n<p>strongSwan VPN server has been setup. You can now proceed to test the IP assignment and local connection via the VPN server.<\/p>\n\n\n\n<p>See our next guide on how to setup strongSwan VPN client on Ubuntu 18.04 and CentOS 8.<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/configure-strongswan-vpn-client-on-ubuntu-18-04-centos-8\/\" target=\"_blank\">Configure strongSwan VPN Client on Ubuntu 18.04\/CentOS 8<\/a><\/p>\n\n\n\n<p>That marks the end of our guide on how to setting up IPSEC VPN using StrongSwan on Debian 10 Buster.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Related Tutorials<\/h3>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-and-setup-openvpn-server-on-fedora-29-centos-7\/\" target=\"_blank\">Install and Setup OpenVPN Server on Fedora 29\/CentOS 7<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-cisco-anyconnect-client-on-centos-8\/\" target=\"_blank\">Install Cisco AnyConnect Client on CentOS 8<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-configure-ipsec-vpn-using-strongswan-on-ubuntu-18-04\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Configure IPSEC VPN using StrongSwan on Ubuntu 18.04<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we are going to learn how to setup IPSec VPN using StrongSwan on Debian 10. StrongSwan is an opensource VPN software for<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,282,34,321],"tags":[997,1287,324,1288,323],"class_list":["post-5034","post","type-post","status-publish","format-standard","hentry","category-howtos","category-openvpn","category-security","category-vpn","tag-debian-10","tag-ipsec-vpn","tag-strongswan","tag-strongswan-debian-10","tag-vpn","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/5034"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=5034"}],"version-history":[{"count":17,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/5034\/revisions"}],"predecessor-version":[{"id":21329,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/5034\/revisions\/21329"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=5034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=5034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=5034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}