ThisCalculator

Everyday

OpenVPN Server Setup on CentOS: Step-by-Step

Updated · Standard formula, shown below

This guide walks you through installing and configuring an OpenVPN server on CentOS (7, 8, or 9). You’ll end up with a secure VPN that lets remote clients connect to your network, encrypting all traffic between them and your server. We’ll cover installation, certificate generation, server and client configuration, and how to test the connection.

What You’ll Need

  • A CentOS server (physical or VPS) with root or sudo access.
  • A static IP address or a hostname that resolves to your server.
  • Firewall access to open port 1194 (UDP) if you use a firewall.
  • OpenVPN and Easy-RSA packages (we’ll install them).
  • Basic familiarity with the command line.

Step-by-Step

  1. Update the system
    Run sudo yum update -y (or sudo dnf update -y on CentOS 8/9) to ensure all packages are current.
  2. Install OpenVPN and Easy-RSA
    Use sudo yum install -y openvpn easy-rsa (or sudo dnf install -y openvpn easy-rsa). This installs the VPN daemon and the certificate authority tools.
  3. Set up Easy-RSA
    Copy the Easy-RSA sample configuration: sudo cp -r /usr/share/easy-rsa/3 /etc/openvpn/easy-rsa. Then edit /etc/openvpn/easy-rsa/vars and set your desired values for set_var EASYRSA_REQ_COUNTRY, PROVINCE, CITY, ORG, EMAIL, and OU. These will be embedded in your certificates.
  4. Initialize the PKI
    Run cd /etc/openvpn/easy-rsa && sudo ./easyrsa init-pki to create the public key infrastructure directory.
  5. Build the CA
    Execute sudo ./easyrsa build-ca nopass. When prompted, enter a Common Name (e.g., myvpn-ca). This creates the CA certificate and key.
  6. Generate the server certificate and key
    Run sudo ./easyrsa gen-req server nopass and then sudo ./easyrsa sign-req server server. This creates a key and a signed certificate for the server.
  7. Generate Diffie-Hellman parameters
    Run sudo ./easyrsa gen-dh. This may take a few minutes. It creates the DH parameters file used for key exchange.
  8. Generate a client certificate (repeat for each client)
    Run sudo ./easyrsa gen-req client1 nopass and sudo ./easyrsa sign-req client client1. Replace client1 with a unique name for each device.
  9. Copy certificates to the OpenVPN directory
    Copy the server certificate, key, and DH file to /etc/openvpn/server (create it if needed). Also copy the CA certificate. For example:
    sudo mkdir -p /etc/openvpn/server
    sudo cp pki/ca.crt pki/issued/server.crt pki/private/server.key pki/dh.pem /etc/openvpn/server/
  10. Create the server configuration file
    Create /etc/openvpn/server/server.conf with the following content (adjust as needed):
    port 1194
    proto udp
    dev tun
    ca /etc/openvpn/server/ca.crt
    cert /etc/openvpn/server/server.crt
    key /etc/openvpn/server/server.key
    dh /etc/openvpn/server/dh.pem
    server 10.8.0.0 255.255.255.0
    ifconfig-pool-persist ipp.txt
    push "redirect-gateway def1 bypass-dhcp"
    push "dhcp-option DNS 8.8.8.8"
    push "dhcp-option DNS 8.8.4.4"
    keepalive 10 120
    cipher AES-256-CBC
    auth SHA256
    user nobody
    group nobody
    persist-key
    persist-tun
    status openvpn-status.log
    verb 3

    The server line defines the VPN subnet. The push lines tell clients to route all traffic through the VPN and use Google’s DNS.

  11. Enable IP forwarding
    Edit /etc/sysctl.conf and set net.ipv4.ip_forward = 1. Then run sudo sysctl -p to apply.
  12. Configure NAT (if you want clients to access the internet)
    Add a firewall rule to masquerade traffic from the VPN subnet. Using firewalld (default on CentOS 7+):
    sudo firewall-cmd --permanent --add-masquerade
    sudo firewall-cmd --permanent --add-port=1194/udp
    sudo firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=10.8.0.0/24 masquerade'
    sudo firewall-cmd --reload
  13. Start and enable the OpenVPN service
    Run sudo systemctl start openvpn-server@server and sudo systemctl enable openvpn-server@server. The @server refers to the configuration file name without the .conf extension.
  14. Check the server status
    Use sudo systemctl status openvpn-server@server to verify it’s running. Also check the log with sudo tail -f /var/log/openvpn/openvpn.log (or the location set in your config).
  15. Create client configuration files
    For each client, create a .ovpn file that includes the CA certificate, client certificate, and client key. You can do this manually or use a script. A minimal client config looks like:
    client
    dev tun
    proto udp
    remote YOUR_SERVER_IP 1194
    resolv-retry infinite
    nobind
    persist-key
    persist-tun
    ca ca.crt
    cert client1.crt
    key client1.key
    cipher AES-256-CBC
    auth SHA256
    verb 3

    Replace YOUR_SERVER_IP with your server’s public IP. Include the actual certificate and key contents between the <ca>, <cert>, and <key> tags, or reference separate files.

  16. Test the VPN connection
    Install an OpenVPN client on a device (e.g., OpenVPN Connect on a phone or the openvpn package on another Linux machine), import the .ovpn file, and connect. Once connected, you should be able to ping the server’s VPN IP (10.8.0.1) and access resources on the server’s network.

