Install OpenVPN server CentOS 7

OpenVPN server CentOS 7

OpenVPN server CentOS 7

Today I will brief necessary steps for installing OpenVPN server CentOS 7. This will include custom server name and also rules for csf firewall which is widely used. The OpenVPN server name in this tutorial will be myvpnserver, associated domain will be, and server IP address is XXX.YYY.ZZZ.UUU . We will user a self-signed certificate for this case.

  1. First, we will add epel -release

    yum install epel-release
  2. Then, install open-vpn server

    yum install openvpn easy-rsa -y
  3. Next we will configure OpenVPN server

    1. First, we will copy the sample server.conf file as a starting point for our own configuration file (since we use myvpnserver as server name, we will use this name):
      cp /usr/share/doc/openvpn-*/sample/sample-config-files/server.conf /etc/openvpn/myvpnserver.conf
    2. Edit the file:
      nano /etc/openvpn/myvpnserver.conf
      • When we generate the keys, the default Diffie-Hellman encryption length for Easy RSA will be 2048 bytes, so we need to change the dh filename to dh2048.pem:
        dh dh2048.pem
      • Uncomment the push “redirect-gateway def1 bypass-dhcp” line to tell clients to redirect all traffic through the OpenVPN:
        push "redirect-gateway def1 bypass-dhcp"
      • We will use Google DNS for the OpenVPN server, so uncomment the push “dhcp-option DNS line:
        push "dhcp-option DNS"
        push "dhcp-option DNS"
      • Update user and group to nobody so that OpenVPN runs with no privileges:
        user nobody
        group nobody
      • Save the .conf file and exit.
    3. Next, we need to Generate Keys and Certificates for the OpenVPN server:
      • Create a directory for the keys:
        mkdir -p /etc/openvpn/easy-rsa/keys
      • Copy the key and cert generation scripts to this folder so that we will use later:
        cp -rf /usr/share/easy-rsa/2.0/* /etc/openvpn/easy-rsa
      • Edit the /etc/openvpn/easy-rsa/vars file. The most important information to edit is KEY_NAME and KEY_CN to reflect the server name and domain name:
        # Don't leave any of these fields blank.
        export KEY_COUNTRY="VN"
        export KEY_PROVINCE="HCM"
        export KEY_CITY="Ho Chi Minh"
        export KEY_ORG="YourORNAME-org"
        export KEY_EMAIL="[email protected]"
        export KEY_OU="YourOrgUnit"
        # X509 Subject Field
        export KEY_NAME="myvpnserver"
        # PKCS11 Smart Card
        # export PKCS11_MODULE_PATH="/usr/lib/"
        # export PKCS11_PIN=1234
        # If you'd like to sign all keys with the same Common Name, uncomment the KEY_CN export below
        # You will also need to make sure your OpenVPN server config has the duplicate-cn option set
        export KEY_CN=""
      • Copy the openssl configuration file:
        cp /etc/openvpn/easy-rsa/openssl-1.0.0.cnf /etc/openvpn/easy-rsa/openssl.cnf
      • We will then start generating keys:
        cd /etc/openvpn/easy-rsa
        source ./vars
        ./build-key-server myvpnserver
      • Next, copy key and cert files to OpenVPN server:
        cd /etc/openvpn/easy-rsa/keys
        cp dh2048.pem ca.crt server.crt server.key /etc/openvpn
      • Next, generate the key and cert for each client to connect. For example, we will generate the key and cert for user “client”:
        cd /etc/openvpn/easy-rsa
        ./build-key client
  4. Setup routing rules for CSF firewall

    1. Enable IP forwarding by editing /etc/sysctl.conf and add the following line:
      net.ipv4.ip_forward = 1
    2. Restart network service:
      systemctl restart network.service
    3. Add pre-routing rules to CSF firewall by adding the following lines to /etc/csf/, remember to update your server IP:
      iptables -A INPUT -j ACCEPT -s -i tun0
      iptables -A OUTPUT -j ACCEPT -s -o tun0
      iptables -A FORWARD -j ACCEPT -p all -s 0/0 -i tun0
      iptables -A FORWARD -j ACCEPT -p all -s 0/0 -o tun0
      iptables -t nat --flush
      iptables -t nat -A POSTROUTING -o venet0 -s -j SNAT --to XXX.YYY.ZZZ.UUU
    4. Restart CSF firewall:
      csf -r
  5. Start OpenVPN

    systemctl -f enable [email protected]
    systemctl start [email protected]
  6. Configure and connect from a client

    1. First, copy client cert and key + ca from the following path to the client machine:
    2. We will need to create a configuration file (client.ovpn) for an OpenVPN client, telling it how to connect to the server. Remember to update client name (client), your server IP (XXX.YYY.ZZZ.UUU), and path to client’s key and certs (ca.crt, client.crt, client.key):
      dev tun
      proto udp
      remote XXX.YYY.ZZZ.UUU 1194
      resolv-retry infinite
      verb 3
      ca /path/to/ca.crt
      cert /path/to/client.crt
      key /path/to/client.key
    3. Use the OpenVPN client software to connect to the server:
      • On MacOS: Usetunnelblick at We will need to place .ovpn file to the ~/Library/Application\ Support/Tunnelblick/Configurations/ folder
      • On Windows: Use OpenVPN binary: Remember to put .ovpn to the OpenVPN config C:\Program Files\OpenVPN\config folder
      • On Linux: we will install OpenVPN from OS official repositories and then invoke OpenVPN by executing:
        sudo openvpn --config ~/path/to/client.ovpn
  7. Done and enjoy 🙂

Leave a Reply