{"id":60,"date":"2020-01-23T01:00:32","date_gmt":"2020-01-23T01:00:32","guid":{"rendered":"https:\/\/serverhub.com\/kb\/?p=60"},"modified":"2020-01-23T01:00:34","modified_gmt":"2020-01-23T01:00:34","slug":"how-to-set-up-and-configure-an-openvpn-server-on-centos","status":"publish","type":"post","link":"https:\/\/serverhub.com\/kb\/how-to-set-up-and-configure-an-openvpn-server-on-centos\/","title":{"rendered":"How To Set Up and Configure an OpenVPN Server on CentOS"},"content":{"rendered":"\n<p>A VPN allows you to connect to remote VPN servers, making your connection encrypted and secure and surf the web anonymously by keeping your traffic data private.<\/p>\n\n\n\n<p>There are many commercial VPN providers you can choose from, but you can never be truly sure that the provider is not logging your activity. The safest option is to set up your own VPN server.<\/p>\n\n\n\n<p>In this tutorial we explain how to install OpenVPN on your ServerHub Bare Metal Server or VPS. <br><br>OpenVPN is an open source VPN application that lets you create and join a private network securely over the internet <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\">Prerequisites<\/h2>\n\n\n\n<p>To complete this tutorial, you will need:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Sudo Access to an CentOS server to host your OpenVPN instance.<\/li><li>The server should have a firewall configured.<\/li><li>Separate dedicated machine to serve as your CA (certificate authority)<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Let&#8217;s Begin <br><br><\/h2>\n\n\n\n<p><strong>Configuring easy-rsa<\/strong><\/p>\n\n\n\n<p>To configure this CLI utility, you\u2019ll need to generate several keys and certificates including:<\/p>\n\n\n\n<p>1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Certificate Authority (CA)<\/p>\n\n\n\n<p>2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Server Key and Certificate<\/p>\n\n\n\n<p>3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Diffie%E2%80%93Hellman_key_exchange\">Diffie-Hellman key<\/a><\/p>\n\n\n\n<p>4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Client Key and Certificate<\/p>\n\n\n\n<p>Here is what you need to do:<\/p>\n\n\n\n<p><strong>Step 1: Copy the easy-rsa script generation to \u201c\/etc\/OpenVPN\/\u201d.<\/strong><\/p>\n\n\n\n<p>cp -r \/usr\/share\/easy-rsa\/ \/etc\/openvpn\/<\/p>\n\n\n\n<p>Then click on the easy-rsa directory and make changes to the vars file.<\/p>\n\n\n\n<p>cd \/etc\/openvpn\/easy-rsa\/2.*\/<\/p>\n\n\n\n<p>vim vars<\/p>\n\n\n\n<p>After this, we can generate new keys and certificates to help us with installation.<\/p>\n\n\n\n<p>source .\/vars<\/p>\n\n\n\n<p>Run clean-all to make sure that you are left with a clean certificate setup.<\/p>\n\n\n\n<p>.\/clean-all<\/p>\n\n\n\n<p>Now it\u2019s time to generate a certificate authority (ca). Here you\u2019ll be asked several details such as Country Name, etc., enter your details.<\/p>\n\n\n\n<p>This command will create a ca.key and ca.crt in the&nbsp;\/etc\/OpenVPN\/easy-rsa\/2.0\/keys\/&nbsp;directory.<\/p>\n\n\n\n<p>.\/build-ca<\/p>\n\n\n\n<p><strong>Step 2: Generating a Server Key and Certificate<\/strong><\/p>\n\n\n\n<p>You need to run the command \u201cbuild-key-server server\u201d in the existing directory.<\/p>\n\n\n\n<p>.\/build-key-server server<\/p>\n\n\n\n<p><strong>Step 3: Building a Diffie-Hellman Key Exchange<\/strong><\/p>\n\n\n\n<p>Execute this build-dh command:<\/p>\n\n\n\n<p>.\/build-dh<\/p>\n\n\n\n<p>It might take some time to generate these files. The waiting time depends on the KEY_SIZE you have set on the file vars.<\/p>\n\n\n\n<p><strong>Step 4: Generating Client Key and Certificate<\/strong><\/p>\n\n\n\n<p>.\/build-key client<\/p>\n\n\n\n<p><strong>Step 5:&nbsp;Move or copy the `keys\/` directory to `\/etc\/opennvpn`.<\/strong><\/p>\n\n\n\n<p>cd \/etc\/openvpn\/easy-rsa\/2.0\/<\/p>\n\n\n\n<p>cp -r keys\/ \/etc\/openvpn\/<\/p>\n\n\n\n<p><strong>Configure OpenVPN<\/strong><\/p>\n\n\n\n<p>You can either copy an OpenVPN configuration or create one from scratch. You can copy it from \/usr\/share\/doc\/openvpn-2.3.6\/sample\/sample-config-files.<\/p>\n\n\n\n<p>Here is how you can create one:<\/p>\n\n\n\n<p>cd \/etc\/openvpn\/<\/p>\n\n\n\n<p>vim server.conf<\/p>\n\n\n\n<p>Paste this configurations<\/p>\n\n\n\n<p>#change with your port<br><br>port 1337<br><br><br><br>#You can use udp or tcp<br><br>proto udp<br><br><br><br># \u201cdev tun\u201d will create a routed IP tunnel.<br><br>dev tun<br><br><br><br>#Certificate Configuration<br><br><br><br>#ca certificate<br><br>ca \/etc\/openvpn\/keys\/ca.crt<br><br><br><br>#Server Certificate<br><br>cert \/etc\/openvpn\/keys\/server.crt<br><br><br><br>#Server Key and keep this is secret<br><br>key \/etc\/openvpn\/keys\/server.key<br><br><br><br>#See the size a dh key in \/etc\/openvpn\/keys\/<br><br>dh \/etc\/openvpn\/keys\/dh1024.pem<br><br><br><br>#Internal IP will get when already connect<br><br>server 192.168.200.0 255.255.255.0<br><br><br><br>#this line will redirect all traffic through our OpenVPN<br><br>push \u201credirect-gateway def1\u201d<br><br><br><br>#Provide DNS servers to the client, you can use goolge DNS<br><br>push \u201cdhcp-option DNS 8.8.8.8\u201d<br><br>push \u201cdhcp-option DNS 8.8.4.4\u201d<br><br><br><br>#Enable multiple client to connect with same key<br><br>duplicate-cn<br><br><br><br>keepalive 20 60<br><br>comp-lzo<br><br>persist-key<br><br>persist-tun<br><br>daemon<br><br><br><br>#enable log<br><br>log-append \/var\/log\/myvpn\/openvpn.log<br><br><br><br>#Log Level<br><br>verb 3<\/p>\n\n\n\n<p>Save it.<\/p>\n\n\n\n<p>Now you need to create a new folder for the log file.<\/p>\n\n\n\n<p>mkdir -p \/var\/log\/myvpn\/<\/p>\n\n\n\n<p>touch \/var\/log\/myvpn\/openvpn.log<\/p>\n\n\n\n<p><strong>How to Disable Selinux and Firewalld<\/strong><\/p>\n\n\n\n<p>Step 1: disabling firewalld<\/p>\n\n\n\n<p>systemctl mask firewalld<\/p>\n\n\n\n<p>systemctl stop firewalld<\/p>\n\n\n\n<p>Step 2: Disabling SELinux<\/p>\n\n\n\n<p>vim \/etc\/sysconfig\/selinux<\/p>\n\n\n\n<p>Ensure you make SELINUX as disabled.<\/p>\n\n\n\n<p>SELINUX=disabled<\/p>\n\n\n\n<p>Now reboot your server to incorporate the changes.<\/p>\n\n\n\n<p><strong>Configure Routing and Iptables<\/strong><\/p>\n\n\n\n<p>Step 1: you need to enable iptables<\/p>\n\n\n\n<p>systemctl enable iptables<\/p>\n\n\n\n<p>systemctl start iptables<\/p>\n\n\n\n<p>iptables \u2013F<\/p>\n\n\n\n<p>Step 2: Add iptable-rule so as to forward the routing to our OpenVPN subnet.<\/p>\n\n\n\n<p>iptables -t nat -A POSTROUTING -s 192.168.200.024 -o eth0 -j MASQUERADE<\/p>\n\n\n\n<p>iptables-save &gt; \/etc\/sysconfig\/iptablesvpn<\/p>\n\n\n\n<p>Step 3: Now enable port forwarding<\/p>\n\n\n\n<p>vim \/etc\/sysctl.conf<\/p>\n\n\n\n<p>Then add this to the end of the line:<\/p>\n\n\n\n<p>net.ipv4.ip_forward = 1.<\/p>\n\n\n\n<p>Step 4: Restart your network server<\/p>\n\n\n\n<p>systemctl start openvpn@server<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A VPN allows you to connect to remote VPN servers, making your connection encrypted and secure and surf the web anonymously by keeping your traffic data private. There are many commercial VPN providers you can choose from, but you can never be truly sure that the provider is not logging your activity. The safest option &#8230; <a title=\"How To Set Up and Configure an OpenVPN Server on CentOS\" class=\"read-more\" href=\"https:\/\/serverhub.com\/kb\/how-to-set-up-and-configure-an-openvpn-server-on-centos\/\" aria-label=\"More on How To Set Up and Configure an OpenVPN Server on CentOS\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[7,41],"tags":[44,8,43,42],"class_list":["post-60","post","type-post","status-publish","format-standard","hentry","category-linux-administration","category-vpn-tunnels","tag-how-to","tag-linux","tag-openvpn","tag-vpn"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/serverhub.com\/kb\/wp-json\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/serverhub.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/serverhub.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/serverhub.com\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/serverhub.com\/kb\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":0,"href":"https:\/\/serverhub.com\/kb\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"wp:attachment":[{"href":"https:\/\/serverhub.com\/kb\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/serverhub.com\/kb\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/serverhub.com\/kb\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}