{"id":5245,"date":"2015-10-04T14:26:23","date_gmt":"2015-10-04T06:26:23","guid":{"rendered":"http:\/\/rmohan.com\/?p=5245"},"modified":"2015-10-04T14:26:23","modified_gmt":"2015-10-04T06:26:23","slug":"setup-your-own-private-network-with-openvpn","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5245","title":{"rendered":"Setup Your Own Private Network With OpenVPN"},"content":{"rendered":"<p>private network connectivity for servers running at the same location. But sometimes you want two servers in different countries \/ datacenters to be able to communicate in a private and secure way. This tutorial will show you how to achieve that with the help of OpenVPN. The operating systems used here are Debian and CentOS, just to show you two different configurations. This can be easily adapted for Debian -> Debian, Ubuntu -> FreeBSD and so on.<\/p>\n<p>Machine 1: Debian, will act as server (Location: NL)<br \/>\nMachine 2: CentOS, will act as client (Location: FR)<br \/>\nMachine 1<br \/>\nStart on machine 1 by installing OpenVPN:<\/p>\n<p>apt-get install openvpn<br \/>\nThen, copy the example configuration and the tool for generating keys, easy-rsa, to \/etc\/openvpn:<\/p>\n<p>cp -r \/usr\/share\/doc\/openvpn\/examples\/easy-rsa\/ \/usr\/share\/doc\/openvpn\/examples\/sample-config-files\/server.conf.gz \/etc\/openvpn<br \/>\nThe default values for your keys aren&#8217;t exactly safe anymore, to fix this open \/etc\/openvpn\/easy-rsa\/2.0\/vars with your favorite text editor and modify the following line:<\/p>\n<p>export KEY_SIZE=4096<br \/>\nNext, ensure that the values are loaded into your current session, clean up eventually existing keys, and generate your certificate authority:<\/p>\n<p>cd \/etc\/openvpn\/easy-rsa\/2.0<br \/>\nsource .\/vars<br \/>\n.\/clean-all<br \/>\n.\/build-ca<br \/>\nYou will be prompted for information. Make your life easier by supplying information about your server, for example, where it&#8217;s located and what the FQDN is\/will be. This is useful for when you have to debug problems:<\/p>\n<p>Country Name (2 letter code) [US]:NL<br \/>\nState or Province Name (full name) [CA]:-<br \/>\nLocality Name (eg, city) [SanFrancisco]:Vultr Datacenter NL<br \/>\nOrganization Name (eg, company) [Fort-Funston]:-<br \/>\nOrganizational Unit Name (eg, section) [changeme]:-<br \/>\nCommon Name (eg, your name or your server&#8217;s hostname) [changeme]:yourserver1.yourdomain.tld<br \/>\nName [changeme]:-<br \/>\nEmail Address [mail@host.domain]:youraddress@yourdomain.tld<br \/>\nAnother necessity is parameters for the Diffie-Hellman key exchange. Those need to be generated too:<\/p>\n<p>.\/build-dh<br \/>\nImportant: The build-dh command is a relatively complex process that can take up to ten minutes, depending on your server&#8217;s resources.<\/p>\n<p>To further improve the security of this connection, we will generate a static secret that needs to be distributed amongst all clients:<\/p>\n<p>mkdir \/etc\/openvpn\/keys<br \/>\nopenvpn &#8211;genkey &#8211;secret \/etc\/openvpn\/keys\/ta.key<br \/>\nNow, you can generate the key for the server:<\/p>\n<p>.\/build-key-server server1<br \/>\nThis command will prompt for some information:<\/p>\n<p>Country Name (2 letter code) [US]:NL<br \/>\nState or Province Name (full name) [CA]:-<br \/>\nLocality Name (eg, city) [SanFrancisco]:Vultr Datacenter NL<br \/>\nOrganization Name (eg, company) [Fort-Funston]:-<br \/>\nOrganizational Unit Name (eg, section) [changeme]:-<br \/>\nCommon Name (eg, your name or your server&#8217;s hostname) [server1]:yourserver1.yourdomain.tld<br \/>\nName [changeme]:-<br \/>\nEmail Address [mail@host.domain]:youraddress@yourdomain.tld<br \/>\nThe final step is to sign the certificate request that was just generated with the CA&#8217;s key:<\/p>\n<p>1 out of 1 certificate requests certified, commit? [y\/n]y<br \/>\nCopy the necessary keys and certificates into a separate folder:<\/p>\n<p>cd \/etc\/openvpn\/easy-rsa\/2.0\/keys<br \/>\ncp dh4096.pem ca.crt server1.crt server1.key \/etc\/openvpn\/keys\/<br \/>\nchmod 700 \/etc\/openvpn\/keys<br \/>\nchmod 600 \/etc\/openvpn\/keys\/*<br \/>\nNow for the configuration, unzip it &#8230;<\/p>\n<p>cd \/etc\/openvpn<br \/>\ngunzip server.conf.gz<br \/>\n&#8230; and open the resulting server.conf with your favorite text editor. The configuration should look similar to this:<\/p>\n<p>port 1194<br \/>\nproto udp<br \/>\ndev tun<\/p>\n<p>ca keys\/ca.crt<br \/>\ncert keys\/server1.crt<br \/>\nkey keys\/server1.key<br \/>\ndh keys\/dh4096.pem<br \/>\nserver 10.8.100.0 255.255.255.0<br \/>\nifconfig-pool-persist ipp.txt<\/p>\n<p># Uncomment this if you have multiple clients<br \/>\n# and want them to be able to see each other<br \/>\n;client-to-client<\/p>\n<p>keepalive 10 120<br \/>\ntls-auth keys\/ta.key 0 <\/p>\n<p>tls-cipher DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-CAMELLIA256-SHA:DHE-RSA-AES256-SHA:DHE-RSA-CAMELLIA128-SHA:DHE-RSA-AES128-SHA:CAMELLIA256-SHA:AES256-SHA:CAMELLIA128-SHA:AES128-SHA<br \/>\ncipher AES-256-CBC<br \/>\nauth SHA384<br \/>\ncomp-lzo<\/p>\n<p>user nobody<br \/>\ngroup nogroup<\/p>\n<p>persist-key<br \/>\npersist-tun<br \/>\nverb 3<br \/>\nmute 20<br \/>\nAfter restarting the service you should watch your log a bit &#8230;<\/p>\n<p>service openvpn restart &#038;&#038; tail -f \/var\/log\/syslog<br \/>\n&#8230; to make sure everything is working. If no errors are detected, then you can generate the keys for your second server:<\/p>\n<p>cd \/etc\/openvpn\/easy-rsa\/2.0<br \/>\nsource .\/vars<br \/>\n.\/build-key server2<br \/>\nAgain, you will be prompted for information:<\/p>\n<p>Country Name (2 letter code) [US]:FR<br \/>\nState or Province Name (full name) [CA]:-<br \/>\nLocality Name (eg, city) [SanFrancisco]:Vultr Datacenter FR<br \/>\nOrganization Name (eg, company) [Fort-Funston]:-<br \/>\nOrganizational Unit Name (eg, section) [changeme]:-<br \/>\nCommon Name (eg, your name or your server&#8217;s hostname)<br \/>\n[server2]:yourserver2.yourdomain.tld<br \/>\nName [changeme]:-<br \/>\nEmail Address [mail@host.domain]:youraddress@yourdomain.tld<br \/>\nNow, you need to transfer the necessary files to your second server, preferably encrypted:<\/p>\n<p>cd \/etc\/openvpn\/easy-rsa\/2.0\/keys<br \/>\ncp \/etc\/openvpn\/keys\/ta.key .<br \/>\ntar -cf vpn.tar ca.crt server2.crt server2.key ta.key<br \/>\nscp vpn.tar yourusername@server2:~\/<br \/>\nrm vpn.tar<br \/>\nMachine 2<br \/>\nTime to switch to the SSH-connection of your second server. The first step is to install OpenVPN &#8230;<\/p>\n<p>yum install openvpn<br \/>\n&#8230; and to deactivate firewalld. The replacement will be plain iptables.<\/p>\n<p>systemctl stop firewalld<br \/>\nsystemctl disable firewalld<br \/>\nUnpack the archive that you just moved to the server and properly set permissions on the files:<\/p>\n<p>cd \/etc\/openvpn<br \/>\nmkdir keys<br \/>\nchmod 700 keys<br \/>\ncd keys<br \/>\ntar -xf ~\/vpn.tar -C .<br \/>\nchmod 600 *<br \/>\nCreate \/etc\/openvpn\/client.conf with your favorite text editor. It should look like this:<\/p>\n<p>client<br \/>\ndev tun<br \/>\nproto udp<\/p>\n<p>remote yourserver yourport<br \/>\nresolv-retry infinite<br \/>\nnobind<br \/>\nuser nobody<br \/>\ngroup openvpn<\/p>\n<p>persist-key<br \/>\npersist-tun<\/p>\n<p>ca keys\/ca.crt<br \/>\ncert keys\/server2.crt<br \/>\nkey keys\/.key<\/p>\n<p>ns-cert-type server<br \/>\ntls-auth keys\/ta.key 1<\/p>\n<p>tls-cipher DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-CAMELLIA256-SHA:DHE-RSA-AES256-SHA:DHE-RSA-CAMELLIA128-SHA:DHE-RSA-AES128-SHA:CAMELLIA256-SHA:AES256-SHA:CAMELLIA128-SHA:AES128-SHA<br \/>\ncipher AES-256-CBC<br \/>\nauth SHA384<\/p>\n<p>remote-cert-tls server<\/p>\n<p>comp-lzo<br \/>\nverb 3<br \/>\nmute 20<br \/>\nThe last step is to start and enable the service:<\/p>\n<p>systemctl start openvpn@client.service<br \/>\nsystemctl enable openvpn@client.service<br \/>\nIf everything is working, then you should have no problem pinging the first server:<\/p>\n<p>PING 10.8.100.1 (10.8.100.1) 56(84) bytes of data.<br \/>\n64 bytes from 10.8.100.1: icmp_seq=1 ttl=64 time=17.8 ms<br \/>\n64 bytes from 10.8.100.1: icmp_seq=2 ttl=64 time=17.9 ms<br \/>\n64 bytes from 10.8.100.1: icmp_seq=3 ttl=64 time=17.8 ms<br \/>\nYou now have a private connection over the Internet!<\/p>\n<p>If you need to troubleshoot any errors, try checking the logs with the following command:<\/p>\n<p>journalctl -xn<\/p>\n","protected":false},"excerpt":{"rendered":"<p>private network connectivity for servers running at the same location. But sometimes you want two servers in different countries \/ datacenters to be able to communicate in a private and secure way. This tutorial will show you how to achieve that with the help of OpenVPN. The operating systems used here are Debian and CentOS, [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5245"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5245"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5245\/revisions"}],"predecessor-version":[{"id":5246,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5245\/revisions\/5246"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}