{"id":6221,"date":"2020-06-21T13:31:12","date_gmt":"2020-06-21T10:31:12","guid":{"rendered":"https:\/\/kifarunix.com\/?p=6221"},"modified":"2024-03-14T21:14:25","modified_gmt":"2024-03-14T18:14:25","slug":"setup-ipsec-vpn-server-with-libreswan-on-centos-8","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/setup-ipsec-vpn-server-with-libreswan-on-centos-8\/","title":{"rendered":"Setup IPSec VPN Server with Libreswan on CentOS 8"},"content":{"rendered":"\n<p>Welcome to our today&#8217;s guide on how to setup IPSec VPN server with Libreswan on CentOS 8. <a rel=\"noreferrer noopener\" aria-label=\"Libreswan (opens in a new tab)\" href=\"https:\/\/libreswan.org\/wiki\/#User_Documentation\" target=\"_blank\">Libreswan<\/a> is a free implementation of IKE\/IPsec for Linux. IPsec is the Internet Protocol Security which uses strong cryptography to provide both authentication and encryption services and allow you to build secure tunnels through untrusted networks. Everything passing through the untrusted network is encrypted by the ipsec gateway machine and decrypted by the gateway at the other end of the tunnel. The resulting tunnel is a virtual private network or VPN.<\/p>\n\n\n\n<p>IKE manages the authentication between two communicating end points. It also enables endpoints to negotiate on algorithms to use to setup an IPsec tunnel.<\/p>\n\n\n\n<p>In our previous guide, we covered how to install and configure IPSec VPN using StrongSwan on Ubuntu 18.04. See the link below;<\/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\n\n\n<h2 class=\"wp-block-heading\">Setting up IPSec VPN Server with Libreswan<\/h2>\n\n\n\n<p>There are different <a rel=\"noreferrer noopener\" aria-label=\"VPN implementations of Libreswan (opens in a new tab)\" href=\"https:\/\/libreswan.org\/wiki\/Configuration_examples\" target=\"_blank\">VPN Server-client implementations of Libreswan<\/a>. In this guide, we are going to learn how setup IPSec VPN server for the mobile clients (clients with dynamically assigned IPs such as laptops) here in known as <strong><code>road warriors<\/code><\/strong>, so that they can be able to connect to local LAN from anywhere. Mobile clients are authenticated using certificates and hence uses the IKEv2 protocol.<\/p>\n\n\n\n<p><strong>IKEv2<\/strong>&nbsp;(Internet Key Exchange version 2) is a VPN encryption protocol that handles request and response actions.&nbsp;IKE performs mutual authentication between two parties and establishes an IKE security association (SA) that includes shared secret information that can be used to efficiently establish SAs for Encapsulating Security Payload (ESP) or Authentication Header (AH) and a set of cryptographic algorithms to be used by the SAs to protect the traffic that they carry.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Run system Update<\/h3>\n\n\n\n<p>Update your system packages on the server to be used as Libreswan VPN server.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf update<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"installlibreswanoncentos8\"><a href=\"#installlibreswanoncentos8\">Install Libreswan on CentOS 8<\/a><\/h3>\n\n\n\n<p>Once the update is done, install Libreswan. Libreswan is available on CentOS 8 AppStream repos and hence, you can simply install using the package manager as follows;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf install libreswan<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Running Libreswan<\/h3>\n\n\n\n<p>Once the installation is done, start and enable Libreswan <code><strong>ipsec<\/strong><\/code> service to run on system boot.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl enable --now ipsec<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Initialize IPSec NSS Database<\/h3>\n\n\n\n<p>Next, you need to initialize the Network Security Services (NSS) database. NSS database is used to store authentication keys and identity certificates.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ipsec initnss<\/code><\/pre>\n\n\n\n<p>If there is any previous database, you can remove it so that you can have a new database. The NSS database is stored under <code>\/etc\/ipsec.d<\/code>.<\/p>\n\n\n\n<p>To remove any old databases, stop IPsec, if running and remove NSS databases by running the commands below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl stop ipsec<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>rm -rf \/etc\/ipsec.d\/*db<\/code><\/pre>\n\n\n\n<p>You can then re-initialize the NSS database;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ipsec initnss<\/code><\/pre>\n\n\n\n<p>Then start IPSec;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl start ipsec<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Open Libreswan Ports and Protocols on Firewall<\/h3>\n\n\n\n<p>The&nbsp;<code>IKE<\/code>&nbsp;protocol uses <code>UDP port 500<\/code> and<code> 4500<\/code> while IPsec protocols, <code>Encapsulated Security Payload<\/code>&nbsp;(ESP) and <code>Authenticated Header<\/code>&nbsp;(AH) uses <code>protocol number 50 and 51<\/code> respectively.<\/p>\n\n\n\n<p>Hence, open these ports and protocols on your active firewall zone on your <strong>VPN (Left Endpoint) Server<\/strong> in this guide.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>firewall-cmd --get-active-zone<\/code><\/pre>\n\n\n\n<p>To open the ports and firewall on the default firewalld zone;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>firewall-cmd --add-port={4500,500}\/udp --permanent<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>firewall-cmd --add-protocol={50,51} --permanent<\/code><\/pre>\n\n\n\n<p>Or you can simply use the IPSec service;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>firewall-cmd --add-service=ipsec --permanent<\/code><\/pre>\n\n\n\n<p>Reload FirewallD<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>firewall-cmd --reload<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Configure IPSec VPN Server with Libreswan<\/h3>\n\n\n\n<p>Libreswan doesn&#8217;t use the client-server model. It however uses the terms&nbsp;<code>left<\/code>&nbsp;and&nbsp;<code>right<\/code>&nbsp;to refer to endpoints involved in any given connection. The left\/right terms can be used arbitrarily to refer to each system as long as you maintain consistency in using the terms while configuring your connections.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enable IP Forwarding<\/h3>\n\n\n\n<p>On both the <strong><code>VPN server<\/code><\/strong>, you need to enable IP forwarding.<\/p>\n\n\n\n<p>Run the command below to check if IP forwarding is enabled;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sysctl net.ipv4.ip_forward<\/code><\/pre>\n\n\n\n<p>If the output is <code><strong>net.ipv4.ip_forward = 0<\/strong><\/code>, then IP forwarding is disabled and you need to enable by executing either of the command below;<\/p>\n\n\n\n<p>IP forwarding can be enabled by just enabling IP masquerading on firewalld.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>firewall-cmd --add-masquerade --permanent\nfirewall-cmd --reload<\/code><\/pre>\n\n\n\n<p>You can the verify IP forwarding;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>cat \/proc\/sys\/net\/ipv4\/ip_forward\n<\/strong>1<\/code><\/pre>\n\n\n\n<p>A value of 1 means, IP forwarding is enabled.<\/p>\n\n\n\n<p>Similarly. you can enable IP forwarding by running the commands below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo \"net.ipv4.ip_forward = 1\" &gt;&gt; \/etc\/sysctl.conf<\/code><\/pre>\n\n\n\n<p>Refresh with the sysctl.conf with new configuration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sysctl -p<\/code><\/pre>\n\n\n\n<p>Also, ensure that redirects are disabled.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>less \/etc\/sysctl.d\/50-libreswan.conf<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n# We disable redirects for XFRM\/IPsec\nnet.ipv6.conf.default.accept_redirects = 0\nnet.ipv6.conf.all.accept_redirects = 0\nnet.ipv4.conf.default.send_redirects = 0\nnet.ipv4.conf.default.accept_redirects = 0\nnet.ipv4.conf.all.send_redirects = 0\nnet.ipv4.conf.all.accept_redirects = 0\nnet.ipv4.conf.all.rp_filter = 0\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"rsa-keys\">Generate VPN Server and Client Certificates<\/h3>\n\n\n\n<p>Next, you need to generate the VPN server and clients certificates for use in authentication.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create Certificates Generation Database<\/h4>\n\n\n\n<p>Run the command below to create a database that can be used to generate store a private key and CA certificate for use in generating hosts certificates. We will be using <code><strong>certutil<\/strong><\/code> command to generate the certificates.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mkdir \/etc\/ipsec.d\/certsdb<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>certutil -N -d sql:\/etc\/ipsec.d\/certsdb<\/code><\/pre>\n\n\n\n<p>The command prompts you to enter the password for encrypting your keys.<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\nEnter a password which will be used to encrypt your keys.\nThe password should be at least 8 characters long,\nand should contain at least one non-alphabetic character.\nEnter new password: <strong>StRONgPassw0Rd<\/strong>\nRe-enter password: <strong>StRONgPassw0Rd<\/strong>\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Generate CA Certificate<\/h4>\n\n\n\n<p>We use self signed certificates in this tutorial and hence, this is how we can generate our local CA certificate.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>certutil -S -x -n \"Kifarunix-demo CA\" -s \"O=Kifarunix-demo,CN=Kifarunix-demo CA\" -k rsa -g 4096 -v 12 -d sql:\/etc\/ipsec.d\/certsdb -t \"CT,,\" -2<\/code><\/pre>\n\n\n\n<p><strong>Refer to <code>man certutil<\/code> to learn about the options used.<\/strong><\/p>\n\n\n\n<p>When the command runs, you will be first prompted to enter the password for encrypting keys you set above. Enter the password to proceed.<\/p>\n\n\n\n<p>Next, you are required to generate random seed for use in creating of your keys by typing any keys on the keyboard until the progress meter is full. Once it is full, press enter to continue.<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\n...\nContinue typing until the progress meter is full:\n\n|************************************************************|\n\nFinished.  Press enter to continue: ENTER\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Next, type <strong>y<\/strong> to specify that this is a CA certificate being generated.<\/li>\n\n\n\n<li>Press ENTER for the path length<\/li>\n\n\n\n<li>Enter <strong>n<\/strong> to specify that this is not a critical extension.<\/li>\n<\/ul>\n\n\n\n<pre class=\"scroll-box\"><code>\nGenerating key.  This may take a few moments...\n\nIs this a CA certificate [y\/N]?\n<strong>y<\/strong>\nEnter the path length constraint, enter to skip [&lt;0 for unlimited path]: &gt; <strong>ENTER<\/strong>\nIs this a critical extension [y\/N]?\n<strong>n<\/strong>\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Generate the VPN Server Certificate<\/h4>\n\n\n\n<p> Next, generate the server certificate signed using the CA created above and assign extensions to it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>certutil -S -c \"Kifarunix-demo CA\" -n \"vpn.kifarunix-demo.com\" -s \"O=Kifarunix-demo,CN=vpn.kifarunix-demo.com\" -k rsa -g 4096 -v 12 -d sql:\/etc\/ipsec.d\/certsdb -t \",,\" -1 -6 -8 \"vpn.kifarunix-demo.com\"<\/code><\/pre>\n\n\n\n<p>Similarly, enter the keys encryption password, generate the seed from the keyboard and press ENTER to continue.<\/p>\n\n\n\n<p>Define the key and the key extension usage.<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\nGenerating key.  This may take a few moments...\n\n\t\t0 - Digital Signature\n\t\t1 - Non-repudiation\n\t\t2 - Key encipherment\n\t\t3 - Data encipherment\n\t\t4 - Key agreement\n\t\t5 - Cert signing key\n\t\t6 - CRL signing key\n\t\tOther to finish\n &gt; 0\n\t\t0 - Digital Signature\n\t\t1 - Non-repudiation\n\t\t2 - Key encipherment\n\t\t3 - Data encipherment\n\t\t4 - Key agreement\n\t\t5 - Cert signing key\n\t\t6 - CRL signing key\n\t\tOther to finish\n &gt; 2\n\t\t0 - Digital Signature\n\t\t1 - Non-repudiation\n\t\t2 - Key encipherment\n\t\t3 - Data encipherment\n\t\t4 - Key agreement\n\t\t5 - Cert signing key\n\t\t6 - CRL signing key\n\t\tOther to finish\n &gt; 8\n<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nIs this a critical extension [y\/N]?\nn\n\t\t0 - Server Auth\n\t\t1 - Client Auth\n\t\t2 - Code Signing\n\t\t3 - Email Protection\n\t\t4 - Timestamp\n\t\t5 - OCSP Responder\n\t\t6 - Step-up\n\t\t7 - Microsoft Trust List Signing\n\t\tOther to finish\n &gt; 0\n\t\t0 - Server Auth\n\t\t1 - Client Auth\n\t\t2 - Code Signing\n\t\t3 - Email Protection\n\t\t4 - Timestamp\n\t\t5 - OCSP Responder\n\t\t6 - Step-up\n\t\t7 - Microsoft Trust List Signing\n\t\tOther to finish\n &gt; 8\nIs this a critical extension [y\/N]?\nN\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Generate the VPN Client Certificate<\/h4>\n\n\n\n<p>Run the command below to generate a VPN client certificate. Replace the name of the certificate (hostname used here) with the name of the host whose client certificate you are generating for;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>certutil -S -c \"Kifarunix-demo CA\" -n \"janedoe.kifarunix-demo.com\" -s \"O=Kifarunix-demo,CN=janedoe.kifarunix-demo.com\" -k rsa -g 4096 -v 12 -d sql:\/etc\/ipsec.d\/certsdb -t \",,\" -1 -6 -8 \"janedoe.kifarunix-demo.com\"<\/code><\/pre>\n\n\n\n<p>Similarly, enter the same options as above.<\/p>\n\n\n\n<p>Listing the Available Certificates in the database<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>certutil -L -d sql:\/etc\/ipsec.d\/certsdb<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nCertificate Nickname                                         Trust Attributes\n                                                             SSL,S\/MIME,JAR\/XPI\n\nKifarunix-demo CA                                            CTu,u,u\nvpn.kifarunix-demo.com                                       u,u,u\njanedoe.kifarunix-demo.com                                   u,u,u\njohndoe.kifarunix-demo.com                                   u,u,u\n<\/code><\/pre>\n\n\n\n<p>Export and import the gateway certificate into the pluto DB.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>pk12util -o vpn.kifarunix-demo.com.p12 -n \"vpn.kifarunix-demo.com\" -d sql:\/etc\/ipsec.d\/certsdb<\/code><\/pre>\n\n\n\n<p>You can skip the PKCS12 password.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vpn.kifarunix-demo.com.p12<\/code><\/pre>\n\n\n\n<p>Once exported, Import the VPN server certificate to DB.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ipsec import vpn.kifarunix-demo.com.p12<\/code><\/pre>\n\n\n\n<p>Export the client host certificates, private key, and CA certificate. All these will be stored in a .p12 file as specified output file in the command below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>pk12util -o janedoe.kifarunix-demo.com.p12 -n \"janedoe.kifarunix-demo.com\" -d sql:\/etc\/ipsec.d\/certsdb<\/code><\/pre>\n\n\n\n<p>Similarly, skip the PKCS12 password.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>janedoe.kifarunix-demo.com.p12 vpn.kifarunix-demo.com.p12<\/strong><\/code><\/pre>\n\n\n\n<p>If you have generated certificates for other client hosts, you can as well export them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create IPSec VPN Endpoint Configuration file<\/h3>\n\n\n\n<p>On your IPSec VPN host, create a configuration file on <strong><code>\/etc\/ipsec.d<\/code><\/strong> directory for your mobile clients. <code><strong>\/etc\/ipsec.conf<\/strong><\/code> is the default configuration file for Libreswan and it has a directive to include other configurations defined on <strong><code>\/etc\/ipsec.d<\/code><\/strong> directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/ipsec.d\/mobile-clients.conf<\/code><\/pre>\n\n\n\n<p>Put the following configurations on the file above.<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\nconn roadwarriors\n    left=vpn.kifarunix-demo.com\n    leftsubnet=0.0.0.0\/0\n    leftcert=vpn.kifarunix-demo.com\n    leftid=%fromcert\n    leftrsasigkey=%cert\n    leftsendcert=always\n    right=%any\n    rightaddresspool=10.0.8.10-10.0.8.254\n    rightca=%same\n    rightrsasigkey=%cert\n    modecfgdns=\"8.8.8.8,10.0.8.1\"\n    authby=rsasig\n    auto=start\n    dpddelay=60\n    dpdtimeout=300\n    dpdaction=clear\n    mobike=yes\n    ikev2=insist\n    fragmentation=yes\n    type=tunnel\n<\/code><\/pre>\n\n\n\n<p><strong>Refer to <code>man ipsec.conf<\/code> for a comprehensive description of the options used above.<\/strong><\/p>\n\n\n\n<p>Verify the configuration file for any errors;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/usr\/libexec\/ipsec\/addconn --config \/etc\/ipsec.conf --checkconfig<\/code><\/pre>\n\n\n\n<p>If there is no error, command exit with 0 status.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo $?<\/code><\/pre>\n\n\n\n<p>Otherwise, any error is displayed on the standard output. Fix the errors before you can proceed.<\/p>\n\n\n\n<p>Enable IPsec logging by uncommenting the line, <code><strong>#logfile=\/var\/log\/pluto.log<\/strong><\/code>, on the <code><strong>\/etc\/ipsec.conf<\/strong><\/code> configuration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>config setup\n        # Normally, pluto logs via syslog.\n        <strong>logfile=\/var\/log\/pluto.log<\/strong>\n...<\/code><\/pre>\n\n\n\n<p>Restart IPsec;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart ipsec<\/code><\/pre>\n\n\n\n<p>Check status;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl status ipsec<\/code><\/pre>\n\n\n\n<p>If ipsec fails to start, there must be a configuration syntax error. Run the command below to pinpoint the error.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>journalctl -xe<\/code><\/pre>\n\n\n\n<p>Disable rp_filter for Libreswan and reload all Kernel configurations.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo \"net.ipv4.conf.all.rp_filter = 0\" &gt;&gt; \/etc\/sysctl.d\/50-libreswan.conf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sysctl --system<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Verify IPsec Configuration<\/h3>\n\n\n\n<p>To confirm that the IPsec configuration is fine, simply run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ipsec verify<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nVerifying installed system and configuration files\n\nVersion check and ipsec on-path                   \t[OK]\nLibreswan 3.29 (netkey) on 4.18.0-193.6.3.el8_2.x86_64\nChecking for IPsec support in kernel              \t[OK]\n NETKEY: Testing XFRM related proc values\n         ICMP default\/send_redirects              \t[OK]\n         ICMP default\/accept_redirects            \t[OK]\n         XFRM larval drop                         \t[OK]\nPluto ipsec.conf syntax                           \t[OK]\nChecking rp_filter                                \t[OK]\nChecking that pluto is running                    \t[OK]\n Pluto listening for IKE on udp 500               \t[OK]\n Pluto listening for IKE\/NAT-T on udp 4500        \t[OK]\n Pluto ipsec.secret syntax                        \t[OK]\nChecking 'ip' command                             \t[OK]\nChecking 'iptables' command                       \t[OK]\nChecking 'prelink' command does not interfere with FIPS\t[OK]\nChecking for obsolete ipsec.conf options          \t[OK]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Copy VPN Client Certificates to your Mobile Clients<\/h3>\n\n\n\n<p>You can now<strong> copy the client certificates to your remote clients <\/strong>and connect to the VPN server.<\/p>\n\n\n\n<p>In this tutorial, we are using Ubuntu 20.04 and Ubuntu 18.04 systems as our remote clients.<\/p>\n\n\n\n<p>We have generated certificates for two hosts, <code>janedoe.kifarunix-demo.com<\/code> and <code>johndoe.kifarunix-demo.com<\/code> for Ubuntu 20.04 and Ubuntu 18.04 client hosts respectively.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Configuring Libreswan Client<\/h3>\n\n\n\n<p>On your Ubuntu system, install libreswan package.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt update<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install libreswan<\/code><\/pre>\n\n\n\n<p>Create Libreswan Client VPN connection configuration file<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/ipsec.d\/johndoe.conf<\/code><\/pre>\n\n\n\n<p>Enter the content below;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\nconn vpn.kifarunix-demo.com\n\tleft=%defaultroute\n\tleftcert=johndoe.kifarunix-demo.com\n\tleftid=%fromcert\n\tleftrsasigkey=%cert\n\tleftsubnet=0.0.0.0\/0 \n\tleftmodecfgclient=yes\n\tright=vpn.kifarunix-demo.com\n\trightsubnet=0.0.0.0\/0 \n\trightid=@vpn.kifarunix-demo.com\n\trightrsasigkey=%cert\n\tnarrowing=yes\n\tikev2=insist\n\trekey=yes\n\tfragmentation=yes\n\tmobike=no\n\tauto=start\n<\/code><\/pre>\n\n\n\n<p><strong>If you are using hostnames, ensure that they are resolvable.<\/strong><\/p>\n\n\n\n<p>Similarly, on your second host;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/ipsec.d\/janedoe.conf<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nconn vpn.kifarunix-demo.com\n\tleft=%defaultroute\n\tleftcert=janedoe.kifarunix-demo.com\n\tleftid=%fromcert\n\tleftrsasigkey=%cert\n\tleftsubnet=0.0.0.0\/0 \n\tleftmodecfgclient=yes\n\tright=vpn.kifarunix-demo.com\n\trightsubnet=0.0.0.0\/0 \n\trightid=@vpn.kifarunix-demo.com\n\trightrsasigkey=%cert\n\tnarrowing=yes\n\tikev2=insist\n\trekey=yes\n\tfragmentation=yes\n\tmobike=no\n\tauto=start\n<\/code><\/pre>\n\n\n\n<p>Check the configuration syntax;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/usr\/lib\/ipsec\/addconn --config \/etc\/ipsec.conf --checkconfig<\/code><\/pre>\n\n\n\n<p>Initialize NSS database;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo ipsec checknss<\/code><\/pre>\n\n\n\n<p>&nbsp;Import PKCS#12 X.509 certificate files into the NSS database;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo ipsec import janedoe.kifarunix-demo.com.p12<\/code><\/pre>\n\n\n\n<p>Do the same on the other client host. Press ENTER to skip the PCKS12 password.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Enter password for PKCS12 file: <strong>ENTER<\/strong>\npk12util: PKCS12 IMPORT SUCCESSFUL\ncorrecting trust bits for Kifarunix-demo CA<\/code><\/pre>\n\n\n\n<p>You can list available certificates on the client host;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>certutil -L -d sql:\/var\/lib\/ipsec\/nss<\/code><\/pre>\n\n\n\n<p>Start IPsec and enable it to run on system boot.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo ipsec setup start<\/code><\/pre>\n\n\n\n<p>Check the status;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl status ipsec<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n\u25cf ipsec.service - Internet Key Exchange (IKE) Protocol Daemon for IPsec\n     Loaded: loaded (\/lib\/systemd\/system\/ipsec.service; enabled; vendor preset: disabled)\n     Active: active (running) since Sun 2020-06-21 12:28:02 EAT; 7min ago\n       Docs: man:ipsec(8)\n             man:pluto(8)\n             man:ipsec.conf(5)\n    Process: 11251 ExecStartPre=\/usr\/lib\/ipsec\/addconn --config \/etc\/ipsec.conf --checkconfig (code=exited, status=0\/SUCCESS)\n    Process: 11266 ExecStartPre=\/usr\/lib\/ipsec\/_stackmanager start (code=exited, status=0\/SUCCESS)\n    Process: 11742 ExecStartPre=\/usr\/sbin\/ipsec --checknss (code=exited, status=0\/SUCCESS)\n    Process: 11743 ExecStartPre=\/usr\/sbin\/ipsec --checknflog (code=exited, status=0\/SUCCESS)\n   Main PID: 11757 (pluto)\n     Status: \"Startup completed.\"\n      Tasks: 2 (limit: 2319)\n     Memory: 10.3M\n     CGroup: \/system.slice\/ipsec.service\n             \u2514\u250011757 \/usr\/lib\/ipsec\/pluto --leak-detective --config \/etc\/ipsec.conf --nofork\n\nJun 21 12:28:02 koromicha pluto[11757]: \"vpn.kifarunix-demo.com\"[1] 192.168.56.133 #2: certificate verified OK: O=Kifarunix-demo,CN=vpn.kifarunix-demo&gt;\nJun 21 12:28:02 koromicha pluto[11757]: \"vpn.kifarunix-demo.com\"[1] 192.168.56.133 #2: IKEv2 mode peer ID is ID_DER_ASN1_DN: 'CN=vpn.kifarunix-demo.co&gt;\nJun 21 12:28:02 koromicha pluto[11757]: \"vpn.kifarunix-demo.com\"[1] 192.168.56.133 #2: Authenticated using RSA\n<strong>Jun 21 12:28:02 koromicha pluto[11757]: \"vpn.kifarunix-demo.com\"[1] 192.168.56.133 #2: received INTERNAL_IP4_ADDRESS 10.0.8.10<\/strong>\nJun 21 12:28:02 koromicha pluto[11757]: \"vpn.kifarunix-demo.com\"[1] 192.168.56.133 #2: received INTERNAL_IP4_DNS 8.8.8.8\nJun 21 12:28:02 koromicha pluto[11757]: \"vpn.kifarunix-demo.com\"[1] 192.168.56.133 #2: received INTERNAL_IP4_DNS 10.0.8.1\nJun 21 12:28:02 koromicha pluto[11757]: \"vpn.kifarunix-demo.com\"[1] 192.168.56.133 #2: up-client output: updating resolvconf\nJun 21 12:28:02 koromicha pluto[11757]: \"vpn.kifarunix-demo.com\"[1] 192.168.56.133 #2: negotiated connection [10.0.8.10-10.0.8.10:0-65535 0] -&gt; [0.0.0&gt;\nJun 21 12:28:02 koromicha pluto[11757]: \"vpn.kifarunix-demo.com\"[1] 192.168.56.133 #2: STATE_V2_IPSEC_I: IPsec SA established tunnel mode {ESP\/NAT=\n<\/code><\/pre>\n\n\n\n<p>Check the status on the other remote host.<\/p>\n\n\n\n<p>From the status output, you can check assigned IP address, <strong>INTERNAL_IP4_ADDRESS 10.0.8.10<\/strong>. On my other client host, <strong>INTERNAL_IP4_ADDRESS 10.0.8.11<\/strong>.<\/p>\n\n\n\n<p>Load the connection on each client host;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo ipsec auto --add vpn.kifarunix-demo.com<\/code><\/pre>\n\n\n\n<p>Establish the tunnel by bringing up the connection on each host;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo ipsec auto --up vpn.kifarunix-demo.com<\/code><\/pre>\n\n\n\n<p>From each host, ping the VPN assigned IP address of the other.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ping 10.0.8.11 -c 4<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nPING 10.0.8.11 (10.0.8.11) 56(84) bytes of data.\n64 bytes from 10.0.8.11: icmp_seq=1 ttl=63 time=2.82 ms\n64 bytes from 10.0.8.11: icmp_seq=2 ttl=63 time=2.84 ms\n64 bytes from 10.0.8.11: icmp_seq=3 ttl=63 time=3.06 ms\n64 bytes from 10.0.8.11: icmp_seq=4 ttl=63 time=2.83 ms\n\n--- 10.0.8.11 ping statistics ---\n4 packets transmitted, 4 received, 0% packet loss, time 3010ms\nrtt min\/avg\/max\/mdev = 2.820\/2.888\/3.060\/0.099 ms\n<\/code><\/pre>\n\n\n\n<p>On the other host;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ping 10.0.8.10 -c 4<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nPING 10.0.8.10 (10.0.8.10) 56(84) bytes of data.\n64 bytes from 10.0.8.10: icmp_seq=1 ttl=63 time=1.63 ms\n64 bytes from 10.0.8.10: icmp_seq=2 ttl=63 time=2.38 ms\n64 bytes from 10.0.8.10: icmp_seq=3 ttl=63 time=3.18 ms\n64 bytes from 10.0.8.10: icmp_seq=4 ttl=63 time=2.86 ms\n\n--- 10.0.8.10 ping statistics ---\n4 packets transmitted, 4 received, 0% packet loss, time 3005ms\nrtt min\/avg\/max\/mdev = 1.631\/2.516\/3.187\/0.588 ms\n<\/code><\/pre>\n\n\n\n<p>The two remote hosts can now communicate via IPsec VPN server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Further Reading\/Reference<\/h3>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/libreswan.org\/wiki\/Configuration_examples\" target=\"_blank\">Libreswan Configuration Examples<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Related Tutorials<\/h3>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/install-and-setup-openvpn-server-on-ubuntu-20-04\/\" target=\"_blank\">Install and Setup OpenVPN Server on Ubuntu 20.04<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/configure-openvpn-ldap-based-authentication\/\" target=\"_blank\">Configure OpenVPN LDAP Based Authentication<\/a><\/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><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/setup-ipsec-vpn-using-strongswan-on-debian-10\/\" target=\"_blank\">Setup IPSEC VPN using StrongSwan on Debian 10<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/connect-to-cisco-vpn-using-pcf-file-on-ubuntu\/\" target=\"_blank\" rel=\"noreferrer noopener\">Connect to Cisco VPN Using PCF file on Ubuntu<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to our today&#8217;s guide on how to setup IPSec VPN server with Libreswan on CentOS 8. Libreswan is a free implementation of IKE\/IPsec for<\/p>\n","protected":false},"author":1,"featured_media":12280,"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":[1712,1709,1713,1711,1716,1715,1714,1710],"class_list":["post-6221","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-openvpn","category-security","category-vpn","tag-ikev2-vpn-setup","tag-install-libreswan-on-centos-8","tag-install-libreswan-on-ubuntu-18-04-20-04","tag-ipsec-vpn-on-centos-8","tag-libreswan","tag-libreswan-road-warriror","tag-setup-ipsec-vpn-roadwarrior","tag-setup-ipsec-vpn-with-libreswan","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6221"}],"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=6221"}],"version-history":[{"count":6,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6221\/revisions"}],"predecessor-version":[{"id":21419,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6221\/revisions\/21419"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/12280"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=6221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=6221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=6221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}