{"id":1681,"date":"2018-10-13T21:06:57","date_gmt":"2018-10-13T18:06:57","guid":{"rendered":"https:\/\/artem.services\/?p=85"},"modified":"2020-03-15T19:56:15","modified_gmt":"2020-03-15T16:56:15","slug":"1681","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=1681&lang=en","title":{"rendered":"OpenVPN &#8212; All traffic through VPN"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"size-full wp-image-105 aligncenter\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/OpenVPN.png\" alt=\"\" width=\"884\" height=\"258\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/OpenVPN.png 884w, https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/OpenVPN-300x88.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/OpenVPN-768x224.png 768w\" sizes=\"(max-width: 884px) 100vw, 884px\" \/><\/p>\n<h3>Goal:<\/h3>\n<p>Allow traffic from any device via <strong>VPN<\/strong>. The maximum convenience is connecting new devices without creating accounts, creating passwords, etc. Fast and encrypted connection.<\/p>\n<p>All steps were performed on <strong>CentOS 7<\/strong>.<\/p>\n<p>Install the <strong>EPEL<\/strong> repository if it is not already in the system and install the necessary packages:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nyum install epel-release -y\r\nyum install openvpn easy-rsa -y\r\n<\/pre>\n<p>Create a configuration file:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nvim \/etc\/openvpn\/server.conf\r\n<\/pre>\n<p><!--more--><\/p>\n<p>And copy the following into it:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nlocal CHANGE_THIS_ON_YOUR_PUBLIC_IP\r\nport 1194\r\n\r\nproto tcp\r\ndev-type tun\r\ndev tun\r\n\r\nca ca.crt\r\ncert server.crt\r\nkey server.key\r\n\r\ndh dh2048.pem\r\n\r\ntopology subnet\r\nserver 10.8.0.1255.255.255.0\r\n\r\ntxqueuelen 250\r\nkeepalive 300 900\r\n\r\ncipher AES-128-CBC\r\nncp-ciphers AES-128-GCM\r\n\r\nuser nobody\r\ngroup nobody\r\n\r\nduplicate-cn\r\n\r\npersist-key\r\npersist-tun\r\n\r\nstatus openvpn-status.log\r\n\r\npush &quot;redirect-gateway def1&quot;\r\npush &quot;remote-gateway 10.8.0.1&quot;\r\npush &quot;dhcp-option DNS 8.8.8.8&quot;\r\n<\/pre>\n<p>Create a folder for keys and copy the necessary scripts to create them:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmkdir -p \/etc\/openvpn\/easy-rsa\/keys\r\ncp -a \/usr\/share\/easy-rsa\/2.0\/* \/etc\/openvpn\/easy-rsa\r\n<\/pre>\n<p>For convenience, we can immediately specify the information necessary for creating keys in environment variables so that we do not constantly enter it in the future:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nvim \/etc\/openvpn\/easy-rsa\/vars\r\n<\/pre>\n<p>And we bring it to this form:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nexport KEY_COUNTRY=&quot;UA&quot;\r\nexport KEY_PROVINCE=&quot;UA&quot;\r\nexport KEY_CITY=&quot;Kiev&quot;\r\nexport KEY_ORG=&quot;openvpn&quot;\r\nexport KEY_EMAIL=&quot;admin@artem.services&quot;\r\nexport KEY_OU=&quot;VPN&quot;\r\nexport KEY_NAME=&quot;openvpn&quot;\r\nexport KEY_CN=&quot;openvpn.artem.services&quot;\r\n<\/pre>\n<p>Copy the <strong>OpenSSL<\/strong> configuration:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncp \/etc\/openvpn\/easy-rsa\/openssl-1.0.0.cnf \/etc\/openvpn\/easy-rsa\/openssl.cnf\r\n<\/pre>\n<p>Go to the script folder for creating keys, and clear its contents for our future keys:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncd \/etc\/openvpn\/easy-rsa\r\nsource .\/vars\r\n.\/clean-all\r\n<\/pre>\n<p>Create a root certificate:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n.\/build-ca\r\n<\/pre>\n<p>Create a key and a public certificate:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n.\/build-key-server server\r\n<\/pre>\n<p>Create a <strong>Diffie-Hellman<\/strong> key:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n.\/build-dh\r\n<\/pre>\n<p>Let&#8217;s go to the directory with the keys and certificates that we created:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncd \/etc\/openvpn\/easy-rsa\/keys\r\n<\/pre>\n<p>And copy the files we need to the <strong>OpenVPN<\/strong> directory:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncp -a dh2048.pem ca.crt server.crt server.key \/etc\/openvpn\r\n<\/pre>\n<p>&nbsp;<\/p>\n<blockquote><p>It is important that after copying these files retain the permission<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>Create a certificate and a key for the client:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncd \/etc\/openvpn\/easy-rsa\r\n.\/build-key client\r\n<\/pre>\n<p>Further the configuration is given for <strong>iptables<\/strong>, if <strong>firewalld<\/strong> is used then you can disable it as follows:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nyum install iptables-services -y\r\nsystemctl mask firewalld\r\nsystemctl enable iptables\r\nsystemctl stop firewalld\r\nsystemctl start iptables\r\niptables --flush\r\n<\/pre>\n<p>Add the rule to <strong>iptables<\/strong> and save:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\niptables -t nat -A POSTROUTING -s 10.8.0.0\/24 -o eth0 -j MASQUERADE\r\niptables-save &gt; \/etc\/sysconfig\/iptables\r\n<\/pre>\n<p>&nbsp;<\/p>\n<blockquote><p>Be sure to check the interface name is correct, in my case it is <strong>eth0<\/strong><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>In the file &quot;<strong>\/etc\/sysctl.conf<\/strong>&quot; we enable packet forwarding:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nnet.ipv4.ip_forward = 1\r\n<\/pre>\n<p>And restart the network service:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsystemctl restart network.service\r\n<\/pre>\n<p>Add the <strong>OpenVPN<\/strong> service to autorun and start it:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsystemctl -f enable openvpn@server.service\r\nsystemctl start openvpn@server.service\r\n<\/pre>\n<p>Create a client configuration file for connecting to the server, immediately inserting the necessary keys and certificates:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nvim openvpn.ovpn\r\n<\/pre>\n<p>And copy the following into it:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nclient\r\nremote artem.services 1194\r\n\r\nnobind\r\n\r\nremote-cert-tls server\r\n\r\ncipher AES-128-CBC\r\n\r\nsetenv opt ncp-ciphers AES-128-GCM\r\n\r\nsetenv opt block-outside-dns\r\n\r\ndev tun\r\n\r\nproto udp\r\n\r\n&lt;ca&gt;\r\nFILE CONTENTS ca.crt\r\n&lt;ca\/&gt;\r\n\r\n&lt;cert&gt;\r\nFILE CONTENTS client.crt\r\n&lt;cert\/&gt;\r\n\r\n&lt;key&gt;\r\nFILE CONTENTS client.key\r\n&lt;key\/&gt;\r\n<\/pre>\n<p>Then this file can be imported to client devices and connected to the server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Goal: Allow traffic from any device via VPN. The maximum convenience is connecting new devices without creating accounts, creating passwords, etc. Fast and encrypted connection. All steps were performed on CentOS 7. Install the EPEL repository if it is not already in the system and install the necessary packages: Create a configuration file:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1335],"tags":[855,1337],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1681"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1681"}],"version-history":[{"count":3,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1681\/revisions"}],"predecessor-version":[{"id":1686,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1681\/revisions\/1686"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}