Common Mistakes

  • Forgetting to enable IP forwarding – clients can connect but cannot route traffic.
  • Not opening the firewall port – connection times out.
  • Using the same certificate for multiple clients – OpenVPN may reject duplicate connections.
  • Incorrect file permissions on keys – OpenVPN will fail to start if the key is world-readable.
  • Mismatched cipher or auth settings between server and client – connection fails with a handshake error.

Tips

  • Always use nopass for server and client keys to avoid password prompts during startup.
  • Use a certificate revocation list (CRL) if you need to revoke a client’s access later.
  • Consider using tls-crypt or tls-auth to add an extra layer of security against port scanning.
  • For better performance, use cipher AES-256-GCM if both server and clients support it.
  • Back up your /etc/openvpn/easy-rsa/pki directory – if lost, you cannot add new clients or revoke old ones.

Troubleshooting

  • Can’t connect – timeout
    Check that the firewall is allowing UDP 1194. Run sudo firewall-cmd --list-all to see open ports. Also verify the server is listening with sudo netstat -ulnp | grep 1194.
  • Connection established but no internet
    Verify IP forwarding is enabled (sysctl net.ipv4.ip_forward should return 1). Check that masquerade is active: sudo firewall-cmd --query-masquerade. Also ensure your server’s external interface is correct in the NAT rule.
  • Client gets an IP but cannot ping anything
    Check the server’s routing table with ip route. The VPN subnet should be routed via tun0. Also check that the client’s firewall isn’t blocking ICMP.
  • OpenVPN service fails to start
    Check the log with sudo journalctl -u openvpn-server@server -e. Common causes: missing certificates, wrong paths, or permission errors. Ensure the nobody user can read the key files.
  • Certificate errors
    If you see “TLS Error: TLS key negotiation failed”, the certificates may be mismatched or the CA not recognized. Regenerate the client certificate and ensure the CA is included in the client config.
  • Slow performance
    Try switching to UDP if you’re using TCP, or adjust the sndbuf and rcvbuf settings. Also consider using a faster cipher like AES-128-GCM if security requirements allow.

Frequently Asked Questions

Do I need a static IP for my server?

Yes, a static IP is highly recommended. If your server IP changes, clients won’t be able to connect unless you update the remote line in their configs.

Can I use this setup for multiple clients?

Absolutely. Generate a unique certificate for each client. You can create as many as you need, but each must be signed by your CA.

How do I revoke a client’s access?

Use Easy-RSA to revoke the certificate: sudo ./easyrsa revoke client1, then generate a CRL and add crl-verify /path/to/crl.pem to your server config.

Is OpenVPN secure enough for production use?

Yes, OpenVPN is widely used in production. The security depends on your configuration – use strong ciphers, keep your CA key secure, and regularly update OpenVPN.

Can I run OpenVPN on a different port?

Yes, change the port directive in the server config and update the firewall rule and client configs accordingly. Using port 443 TCP can help bypass restrictive networks.

What if I want clients to only access the server, not the internet?

Remove the push "redirect-gateway" line from the server config. Clients will then only route traffic to the VPN subnet.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.