{"id":734,"date":"2014-07-14T15:42:29","date_gmt":"2014-07-14T08:42:29","guid":{"rendered":"http:\/\/whplus.com\/blog\/?p=734"},"modified":"2015-01-29T17:03:01","modified_gmt":"2015-01-29T10:03:01","slug":"setup-and-configure-an-openvpn-server-on-centos-6","status":"publish","type":"post","link":"https:\/\/www.whplus.com\/blog\/2014\/07\/14\/setup-and-configure-an-openvpn-server-on-centos-6.html","title":{"rendered":"Setup and Configure an OpenVPN Server on CentOS 6"},"content":{"rendered":"<p>This article will guide you through the setup and configuration of OpenVPN server on your CentOS 6 cloud server. We also cover how to configure openvpn client to connect to your newly installed OpenVPN server.<\/p>\n<p>Before we begin, you&#8217;ll need to have the Extra Packages for Enterprise Linux (EPEL) Repository enabled on your cloud server. This is a third party repository offered by the Fedora Project which will provide the OpenVPN package.<\/p>\n<p><code>wget http:\/\/dl.fedoraproject.org\/pub\/epel\/6\/i386\/epel-release-6-8.noarch.rpm<br \/>\nrpm -Uvh epel-release-6-8.noarch.rpm<\/code><\/p>\n<p><strong>Initial OpenVPN Configuration<\/strong><br \/>\nFirst, install the OpenVPN and easy-rsa packages :<\/p>\n<p><code>yum -y install openvpn easy-rsa<\/code><br \/>\n<!--more--><\/p>\n<p>OpenVPN ships with only a sample configuration, so we will copy the configuration file to its destination:<\/p>\n<p><code>cp \/usr\/share\/doc\/openvpn-*\/sample\/sample-config-files\/server.conf \/etc\/openvpn\/<\/code><\/p>\n<p>Now that we have the file in the proper location, open it for editing:<\/p>\n<p><code>nano -w \/etc\/openvpn\/server.conf<\/code><\/p>\n<p>Our first change will be to uncomment the &#8220;push&#8221; parameter which causes traffic on our client systems to be routed through OpenVPN.<\/p>\n<p><code>push \"redirect-gateway def1 bypass-dhcp\"<\/code><\/p>\n<p>We&#8217;ll also want to change the section that immediately follows route DNS queries to Google&#8217;s Public DNS servers.<\/p>\n<p><code>push \"dhcp-option DNS 8.8.8.8\"<br \/>\npush \"dhcp-option DNS 8.8.4.4\"<\/code><\/p>\n<p>In addition, to enhance security, make sure OpenVPN drops privileges after startup. Uncomment the relevant &#8220;user&#8221; and &#8220;group&#8221; lines.<\/p>\n<p><code>user nobody<br \/>\ngroup nobody<\/code><\/p>\n<p>Generating Keys and Certificates Using easy-rsa<\/p>\n<p>Now that we&#8217;ve finished modifying the configuration file, we&#8217;ll generate the required keys and certificates. As with the configuration file, OpenVPN places the required scripts in the documentation folder by default. Create the required folder and copy the files over.<\/p>\n<p><code>mkdir -p \/etc\/openvpn\/easy-rsa\/keys<br \/>\ncp -rf \/usr\/share\/openvpn\/easy-rsa\/2.0\/* \/etc\/openvpn\/easy-rsa<\/code><\/p>\n<p>With the files in the desired location, we&#8217;ll edit the &#8220;vars&#8221; file which provides the easy-rsa scripts with required information.<\/p>\n<p><code>nano -w \/etc\/openvpn\/easy-rsa\/vars<\/code><\/p>\n<p>We&#8217;re looking to modify the &#8220;KEY_&#8221; variables, located at the bottom of the file. The variable names are fairly descriptive and should be filled out with the applicable information.<\/p>\n<p>Once completed, the bottom of your &#8220;vars&#8221; file should appear similar to the following:<\/p>\n<p><code>export KEY_COUNTRY=\"US\"<br \/>\nexport KEY_PROVINCE=\"NY\"<br \/>\nexport KEY_CITY=\"New York\"<br \/>\nexport KEY_ORG=\"Organization Name\"<br \/>\nexport KEY_EMAIL=\"administrator@example.com\"<br \/>\nexport KEY_CN=droplet.example.com<br \/>\nexport KEY_NAME=server<br \/>\nexport KEY_OU=server<br \/>\n<\/code><br \/>\nOpenVPN might fail to properly detect the OpenSSL version on CentOS 6. As a precaution, manually copy the required OpenSSL configuration file.<\/p>\n<p><code>cp \/etc\/openvpn\/easy-rsa\/openssl-1.0.0.cnf \/etc\/openvpn\/easy-rsa\/openssl.cnf<\/code><\/p>\n<p>We&#8217;ll now change into our working directory and build our Certificate Authority, or CA, based on the information provided above.<\/p>\n<p><code>cd \/etc\/openvpn\/easy-rsa<br \/>\nsource .\/vars<br \/>\n.\/clean-all<br \/>\n.\/build-ca<\/code><\/p>\n<p>Now that we have our CA, we&#8217;ll create our certificate for the OpenVPN server. When asked by build-key-server, answer yes to commit.<\/p>\n<p><code>.\/build-key-server server<\/code><\/p>\n<p>We&#8217;re also going to need to generate our Diffie Hellman key exchange files using the build-dh script and copy all of our files into \/etc\/openvpn as follows:<\/p>\n<p><code>.\/build-dh<br \/>\ncd \/etc\/openvpn\/easy-rsa\/keys<br \/>\ncp dh2048.pem ca.crt server.crt server.key \/etc\/openvpn<br \/>\n<\/code><br \/>\nIn order to allow clients to authenticate, we&#8217;ll need to create client certificates. You can repeat this as necessary to generate a unique certificate and key for each client or device. If you plan to have more than a couple certificate pairs be sure to use descriptive filenames.<\/p>\n<p><code>cd \/etc\/openvpn\/easy-rsa<br \/>\n.\/build-key client<br \/>\n<\/code><br \/>\nRouting Configuration and Starting OpenVPN Server<\/p>\n<p>Create an iptables rule to allow proper routing of our VPN subnet.<\/p>\n<p>Xen and KVM users use:<br \/>\n<code>iptables -t nat -A POSTROUTING -s 10.8.0.0\/24 -o eth0 -j MASQUERADE<\/code><\/p>\n<p>And for OpenVZ use these two instead:<br \/>\n<code>iptables -t nat -A POSTROUTING -o venet0 -j SNAT --to-source 123.123.123.123<\/code><br \/>\n<code>iptables -t nat -A POSTROUTING -s 10.8.0.0\/24 -j SNAT --to-source 123.123.123.123<\/code><\/p>\n<p>Do not forget to replace 123.123.123.123 with your server IP.<br \/>\nNow save that iptables rules:<br \/>\n<code>service iptables save<\/code><\/p>\n<p>Then, enable IP Forwarding in sysctl:<\/p>\n<p><code>nano -w \/etc\/sysctl.conf<\/code><\/p>\n<p><code># Controls IP packet forwarding<br \/>\nnet.ipv4.ip_forward = 1<\/code><\/p>\n<p>Finally, apply our new sysctl settings. Start the server and assure that it starts automatically on boot:<\/p>\n<p><code>sysctl -p<br \/>\nservice openvpn start<br \/>\nchkconfig openvpn on<br \/>\n<\/code><br \/>\nYou now have a working OpenVPN server. In the following steps, we&#8217;ll discuss how to properly configure your client.<\/p>\n<p><strong>Configuring OpenVPN Client<\/strong><\/p>\n<p>Now that your OpenVPN server is online, lets configure your client to connect. The steps are largely the same regardless of what operating system you have.<\/p>\n<p>In order to proceed, we will need to retrieve the ca.crt, client.crt and client.key files from the remote server. Simply use your favorite SFTP\/SCP (Secure File Transfer Protocol\/Secure Copy) client and move them to a local directory. You can alternatively open the files in nano and copy the contents to local files manually. Be aware that the client.crt and client.key files will are automatically named based on the parameters used with &#8220;.\/build-key&#8221; earlier. All of the necessary files are located in \/etc\/openvpn\/easy-rsa\/keys<\/p>\n<p><code>nano -w \/etc\/openvpn\/easy-rsa\/keys\/ca.crt<br \/>\nnano -w \/etc\/openvpn\/easy-rsa\/keys\/client.crt<br \/>\nnano -w \/etc\/openvpn\/easy-rsa\/keys\/client.key<br \/>\n<\/code><br \/>\nWith our certificates now on our client system, we&#8217;ll create another new file called client.ovpn, where &#8220;client&#8221; should match the name of the client being deployed (from build-key), the contents should be as follows, substituting &#8220;x.x.x.x&#8221; with your cloud servers IP address, and with the appropriate files pasted into the designated areas. Include only the contents starting from the &#8220;BEGIN&#8221; header line, to the &#8220;END&#8221; line, as demonstrated below. Be sure to keep these files as confidential as you would any authentication token.<\/p>\n<p><code>client<br \/>\ndev tun<br \/>\nproto udp<br \/>\nremote x.x.x.x 1194<br \/>\nresolv-retry infinite<br \/>\nnobind<br \/>\npersist-key<br \/>\npersist-tun<br \/>\ncomp-lzo<br \/>\nverb 3<br \/>\n<ca><br \/>\nContents of ca.crt<br \/>\n<\/ca><br \/>\n<cert><br \/>\nContents of client.crt<br \/>\n<\/cert><br \/>\n<key><br \/>\nContents of client.key<br \/>\n<\/key><br \/>\n<\/code><\/p>\n<p>As all of the required information to establish a connection is now centralized in the .ovpn file, we can now deploy it on our client system.<\/p>\n<p>On Windows, regardless of edition, you will need the official OpenVPN Community Edition binaries which come prepackaged with a GUI. The only step required post-installation is to place your .ovpn configuration file into the proper directory (C:\\Program Files\\OpenVPN\\config) and click connect in the GUI. OpenVPN GUI on Windows must be executed with administrative privileges.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article will guide you through the setup and configuration of OpenVPN server on your CentOS 6 cloud server. We also cover how to configure openvpn client to connect to your newly installed OpenVPN server. Before we begin, you&#8217;ll need to have the Extra Packages for Enterprise Linux (EPEL) Repository enabled on your cloud server.\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.whplus.com\/blog\/2014\/07\/14\/setup-and-configure-an-openvpn-server-on-centos-6.html\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-734","post","type-post","status-publish","format-standard","hentry","category-tutorial"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.whplus.com\/blog\/wp-json\/wp\/v2\/posts\/734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whplus.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whplus.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whplus.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whplus.com\/blog\/wp-json\/wp\/v2\/comments?post=734"}],"version-history":[{"count":5,"href":"https:\/\/www.whplus.com\/blog\/wp-json\/wp\/v2\/posts\/734\/revisions"}],"predecessor-version":[{"id":759,"href":"https:\/\/www.whplus.com\/blog\/wp-json\/wp\/v2\/posts\/734\/revisions\/759"}],"wp:attachment":[{"href":"https:\/\/www.whplus.com\/blog\/wp-json\/wp\/v2\/media?parent=734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whplus.com\/blog\/wp-json\/wp\/v2\/categories?post=734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whplus.com\/blog\/wp-json\/wp\/v2\/tags?post=734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}