{"id":1838,"date":"2018-12-31T13:45:43","date_gmt":"2018-12-31T10:45:43","guid":{"rendered":"http:\/\/kifarunix.com\/?p=1838"},"modified":"2020-04-14T21:04:06","modified_gmt":"2020-04-14T18:04:06","slug":"install-and-setup-openvpn-server-on-fedora-29-centos-7","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-and-setup-openvpn-server-on-fedora-29-centos-7\/","title":{"rendered":"Install and Setup OpenVPN Server on Fedora 29\/CentOS 7"},"content":{"rendered":"<p>In this guide, we are going to learn how to Install and Setup OpenVPN Server on Fedora 29\/CentOS 7.<br \/>\nOpenVPN is an open-source VPN software that enables us to create an SSL-based VPN tunnel. Imagine you would like to connect to your co-oporate intranet from a remote location. Well worry not because if you have VPN server setup, this can be possible. Virtual Private Network (VPN) provides a secure tunnel that extends private network across a public network, i.e It helps create a Wide Area Network (WAN) from existing Local Area Networks (LAN). As a result, users can securely send data across public networks as if they were directly connected to their LAN.<\/p>\n<p>You can learn more about OpenVPN <a href=\"https:\/\/openvpn.net\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<h2>Install and Setup OpenVPN Server on Fedora 29\/28\/CentOS 7<\/h2>\n<p>Without much theory, let&#8217;s have a look at a step by step procedure on how Install and Setup OpenVPN Server on Fedora 29\/28\/CentOS 7.<\/p>\n<p>Update your server.<\/p>\n<pre>dnf update &lt;&lt; Fedora\r\nyum update &lt;&lt; Fedora\/CentOS<\/code><\/pre>\n<h3>Install OpenVPN and Easy-RSA on Fedora 29\/28<\/h3>\n<p>OpenVPN provides a robust and a highly flexible VPN daemon while Easy-RSA package is used to generate SSL key-pairs that is used to secure VPN connections. Both OpenVPN and Easy-RSA packages are available on the default Fedora repos. Run the command below to install them.<\/p>\n<pre>dnf install openvpn easy-rsa<\/code><\/pre>\n<h3>Install OpenVPN and Easy-RSA on CentOS 7<\/h3>\n<p>OpenVPN isn&#8217;t available in the default CentOS repositories but it is available in EPEL hence you need to install EPEL repos before you can install OpenVPN.<\/p>\n<p>To add Extra Packages for Enterprise Linux (EPEL), run either of the following commands;<\/p>\n<pre>yum install epel-release<\/code><\/pre>\n<p>or<\/p>\n<pre>wget http:\/\/dl.fedoraproject.org\/pub\/epel\/epel-release-latest-7.noarch.rpm\r\nrpm -ivh epel-release-latest-7.noarch.rpm<\/code><\/pre>\n<p>Install OpenVPN and Easy-RSA<\/p>\n<pre>yum install openvpn easy-rsa<\/code><\/pre>\n<h3>Build the Local CA and generate Server Keys and Certificate file<\/h3>\n<p>Create a directory to store Server keys and Certificate files.<\/p>\n<pre>mkdir \/etc\/openvpn\/easy-rsa<\/code><\/pre>\n<p>Copy the key\/certificate generation scripts installed by Easy-RSA from the default directory to the directory created above.<\/p>\n<pre>cp -air \/usr\/share\/easy-rsa\/3\/* \/etc\/openvpn\/easy-rsa<\/code><\/pre>\n<p>Navigate to <code>\/etc\/openvpn\/easy-rsa<\/code> directory and start new PKI.<\/p>\n<pre>cd \/etc\/openvpn\/easy-rsa\r\n.\/easyrsa init-pki<\/code><\/pre>\n<p>Build the CA certificate. This will prompt you for the encryption password and the server common name.<\/p>\n<pre>.\/easyrsa build-ca<\/code><\/pre>\n<pre>...\r\nwriting new private key to '\/etc\/openvpn\/easy-rsa\/pki\/private\/ca.key.EajtR0SkLM'\r\nEnter PEM pass phrase: <strong>PASSWORD<\/strong>\r\nVerifying - Enter PEM pass phrase: <strong>PASSWORD<\/strong>\r\n-----\r\n...\r\n-----\r\nCommon Name (eg: your user, host, or server name) [Easy-RSA CA]:<strong>server<\/strong>\r\n\r\nCA creation complete and you may now import and sign cert requests.\r\nYour new CA certificate file for publishing is at:\r\n\/etc\/openvpn\/easy-rsa\/pki\/ca.crt<\/code><\/pre>\n<p>As stated, the CA certificate is stored at <code>\/etc\/openvpn\/easy-rsa\/pki\/ca.crt<\/code>.<\/p>\n<p>Generate Diffie-Hellman key file that can be used during the TLS handshake with connecting clients.<\/p>\n<pre>.\/easyrsa gen-dh<\/code><\/pre>\n<p>This will generate the DH key and store as <code>\/etc\/openvpn\/easy-rsa\/pki\/dh.pem<\/code>.<\/p>\n<p>Generate a key and certificate file for the server.<\/p>\n<pre>.\/easyrsa build-server-full server nopass<\/code><\/pre>\n<p>Generate a key and certificate file for the client.<\/p>\n<pre>.\/easyrsa build-client-full client nopass<\/code><\/pre>\n<p>In case you need to invalidate a previously signed certificate, generate a revocation certificate.<\/p>\n<pre>.\/easyrsa gen-crl<\/code><\/pre>\n<p>This stores the revocation certificate under <code>\/etc\/openvpn\/easy-rsa\/pki\/crl.pem<\/code>.<\/p>\n<p>Generate TLS\/SSL pre-shared authentication key<\/p>\n<pre>openvpn --genkey --secret \/etc\/openvpn\/easy-rsa\/pki\/ta.key<\/code><\/pre>\n<p>Copy generated Certificates\/Keys to server configuration directory.<\/p>\n<pre>cp -rp \/etc\/openvpn\/easy-rsa\/pki\/{ca.crt,dh.pem,ta.key,issued,private} \/etc\/openvpn\/server\/<\/code><\/pre>\n<h2>Configure OpenVPN Server<\/h2>\n<p>OpenVPN has a sample configuration file within its documentation directory and therefore to ease our life, we will copy the sample <code>\/usr\/share\/doc\/openvpn{-2.4.6,}\/sample\/sample-config-files\/server.conf<\/code> file to <code>\/etc\/openvpn<\/code> for modification.<\/p>\n<p>On Fedora<\/p>\n<pre>cp \/usr\/share\/doc\/openvpn\/sample\/sample-config-files\/server.conf \/etc\/openvpn\/server\/<\/code><\/pre>\n<p>On CentOS 7<\/p>\n<pre>cp \/usr\/share\/doc\/openvpn-2.4.6\/sample\/sample-config-files\/server.conf \/etc\/openvpn\/server\/<\/code><\/pre>\n<p>Edit the <code>server.conf<\/code> file as follows;<\/p>\n<pre>vim \/etc\/openvpn\/server\/server.conf<\/code><\/pre>\n<p>Modify the file such that it looks like the below;<\/p>\n<pre># Which TCP\/UDP port should OpenVPN listen on?\r\n# Change to match your port and open it in the firewall\r\nport 1194\r\n\r\n# TCP or UDP server?\r\nproto udp\r\n\r\n# \"dev tun\" will create a routed IP tunnel\r\ndev tun\r\n\r\n# Change path for certificates\r\nca ca.crt\r\ncert issued\/server.crt\r\nkey private\/server.key\r\n\r\n# Diffie hellman exchange key path\r\ndh dh.pem\r\n\r\n# Network topology\r\ntopology subnet\r\n\r\n# OpenVPN Network IP. For below, The server will take 10.8.0.1 for itself,\r\n# the rest will be made available to clients.\r\nserver 172.16.0.0 255.255.255.0\r\n\r\n# this directive will configure all clients to redirect their default\r\n# network gateway through the VPN\r\npush \"redirect-gateway def1 bypass-dhcp\"\r\n\r\n# DNS servers\r\npush \"dhcp-option DNS 208.67.222.222\"\r\npush \"dhcp-option DNS 208.67.220.220\"\r\n\r\n# For compression compatible with older clients use comp-lzo.\r\ncomp-lzo\r\n\r\n# Run VPN with limited privileges\r\nuser nobody\r\ngroup nobody\r\n\r\n# Status log file\r\nstatus \/var\/log\/openvpn\/openvpn-status.log\r\n\r\n# TLS\/SSL pre-shared authentication key\r\ntls-auth ta.key 0\r\n\r\n# Make VPN log directory and log file\r\nlog-append \/var\/log\/openvpn\/openvpn.log\r\n\r\n#Append this line to change authentication algorithm (HMAC) from SHA1 to SHA512\r\nauth SHA512<\/code><\/pre>\n<p>Create the log directory;<\/p>\n<pre>mkdir \/var\/log\/openvpn\/<\/code><\/pre>\n<p>Save the configuration file<\/p>\n<h3>Configure Routing<\/h3>\n<h4>Enable IP forwarding<\/h4>\n<p>Enabling IP forwarding ensures that traffic from the client is routed through the servers IP address so that the client IP address is masked.<\/p>\n<pre>echo \"net.ipv4.ip_forward = 1\" &gt;&gt; \/etc\/sysctl.conf<\/code><\/pre>\n<p>Run the command below to effect the changes;<\/p>\n<pre>sysctl --system<\/code><\/pre>\n<p>Allow OpenVPN service port through firewall<\/p>\n<pre>firewall-cmd --add-port=1194\/udp --permanent<\/code><\/pre>\n<p>Activate IP Masquerading<\/p>\n<pre>firewall-cmd --add-masquerade --permanent<\/code><\/pre>\n<p>Forward traffic received on the specified OpenVPN subnet to an interface via which packets are going to be sent.<\/p>\n<p>Find the interface via which packets are sent through by running the command below;<\/p>\n<pre>ip route get 8.8.8.8\r\n8.8.8.8 via 192.168.43.1 dev <strong>enp0s8<\/strong> src 192.168.43.23<\/code><\/pre>\n<p>The interface name maybe different for your case. Replace accordingly.<\/p>\n<pre>firewall-cmd --permanent --direct --passthrough ipv4 -t nat -A POSTROUTING -s 172.16.0.0\/24 -o <strong>enp0s8<\/strong> -j MASQUERADE<\/code><\/pre>\n<p>Reload firewalld for the changes to take effect.<\/p>\n<pre>firewall-cmd --reload<\/code><\/pre>\n<p>Start and set OpenVPN start on boot.<\/p>\n<pre>systemctl start openvpn-server@server\r\nsystemctl enable openvpn-server@server<\/code><\/pre>\n<p>When OpenVPN service runs, it will create a tunnelling interface, tun0;<\/p>\n<pre>ip add show tun0\r\n4: tun0: &lt;POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 100\r\n    link\/none \r\n    inet 172.16.0.1\/24 brd 172.16.0.255 scope global tun0\r\n       valid_lft forever preferred_lft forever\r\n    inet6 fe80::1155:c60c:c009:48c9\/64 scope link stable-privacy \r\n       valid_lft forever preferred_lft forever<\/code><\/pre>\n<p>The VPN server is assigned the IP address, 172.16.0.1 while the first client will be assigned 172.16.0.2.<\/p>\n<h3>Configure VPN Client<\/h3>\n<p>In order for the VPN client to connect to VPN server, a copy of CA certificate, client key, client certificate and TLS\/SSL authentication key generated above are required. Hence, copy these files to the target client and place them on a convenient directory.<\/p>\n<pre>cd \/etc\/openvpn\/easy-rsa\/pki\/\r\nscp {ca.crt,issued\/client.crt,private\/client.key,ta.key} username@client-IP:~\/<\/code><\/pre>\n<h4>Install OpenVPN client<\/h4>\n<ul>\n<li>If you are using a Linux\/Unix server as a client, install the OpenVPN client using the respecitve package manager for example;\n<pre>apt install openvpn\r\nyum install openvpn<\/code><\/pre>\n<\/li>\n<li>If you are connecting from a windows machine, you can simply download the OpenVPN client installer from<a href=\"https:\/\/openvpn.net\/community-downloads\/\" target=\"_blank\" rel=\"noopener noreferrer\"> OpenVPN downloads page<\/a> and install it.<\/li>\n<\/ul>\n<p>Create OpenVPN configuration file for the client as shown below. As an example, i have copied the certificate and key files to my home directory on the client.<\/p>\n<pre>root@devserver:\/home\/amos# ls\r\nca.crt client.crt client.key ta.key<\/code><\/pre>\n<pre>vim client.ovpn<\/code><\/pre>\n<pre>client\r\ntls-client\r\npull\r\ndev tun\r\nproto udp\r\nremote 192.168.43.69 1194\r\nresolv-retry infinite\r\nnobind\r\ndhcp-option DNS 8.8.8.8\r\nuser nobody\r\ngroup nogroup\r\npersist-key\r\npersist-tun\r\nkey-direction 1\r\ntls-auth ta.key 1\r\ncomp-lzo\r\nverb 3\r\nca ca.crt\r\ncert client.crt\r\nkey client.key\r\nauth SHA512<\/code><\/pre>\n<p>To connect to the OpenVPN server from the client, run either of the commands below;<\/p>\n<pre>sudo openvpn client.ovpn<\/code><\/pre>\n<p>or<\/p>\n<pre>sudo openvpn --config client.ovpn<\/code><\/pre>\n<p>If the connection is successful, you should see an <code>Initialization Sequence Completed<\/code>.<\/p>\n<pre>Mon Dec 31 03:54:39 2018 TUN\/TAP device tun0 opened\r\nMon Dec 31 03:54:39 2018 TUN\/TAP TX queue length set to 100\r\nMon Dec 31 03:54:39 2018 do_ifconfig, tt-&gt;did_ifconfig_ipv6_setup=0\r\nMon Dec 31 03:54:39 2018 \/sbin\/ip link set dev tun0 up mtu 1500\r\nMon Dec 31 03:54:39 2018 \/sbin\/ip addr add dev tun0 172.16.0.2\/24 broadcast 172.16.0.255\r\nMon Dec 31 03:54:39 2018 \/sbin\/ip route add 192.168.43.69\/32 dev enp0s8\r\nMon Dec 31 03:54:39 2018 \/sbin\/ip route add 0.0.0.0\/1 via 172.16.0.1\r\nMon Dec 31 03:54:39 2018 \/sbin\/ip route add 128.0.0.0\/1 via 172.16.0.1\r\nMon Dec 31 03:54:39 2018 GID set to nogroup\r\nMon Dec 31 03:54:39 2018 UID set to nobody\r\nMon Dec 31 03:54:39 2018 Initialization Sequence Completed<\/code><\/pre>\n<p>If you can check the interface, you should see a tunnelling interface created.<\/p>\n<pre>ip add sh tun0\r\n20: tun0: &lt;POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 100\r\n    link\/none \r\n    inet <strong>172.16.0.2<\/strong>\/24 brd 172.16.0.255 scope global tun0\r\n       valid_lft forever preferred_lft forever\r\n    inet6 fe80::dc37:c115:60f:6b86\/64 scope link flags 800 \r\n       valid_lft forever preferred_lft forever<\/code><\/pre>\n<p>You have successfully connected to your VPN server.<\/p>\n<p>That is all about how to install and setup OpenVPN server on Fedora 29\/CentOS 7. You OpenVPN server is fully operational. Enjoy.<\/p>\n<h3>Related Tutorials<\/h3>\n<p class=\"entry-title td-module-title\"><a title=\"Configure IPSEC VPN using StrongSwan on Ubuntu 18.04\" href=\"https:\/\/kifarunix.com\/how-to-configure-ipsec-vpn-using-strongswan-on-ubuntu-18-04\/\" target=\"_blank\" rel=\"bookmark noopener noreferrer\">Configure IPSEC VPN using StrongSwan on Ubuntu 18.04<\/a><\/p>\n<p class=\"entry-title td-module-title\"><a title=\"Configure strongSwan VPN Client on Ubuntu 18.04\/CentOS 8\" href=\"https:\/\/kifarunix.com\/configure-strongswan-vpn-client-on-ubuntu-18-04-centos-8\/\" target=\"_blank\" rel=\"bookmark noopener noreferrer\">Configure strongSwan VPN Client on Ubuntu 18.04\/CentOS 8<\/a><\/p>\n<p class=\"entry-title td-module-title\"><a title=\"Setup IPSEC VPN using StrongSwan on Debian 10\" href=\"https:\/\/kifarunix.com\/setup-ipsec-vpn-using-strongswan-on-debian-10\/\" target=\"_blank\" rel=\"bookmark noopener noreferrer\">Setup IPSEC VPN using StrongSwan on Debian 10<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we are going to learn how to Install and Setup OpenVPN Server on Fedora 29\/CentOS 7. OpenVPN is an open-source VPN software<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,63,282],"tags":[284,242,283],"class_list":["post-1838","post","type-post","status-publish","format-standard","hentry","category-howtos","category-networking","category-openvpn","tag-centos","tag-fedora","tag-openvpn","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1838"}],"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=1838"}],"version-history":[{"count":4,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1838\/revisions"}],"predecessor-version":[{"id":5444,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1838\/revisions\/5444"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=1838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=1838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=1838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}