{"id":171,"date":"2019-12-10T21:05:22","date_gmt":"2019-12-10T13:05:22","guid":{"rendered":"http:\/\/www.betterit360.com\/?p=171"},"modified":"2019-12-10T21:05:22","modified_gmt":"2019-12-10T13:05:22","slug":"set-up-and-configure-an-openvpn-server-on-centos-7","status":"publish","type":"post","link":"http:\/\/www.betterit360.com\/?p=171","title":{"rendered":"Set Up and Configure an OpenVPN Server on CentOS 7"},"content":{"rendered":"\n<p><strong>Installing OpenVPN<\/strong><strong><\/strong><\/p>\n\n\n\n<p># yum install epel-release<\/p>\n\n\n\n<p># yum install openvpn<\/p>\n\n\n\n<p><strong>Building CA with EasyRSA<\/strong><strong><\/strong><\/p>\n\n\n\n<p># cd \/usr\/local\/src<\/p>\n\n\n\n<p># wget https:\/\/github.com\/OpenVPN\/easy-rsa\/releases\/download\/v3.0.5\/EasyRSA-nix-3.0.5.tgz<\/p>\n\n\n\n<p># tar xzf EasyRSA-nix-3.0.5.tgz -C \/usr\/local<\/p>\n\n\n\n<p># cd \/usr\/local\/EasyRSA-3.0.5\/<\/p>\n\n\n\n<p># cp vars.example vars<\/p>\n\n\n\n<p>uncomment and update the following entries to match your information:<\/p>\n\n\n\n<p># vi vars<\/p>\n\n\n\n<p>set_var EASYRSA_REQ_COUNTRY &nbsp;&nbsp;&nbsp;&#8220;CN&#8221;<\/p>\n\n\n\n<p>set_var EASYRSA_REQ_PROVINCE &nbsp;&nbsp;&#8220;Jiangsu&#8221;<\/p>\n\n\n\n<p>set_var EASYRSA_REQ_CITY &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;Suzhou&#8221;<\/p>\n\n\n\n<p>set_var EASYRSA_REQ_ORG &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;Betterit&#8221;<\/p>\n\n\n\n<p>set_var EASYRSA_REQ_EMAIL &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;admin@betterit.info&#8221;<\/p>\n\n\n\n<p>set_var EASYRSA_REQ_OU &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;IT&#8221;<\/p>\n\n\n\n<p>Save and close the file.<\/p>\n\n\n\n<p>Before generating a CA keypair first we need to initialize a new PKI with:<\/p>\n\n\n\n<p># .\/easyrsa init-pki<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"488\" height=\"155\" src=\"http:\/\/www.betterit360.com\/wp-content\/uploads\/2019\/12\/openvpn_\u56fe\u72471.png\" alt=\"\" class=\"wp-image-172\"\/><\/figure>\n\n\n\n<p>The next step is to build the CA:<\/p>\n\n\n\n<p># .\/easyrsa build-ca<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"543\" height=\"381\" src=\"http:\/\/www.betterit360.com\/wp-content\/uploads\/2019\/12\/openvpn_\u56fe\u72472.png\" alt=\"\" class=\"wp-image-173\"\/><\/figure>\n\n\n\n<p>You\u2019ll be asked to set a password for the CA key and enter a common name for your CA.<\/p>\n\n\n\n<p>Once completed, the script will create two files \u2014 CA public certificate ca.crt and CA private key ca.key.<\/p>\n\n\n\n<p>Now that the Certificate Authority (CA) is created, you can use it to sign certificate requests for one or multiple OpenVPN servers and clients.<\/p>\n\n\n\n<p>Note:If you don\u2019t want to be prompted for a password each time you sign your certificates, run the build-ca command using the nopass option: .\/easyrsa build-ca nopass.<\/p>\n\n\n\n<p><strong>Creating Diffie-Hellman and HMAC keys<\/strong><strong><\/strong><\/p>\n\n\n\n<p>[root@iZbp16cdvzk4ribfwjca03Z EasyRSA-3.0.5]# .\/easyrsa gen-dh<\/p>\n\n\n\n<p>Note: using Easy-RSA configuration from: .\/vars<\/p>\n\n\n\n<p>Using SSL: openssl OpenSSL 1.0.2k-fips &nbsp;26 Jan 2017<\/p>\n\n\n\n<p>Generating DH parameters, 2048 bit long safe prime, generator 2<\/p>\n\n\n\n<p>This is going to take a long time<\/p>\n\n\n\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..+&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..<\/p>\n\n\n\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.+&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.+&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.++*++*<\/p>\n\n\n\n<p>DH parameters of size 2048 created at \/usr\/local\/EasyRSA-3.0.5\/pki\/dh.pem<\/p>\n\n\n\n<p>[root@iZbp16cdvzk4ribfwjca03Z EasyRSA-3.0.5]# <\/p>\n\n\n\n<p>Copy the dh.pem file to the \/etc\/openvpn directory:<\/p>\n\n\n\n<p># cp pki\/dh.pem \/etc\/openvpn\/<\/p>\n\n\n\n<p>generate a HMAC signature using the openvpn binary:<\/p>\n\n\n\n<p># openvpn &#8211;genkey &#8211;secret ta.key<\/p>\n\n\n\n<p>Once completed copy the ta.key file to the \/etc\/openvpn directory:<\/p>\n\n\n\n<p># cp ta.key \/etc\/openvpn\/<\/p>\n\n\n\n<p><strong>Creating Server Certificate and Private Key<\/strong><strong><\/strong><\/p>\n\n\n\n<p>.\/easyrsa gen-req vpnsvr01&nbsp;nopass<\/p>\n\n\n\n<p>We are using the nopass argument because we want to start the OpenVPN server without a password input. Also in this example, we are using vpnsvr01&nbsp;as a server name (entity) identifier. If you choose a different name for your server don\u2019t forget to adjust the instructions below where the server name is used.<\/p>\n\n\n\n<p>The command will create two files, a private key (vpnsvr01.key) and a certificate request file (vpnsvr01.req).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"629\" height=\"365\" src=\"http:\/\/www.betterit360.com\/wp-content\/uploads\/2019\/12\/openvpn_\u56fe\u72473.png\" alt=\"\" class=\"wp-image-174\"\/><\/figure>\n\n\n\n<p>Copy the private key to the \/etc\/openvpn directory:<\/p>\n\n\n\n<p># cp pki\/private\/vpnsvr01.key \/etc\/openvpn\/<\/p>\n\n\n\n<p>run the following command to sign the request:<\/p>\n\n\n\n<p>.\/easyrsa sign-req server vpnsvr01<\/p>\n\n\n\n<p>The first argument can either be server or client and the second one is the server short (entity) name.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"577\" height=\"497\" src=\"http:\/\/www.betterit360.com\/wp-content\/uploads\/2019\/12\/openvpn_\u56fe\u72474.png\" alt=\"\" class=\"wp-image-175\"\/><\/figure>\n\n\n\n<p># cp pki\/ca.crt \/etc\/openvpn\/<\/p>\n\n\n\n<p># cp pki\/issued\/vpnsvr01.crt \/etc\/openvpn\/<\/p>\n\n\n\n<p>Upon completing the steps outlined in this section, you should have the following new files on your OpenVPN server:<\/p>\n\n\n\n<p>\/etc\/openvpn\/ca.crt<\/p>\n\n\n\n<p>\/etc\/openvpn\/dh.pem<\/p>\n\n\n\n<p>\/etc\/openvpn\/ta.key<\/p>\n\n\n\n<p>\/etc\/openvpn\/vpnsvr01.crt<\/p>\n\n\n\n<p>\/etc\/openvpn\/vpnsvr01.key<\/p>\n\n\n\n<p><strong>Configuring the OpenVPN Service<\/strong><strong><\/strong><\/p>\n\n\n\n<p>We will use the sample configuration file provided with OpenVPN installation package as a starting point and then add our own custom configuration options to it.<\/p>\n\n\n\n<p># cp \/usr\/share\/doc\/openvpn-*\/sample\/sample-config-files\/server.conf \/etc\/openvpn\/vpnsvr01.conf<\/p>\n\n\n\n<p>Open the file and Find the Certificate, Key and DH parameters directives and change the file names:<\/p>\n\n\n\n<p># vi \/etc\/openvpn\/vpnsvr01.conf<\/p>\n\n\n\n<p>cert vpnsvr01.crt<\/p>\n\n\n\n<p>key vpnsvr01.key <\/p>\n\n\n\n<p>dh dh.pem<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"393\" height=\"178\" src=\"http:\/\/www.betterit360.com\/wp-content\/uploads\/2019\/12\/openvpn_\u56fe\u72475.png\" alt=\"\" class=\"wp-image-176\"\/><\/figure>\n\n\n\n<p>To redirect the clients traffic through the VPN find and uncomment the redirect-gateway and dhcp-option options:<\/p>\n\n\n\n<p>push &#8220;redirect-gateway def1 bypass-dhcp&#8221;<\/p>\n\n\n\n<p>push &#8220;dhcp-option DNS 208.67.222.222&#8221;<\/p>\n\n\n\n<p>push &#8220;dhcp-option DNS 208.67.220.220&#8221;<\/p>\n\n\n\n<p>By default OpenDNS resolvers are used. You can change it and use Google or any other DNS resolvers you want.<\/p>\n\n\n\n<p>Find the user and group directives and uncomment these settings by removing the \u201c;\u201d at the beginning of each line:<\/p>\n\n\n\n<p>user nobody<\/p>\n\n\n\n<p>group nogroup<\/p>\n\n\n\n<p>Append the following line at the end of the file. This directive will change the message authentication algorithm (HMAC) from SHA1 to SHA256<\/p>\n\n\n\n<p>auth SHA256<\/p>\n\n\n\n<p>Once you are done, the server configuration file (excluding comments) should look something like this:<\/p>\n\n\n\n<p>port 1194<\/p>\n\n\n\n<p>proto udp<\/p>\n\n\n\n<p>dev tun<\/p>\n\n\n\n<p>ca ca.crt<\/p>\n\n\n\n<p>cert vpnsvr01.crt<\/p>\n\n\n\n<p>key vpnsvr01.key &nbsp;# This file should be kept secret<\/p>\n\n\n\n<p>dh dh.pem<\/p>\n\n\n\n<p>server 10.8.0.0 255.255.255.0<\/p>\n\n\n\n<p>ifconfig-pool-persist ipp.txt<\/p>\n\n\n\n<p>push &#8220;dhcp-option DNS 208.67.222.222&#8221;<\/p>\n\n\n\n<p>push &#8220;dhcp-option DNS 208.67.220.220&#8221;<\/p>\n\n\n\n<p>keepalive 10 120<\/p>\n\n\n\n<p>tls-auth ta.key 0 # This file is secret<\/p>\n\n\n\n<p>cipher AES-256-CBC<\/p>\n\n\n\n<p>user nobody<\/p>\n\n\n\n<p>group nobody<\/p>\n\n\n\n<p>persist-key<\/p>\n\n\n\n<p>persist-tun<\/p>\n\n\n\n<p>status openvpn-status.log<\/p>\n\n\n\n<p>verb 3<\/p>\n\n\n\n<p>explicit-exit-notify 1<\/p>\n\n\n\n<p>auth SHA256<\/p>\n\n\n\n<p><strong>Starting OpenVPN Service<\/strong><strong><\/strong><\/p>\n\n\n\n<p># systemctl start openvpn@vpnsvr01<\/p>\n\n\n\n<p>Verify whether the service has started successfully by typing:<\/p>\n\n\n\n<p># systemctl status openvpn@vpnsvr01<\/p>\n\n\n\n<p>Enable the service to automatically start on boot with:<\/p>\n\n\n\n<p># systemctl enable openvpn@vpnsvr01<\/p>\n\n\n\n<p>When starting, the OpenVPN Server creates a tun device tun0. To check if the device is available, type:<\/p>\n\n\n\n<p># ip a show tun0<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"658\" height=\"108\" src=\"http:\/\/www.betterit360.com\/wp-content\/uploads\/2019\/12\/openvpn_\u56fe\u72476.png\" alt=\"\" class=\"wp-image-177\"\/><\/figure>\n\n\n\n<p><strong>Firewall and Server Networking Configuration<\/strong><strong><\/strong><\/p>\n\n\n\n<p>In order to forward network packets properly, we need to enable IP forwarding.<\/p>\n\n\n\n<p>Open the \/etc\/sysctl.conf file and add the following line:<\/p>\n\n\n\n<p>net.ipv4.ip_forward = 1<\/p>\n\n\n\n<p>Apply the new settings by running the following command:<\/p>\n\n\n\n<p>sysctl -p<\/p>\n\n\n\n<p>Now we need to add firewall rules open OpenVPN port and to enable masquerading.<\/p>\n\n\n\n<p>Start by adding the tun0 interface to the trusted zone:<\/p>\n\n\n\n<p>firewall-cmd &#8211;permanent &#8211;zone=trusted &#8211;add-interface=tun0<\/p>\n\n\n\n<p>Open the default openvpn port 1194 by adding the openvpn service to the list of services allowed by firewalld :<\/p>\n\n\n\n<p>firewall-cmd &#8211;permanent &#8211;add-service openvpn<\/p>\n\n\n\n<p>Set IP masquerading on trusted zone:<\/p>\n\n\n\n<p>firewall-cmd &#8211;permanent &#8211;zone=trusted &#8211;add-masquerade<\/p>\n\n\n\n<p>Before adding the nat rule you need to know the public network interface of your CentOS OpenVPN Server. You can easily find the interface by running the following command:<\/p>\n\n\n\n<p>ip -o -4 route show to default | awk &#8216;{print $5}&#8217;<\/p>\n\n\n\n<p>In our case, the interface is named eth0 as shown on the output below. Your interface may have a different name.<\/p>\n\n\n\n<p>The following command will allow the traffic to leave the VPN, giving your VPN clients access to the Internet. Don\u2019t forget to replace eth0 to match the name of public network interface you found in the previous command.<\/p>\n\n\n\n<p>firewall-cmd &#8211;permanent &#8211;direct &#8211;passthrough ipv4 -t nat -A POSTROUTING -s &nbsp;10.8.0.0\/24 -o eth0 -j MASQUERADE<\/p>\n\n\n\n<p>Finally reload the firewall rules for changes to take effect:<\/p>\n\n\n\n<p>firewall-cmd &#8211;reload<\/p>\n\n\n\n<p><strong>Creating the Client Configuration Infrastructure<\/strong><strong><\/strong><\/p>\n\n\n\n<p>The whole process of generating the client certificate and configuration file is as follows:<\/p>\n\n\n\n<ol><li>Generate a private key and certificate request on the OpenVPN server.<\/li><li>Send the request to the CA machine to be signed.<\/li><li>Copy the signed SSL certificate to the OpenVPN server and generate a configuration file.<\/li><li>Send the configuration file to the VPN client\u2019s machine.<\/li><\/ol>\n\n\n\n<p>Start by creating a set of directories to store the clients files:<\/p>\n\n\n\n<p># cd \/etc\/openvpn<\/p>\n\n\n\n<p># mkdir -p openvpn-clients\/{configs,base,files}<\/p>\n\n\n\n<ul><li>base directory will store the base files and configuration that will be shared across all client files.<\/li><li>configs directory will store the generated client configuration.<\/li><li>files directory will store client-specific certificate\/key pair.<\/li><\/ul>\n\n\n\n<p>Copy the ca.crt and ta.key files to the openvpn-clients\/base directory:<\/p>\n\n\n\n<p># cp ta.key openvpn-clients\/base<\/p>\n\n\n\n<p># cp ca.crt openvpn-clients\/base<\/p>\n\n\n\n<p>Next copy the sample VPN client configuration file into the client-~\/openvpn-clients\/base directory. We will use this file as a base configuration:<\/p>\n\n\n\n<p># cp \/usr\/share\/doc\/openvpn-*\/sample\/sample-config-files\/client.conf openvpn-clients\/base<\/p>\n\n\n\n<p>Now we need to edit the file to match our server settings and configuration.<\/p>\n\n\n\n<p># vi openvpn-clients\/base\/client.conf<\/p>\n\n\n\n<p>Find the remote directive and change the default placeholder with the public IP address of your OpenVPN server:<\/p>\n\n\n\n<p># The hostname\/IP and port of the server.<\/p>\n\n\n\n<p># You can have multiple remote entries<\/p>\n\n\n\n<p># to load balance between the servers.<\/p>\n\n\n\n<p>remote YOUR_SERVER_IP 1194<\/p>\n\n\n\n<p>Locate and comment the ca, cert, and key directives. The certs and keys will be added within the configuration file:<\/p>\n\n\n\n<p># SSL\/TLS parms.<\/p>\n\n\n\n<p># See the server config file for more<\/p>\n\n\n\n<p># description. &nbsp;It&#8217;s best to use<\/p>\n\n\n\n<p># a separate .crt\/.key file pair<\/p>\n\n\n\n<p># for each client. &nbsp;A single ca<\/p>\n\n\n\n<p># file can be used for all clients.<\/p>\n\n\n\n<p># ca ca.crt<\/p>\n\n\n\n<p># cert client.crt<\/p>\n\n\n\n<p># key client.key<\/p>\n\n\n\n<p>Append the following lines at the end of the file to match the server settings:<\/p>\n\n\n\n<p>auth SHA256<\/p>\n\n\n\n<p>key-direction 1<\/p>\n\n\n\n<p>Once you are done, the server configuration file should look something like this:<\/p>\n\n\n\n<p>client<\/p>\n\n\n\n<p>dev tun<\/p>\n\n\n\n<p>proto udp<\/p>\n\n\n\n<p>remote 118.31.38.47 1194<\/p>\n\n\n\n<p>resolv-retry infinite<\/p>\n\n\n\n<p>nobind<\/p>\n\n\n\n<p>persist-key<\/p>\n\n\n\n<p>persist-tun<\/p>\n\n\n\n<p>remote-cert-tls server<\/p>\n\n\n\n<p>tls-auth ta.key 1<\/p>\n\n\n\n<p>cipher AES-256-CBC<\/p>\n\n\n\n<p>verb 3<\/p>\n\n\n\n<p>auth SHA256<\/p>\n\n\n\n<p>key-direction 1<\/p>\n\n\n\n<p>Next, create a simple bash script that will merge the base configuration and files with the client certificate and key, and store the generated configuration in the&nbsp;openvpn-clients\/configs directory.<\/p>\n\n\n\n<p>vi openvpn-clients\/gen_config.sh<\/p>\n\n\n\n<p>#!\/bin\/bash<\/p>\n\n\n\n<p>FILES_DIR=\/etc\/openvpn\/openvpn-clients\/files<\/p>\n\n\n\n<p>BASE_DIR=\/etc\/openvpn\/openvpn-clients\/base<\/p>\n\n\n\n<p>CONFIGS_DIR=\/etc\/openvpn\/openvpn-clients\/configs<\/p>\n\n\n\n<p>BASE_CONF=${BASE_DIR}\/client.conf<\/p>\n\n\n\n<p>CA_FILE=${BASE_DIR}\/ca.crt<\/p>\n\n\n\n<p>TA_FILE=${BASE_DIR}\/ta.key<\/p>\n\n\n\n<p>CLIENT_CERT=${FILES_DIR}\/${1}.crt<\/p>\n\n\n\n<p>CLIENT_KEY=${FILES_DIR}\/${1}.key<\/p>\n\n\n\n<p># Test for files<\/p>\n\n\n\n<p>for i in &#8220;$BASE_CONF&#8221; &#8220;$CA_FILE&#8221; &#8220;$TA_FILE&#8221; &#8220;$CLIENT_CERT&#8221; &#8220;$CLIENT_KEY&#8221;; do<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if [[ ! -f $i ]]; then<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &#8221; The file $i does not exist&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit 1<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;fi<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if [[ ! -r $i ]]; then<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &#8221; The file $i is not readable.&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit 1<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;fi<\/p>\n\n\n\n<p>done<\/p>\n\n\n\n<p># Generate client config<\/p>\n\n\n\n<p>cat &gt; ${CONFIGS_DIR}\/${1}.ovpn &lt;&lt;EOF<\/p>\n\n\n\n<p>$(cat ${BASE_CONF})<\/p>\n\n\n\n<p>&lt;key&gt;<\/p>\n\n\n\n<p>$(cat ${CLIENT_KEY})<\/p>\n\n\n\n<p>&lt;\/key&gt;<\/p>\n\n\n\n<p>&lt;cert&gt;<\/p>\n\n\n\n<p>$(cat ${CLIENT_CERT})<\/p>\n\n\n\n<p>&lt;\/cert&gt;<\/p>\n\n\n\n<p>&lt;ca&gt;<\/p>\n\n\n\n<p>$(cat ${CA_FILE})<\/p>\n\n\n\n<p>&lt;\/ca&gt;<\/p>\n\n\n\n<p>&lt;tls-auth&gt;<\/p>\n\n\n\n<p>$(cat ${TA_FILE})<\/p>\n\n\n\n<p>&lt;\/tls-auth&gt;<\/p>\n\n\n\n<p>EOF<\/p>\n\n\n\n<p>Save the file and make it executable by running:<\/p>\n\n\n\n<p>chmod u+x openvpn-clients\/gen_config.sh<\/p>\n\n\n\n<p><strong>Creating Client Certificate Private Key and Configuration<\/strong><strong><\/strong><\/p>\n\n\n\n<p>In this example the name of the first VPN client will be&nbsp;david.<\/p>\n\n\n\n<p># cd \/usr\/local\/EasyRSA-3.0.5\/<\/p>\n\n\n\n<p># .\/easyrsa gen-req david&nbsp;nopass<\/p>\n\n\n\n<p>The command will create two files, a private key (david.key) and a certificate request file (david.req).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"612\" height=\"362\" src=\"http:\/\/www.betterit360.com\/wp-content\/uploads\/2019\/12\/openvpn_\u56fe\u72477.png\" alt=\"\" class=\"wp-image-178\"\/><\/figure>\n\n\n\n<p>Copy the private key david.key to the \/etc\/openvpn\/openvpn-clients\/files directory you created in the previous section:<\/p>\n\n\n\n<p># cp pki\/private\/david.key \/etc\/openvpn\/openvpn-clients\/files\/<\/p>\n\n\n\n<p>run the following command to sign the request:<\/p>\n\n\n\n<p># .\/easyrsa sign-req client david<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"575\" height=\"501\" src=\"http:\/\/www.betterit360.com\/wp-content\/uploads\/2019\/12\/openvpn_\u56fe\u72478.png\" alt=\"\" class=\"wp-image-179\"\/><\/figure>\n\n\n\n<p>Copy the david.crt file to the \/etc\/openvpn\/openvpn-clients\/files directory<\/p>\n\n\n\n<p># cp pki\/issued\/david.crt \/etc\/openvpn\/openvpn-clients\/files\/<\/p>\n\n\n\n<p>The final step is to generate a client configuration using the gen_config.sh script:<\/p>\n\n\n\n<p># cd \/etc\/openvpn\/openvpn-clients\/<\/p>\n\n\n\n<p># .\/gen_config.sh david<\/p>\n\n\n\n<p>The script will create a file named david.ovpn in the configs directory. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"443\" height=\"63\" src=\"http:\/\/www.betterit360.com\/wp-content\/uploads\/2019\/12\/openvpn_\u56fe\u72479.png\" alt=\"\" class=\"wp-image-180\"\/><\/figure>\n\n\n\n<p>At this point the client configuration is created. You can now transfer the configuration file to the device you intend to use as a client.<\/p>\n\n\n\n<p><strong>Revoking Client Certificates<\/strong><strong><\/strong><\/p>\n\n\n\n<p>Revoking a certificate means to invalidate a signed certificate so that it can no longer be used for accessing the OpenVPN server.<\/p>\n\n\n\n<p>Run the easyrsa script using the revoke argument, followed by the client name you want to revoke:<\/p>\n\n\n\n<p># .\/easyrsa revoke david<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"572\" height=\"362\" src=\"http:\/\/www.betterit360.com\/wp-content\/uploads\/2019\/12\/openvpn_\u56fe\u724710.png\" alt=\"\" class=\"wp-image-181\"\/><\/figure>\n\n\n\n<p>Use the gen-crl option to generate a certificate revocation list (CRL):<\/p>\n\n\n\n<p># .\/easyrsa gen-crl<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"461\" height=\"176\" src=\"http:\/\/www.betterit360.com\/wp-content\/uploads\/2019\/12\/openvpn_\u56fe\u724711.png\" alt=\"\" class=\"wp-image-182\"\/><\/figure>\n\n\n\n<p>Copy&nbsp;the file crl.pem to the \/etc\/openvpn directory:<\/p>\n\n\n\n<p># cp pki\/crl.pem \/etc\/openvpn\/<\/p>\n\n\n\n<p>Open the OpenVPN server configuration file,Paste the following line at the end of the file:<\/p>\n\n\n\n<p># vi \/etc\/openvpn\/vpnsvr01.conf<\/p>\n\n\n\n<p>crl-verify crl.pem<\/p>\n\n\n\n<p>Restart the OpenVPN service for the revocation directive to take effect:<\/p>\n\n\n\n<p># systemctl restart openvpn@vpnsvr01<\/p>\n\n\n\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n\n\n\n<p>1.openvpn\u670d\u52a1\u5668\u9632\u706b\u5899\u5fc5\u987b\u5f00\u542f\uff0c\u5e76\u914d\u7f6e\u76f8\u5173\u89c4\u5219\u3002\u4e0d\u5f00\u542f\u9632\u706b\u5899\uff0copenvpn\u5ba2\u6237\u7aef\u8fde\u63a5\u540e\u65e0\u6cd5\u8fde\u901aopenvpn\u670d\u52a1\u5668\u6240\u5728\u7f51\u6bb5\u53ca\u5176\u5b83\u7f51\u6bb5IP\u3002<\/p>\n\n\n\n<p>2.openvpn\u5ba2\u6237\u7aef\u8981\u8fde\u901aopenvpn\u670d\u52a1\u5668\u6240\u5728\u7f51\u6bb5\u53ca\u5176\u5b83\u7f51\u6bb5IP\uff0c\u8fd8\u9700\u5728openvpn\u670d\u52a1\u5668\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u7c7b\u4f3c\u5982\u4e0b\u914d\u7f6e\u9879\uff1a<\/p>\n\n\n\n<p>push &#8220;route 192.168.50.0 255.255.255.0&#8221; &nbsp;\/\/openvpn\u670d\u52a1\u5668\u6240\u5728\u7f51\u6bb5IP<\/p>\n\n\n\n<p>push &#8220;route 192.168.30.0 255.255.255.0&#8221; &nbsp;\/\/\u5176\u5b83\u7f51\u6bb5IP<\/p>\n\n\n\n<p>3.openvpn\u5ba2\u6237\u7aef\u8fde\u63a5\u540e\uff0c\u8981\u5229\u7528openvpn\u670d\u52a1\u5668\u4e0a\u7f51\uff0c\u5373openvpn\u5ba2\u6237\u7aef\u8fde\u63a5\u540e\u7684\u516c\u7f51IP\u4e0eopenvpn\u670d\u52a1\u5668\u7684\u516c\u7f51IP\u76f8\u540c\uff0c\u9700\u8981\u5728openvpn\u670d\u52a1\u5668\u914d\u7f6e\u6587\u4ef6\u4e2d\u5f00\u542f\u5982\u4e0b\u914d\u7f6e\u9879\uff1a<\/p>\n\n\n\n<p>push &#8220;redirect-gateway def1 bypass-dhcp&#8221;<\/p>\n\n\n\n<p>4.\u901a\u8fc7.\/easyrsa gen-req david&nbsp;nopass\u521b\u5efa\u751f\u6210\u7684\u914d\u7f6e\u6587\u4ef6\uff08ovpn\uff09\u8fde\u63a5\u670d\u52a1\u5668\u65f6\u65e0\u9700\u8f93\u5165\u5bc6\u7801\uff0c\u82e5\u8fde\u63a5\u65f6\u9700\u8981\u8f93\u5165\u5bc6\u7801\uff0c\u5219\u4f7f\u7528.\/easyrsa gen-req david\u5e76\u8bbe\u7f6e\u5bc6\u7801\u3002<\/p>\n\n\n\n<p>5.\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u65e5\u5fd7\u6d88\u606f\u5199\u5165\/var\/log\/messages\uff0copenvpn\u670d\u52a1\u5668\u914d\u7f6e\u6587\u4ef6\u4e2d\u53d6\u6d88\u5982\u4e0b\u914d\u7f6e\u9879\u524d\u7684\u6ce8\u91ca\u53ef\u5c06\u65e5\u5fd7\u6d88\u606f\u5199\u5165openvpn.log\u3002<\/p>\n\n\n\n<p>;log-append openvpn.log<\/p>\n\n\n\n<p>6.\u82e5openvpn\u670d\u52a1\u5668\u914d\u7f6e\u6587\u4ef6\u4e2d\u53bb\u6389\u914d\u7f6e\u9879crl-verify crl.pem\u5e76\u91cd\u542fopenvpn\uff0c\u5219\u540a\u9500\u8bc1\u4e66\u7684\u7528\u6237\u4ecd\u53ef\u8fde\u63a5\u3002<\/p>\n\n\n\n<p><strong>\u9644\u5f55\uff1a<\/strong>\u521b\u5efa\u7528\u6237\u8bc1\u4e66\u3001\u521b\u5efa\u7528\u6237VPN\u914d\u7f6e\u6587\u4ef6\u3001\u540a\u9500\u7528\u6237\u8bc1\u4e66\u64cd\u4f5c\u90fd\u53ef\u901a\u8fc7\u811a\u672c\u7b80\u5316\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u811a\u672c\u5185\u5bb9\u53c2\u8003\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p><strong>\u521b\u5efa\u7528\u6237\u8bc1\u4e66\uff1a<\/strong><strong><\/strong><\/p>\n\n\n\n<p># cat create_client_cert.sh <\/p>\n\n\n\n<p>#!\/bin\/bash<\/p>\n\n\n\n<p>echo &#8220;Please input a client name for the client certificate.&#8221;<\/p>\n\n\n\n<p>read -p &#8220;Client name: &#8221; -e CLIENT<\/p>\n\n\n\n<p>cd \/usr\/local\/EasyRSA-3.0.5\/<\/p>\n\n\n\n<p>.\/easyrsa gen-req $CLIENT<\/p>\n\n\n\n<p>cp pki\/private\/$CLIENT.key \/etc\/openvpn\/openvpn-clients\/files\/<\/p>\n\n\n\n<p>.\/easyrsa sign-req client $CLIENT<\/p>\n\n\n\n<p>cp pki\/issued\/$CLIENT.crt \/etc\/openvpn\/openvpn-clients\/files\/<\/p>\n\n\n\n<p>echo &#8220;Client $CLIENT cert created&#8221;<\/p>\n\n\n\n<p>exit<\/p>\n\n\n\n<p><strong>\u521b\u5efa\u7528\u6237VPN\u914d\u7f6e\u6587\u4ef6\uff1a<\/strong><strong><\/strong><\/p>\n\n\n\n<p># cat gen_client_ovpn.sh<\/p>\n\n\n\n<p>#!\/bin\/bash<\/p>\n\n\n\n<p>echo &#8220;Please input a client cert name for the client ovpn.&#8221;<\/p>\n\n\n\n<p>read -p &#8220;Client name: &#8221; -e CLIENT<\/p>\n\n\n\n<p>FILES_DIR=\/etc\/openvpn\/openvpn-clients\/files<\/p>\n\n\n\n<p>BASE_DIR=\/etc\/openvpn\/openvpn-clients\/base<\/p>\n\n\n\n<p>CONFIGS_DIR=\/etc\/openvpn\/openvpn-clients\/configs<\/p>\n\n\n\n<p>BASE_CONF=${BASE_DIR}\/client.conf<\/p>\n\n\n\n<p>CA_FILE=${BASE_DIR}\/ca.crt<\/p>\n\n\n\n<p>TA_FILE=${BASE_DIR}\/ta.key<\/p>\n\n\n\n<p>CLIENT_CERT=${FILES_DIR}\/$CLIENT.crt<\/p>\n\n\n\n<p>CLIENT_KEY=${FILES_DIR}\/$CLIENT.key<\/p>\n\n\n\n<p>cat &gt; ${CONFIGS_DIR}\/$CLIENT.ovpn &lt;&lt;EOF<\/p>\n\n\n\n<p>$(cat ${BASE_CONF})<\/p>\n\n\n\n<p>&lt;key&gt;<\/p>\n\n\n\n<p>$(cat ${CLIENT_KEY})<\/p>\n\n\n\n<p>&lt;\/key&gt;<\/p>\n\n\n\n<p>&lt;cert&gt;<\/p>\n\n\n\n<p>$(cat ${CLIENT_CERT})<\/p>\n\n\n\n<p>&lt;\/cert&gt;<\/p>\n\n\n\n<p>&lt;ca&gt;<\/p>\n\n\n\n<p>$(cat ${CA_FILE})<\/p>\n\n\n\n<p>&lt;\/ca&gt;<\/p>\n\n\n\n<p>&lt;tls-auth&gt;<\/p>\n\n\n\n<p>$(cat ${TA_FILE})<\/p>\n\n\n\n<p>&lt;\/tls-auth&gt;<\/p>\n\n\n\n<p>EOF<\/p>\n\n\n\n<p>echo &#8220;Client $CLIENT ovpn created&#8221;<\/p>\n\n\n\n<p>exit<\/p>\n\n\n\n<p>\u6ce8\uff1aVPN\u914d\u7f6e\u6587\u4ef6\u5b58\u653e\u8def\u5f84\uff1a\/etc\/openvpn\/openvpn-clients\/configs\uff0c\u7528\u6237\u4f7f\u7528\u751f\u6210\u7684ovpn\u6587\u4ef6\u53ca\u521b\u5efa\u7528\u6237\u8bc1\u4e66\u65f6\u8bbe\u7f6e\u7684\u5bc6\u94a5\u5373\u53ef\u8fde\u63a5VPN\u670d\u52a1\u5668\u3002<\/p>\n\n\n\n<p><strong>\u540a\u9500\u7528\u6237\u8bc1\u4e66\uff1a<\/strong><strong><\/strong><\/p>\n\n\n\n<p># cat revoke_client_cert.sh<\/p>\n\n\n\n<p>#!\/bin\/bash<\/p>\n\n\n\n<p>echo &#8220;Please input a client cert name for revoke.&#8221;<\/p>\n\n\n\n<p>read -p &#8220;Client name: &#8221; -e CLIENT<\/p>\n\n\n\n<p>cd \/usr\/local\/EasyRSA-3.0.5\/<\/p>\n\n\n\n<p>.\/easyrsa revoke $CLIENT<\/p>\n\n\n\n<p>.\/easyrsa gen-crl<\/p>\n\n\n\n<p>\\cp -f pki\/crl.pem \/etc\/openvpn\/<\/p>\n\n\n\n<p>systemctl restart openvpn@vpnsvr<\/p>\n\n\n\n<p>echo &#8220;Client $CLIENT cert revoked&#8221;<\/p>\n\n\n\n<p>exit<\/p>\n\n\n\n<p>\u6ce8\uff1a\u8fd0\u884c\u6b64\u811a\u672c\u540a\u9500\u7528\u6237\u8bc1\u4e66\u540e\uff0c\u7528\u6237\u5373\u65e0\u6cd5\u8fde\u63a5VPN\u670d\u52a1\u5668\u3002\u82e5\u9700\u8981\u6062\u590d\u4f7f\u7528\uff0c\u9700\u518d\u6b21\u521b\u5efa\u7528\u6237\u8bc1\u4e66\u53caVPN\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Installing OpenVPN # yum install epel-release # yum ins &hellip; <a href=\"http:\/\/www.betterit360.com\/?p=171\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cSet Up and Configure an OpenVPN Server on CentOS 7\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.betterit360.com\/index.php?rest_route=\/wp\/v2\/posts\/171"}],"collection":[{"href":"http:\/\/www.betterit360.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.betterit360.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.betterit360.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.betterit360.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=171"}],"version-history":[{"count":0,"href":"http:\/\/www.betterit360.com\/index.php?rest_route=\/wp\/v2\/posts\/171\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.betterit360.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.betterit360.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.betterit360.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}