OpenVPN Server Setup on CentOS: Step-by-Step
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
- Update the system
Runsudo yum update -y(orsudo dnf update -yon CentOS 8/9) to ensure all packages are current. - Install OpenVPN and Easy-RSA
Usesudo yum install -y openvpn easy-rsa(orsudo dnf install -y openvpn easy-rsa). This installs the VPN daemon and the certificate authority tools. - 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/varsand set your desired values forset_var EASYRSA_REQ_COUNTRY,PROVINCE,CITY,ORG,EMAIL, andOU. These will be embedded in your certificates. - Initialize the PKI
Runcd /etc/openvpn/easy-rsa && sudo ./easyrsa init-pkito create the public key infrastructure directory. - Build the CA
Executesudo ./easyrsa build-ca nopass. When prompted, enter a Common Name (e.g.,myvpn-ca). This creates the CA certificate and key. - Generate the server certificate and key
Runsudo ./easyrsa gen-req server nopassand thensudo ./easyrsa sign-req server server. This creates a key and a signed certificate for the server. - Generate Diffie-Hellman parameters
Runsudo ./easyrsa gen-dh. This may take a few minutes. It creates the DH parameters file used for key exchange. - Generate a client certificate (repeat for each client)
Runsudo ./easyrsa gen-req client1 nopassandsudo ./easyrsa sign-req client client1. Replaceclient1with a unique name for each device. - 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/
- Create the server configuration file
Create/etc/openvpn/server/server.confwith 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
serverline defines the VPN subnet. Thepushlines tell clients to route all traffic through the VPN and use Google’s DNS. - Enable IP forwarding
Edit/etc/sysctl.confand setnet.ipv4.ip_forward = 1. Then runsudo sysctl -pto apply. - 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
- Start and enable the OpenVPN service
Runsudo systemctl start openvpn-server@serverandsudo systemctl enable openvpn-server@server. The@serverrefers to the configuration file name without the.confextension. - Check the server status
Usesudo systemctl status openvpn-server@serverto verify it’s running. Also check the log withsudo tail -f /var/log/openvpn/openvpn.log(or the location set in your config). - Create client configuration files
For each client, create a.ovpnfile 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_IPwith your server’s public IP. Include the actual certificate and key contents between the<ca>,<cert>, and<key>tags, or reference separate files. - 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.ovpnfile, 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
nopassfor 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-cryptortls-authto add an extra layer of security against port scanning. - For better performance, use
cipher AES-256-GCMif both server and clients support it. - Back up your
/etc/openvpn/easy-rsa/pkidirectory – if lost, you cannot add new clients or revoke old ones.
Troubleshooting
- Can’t connect – timeout
Check that the firewall is allowing UDP 1194. Runsudo firewall-cmd --list-allto see open ports. Also verify the server is listening withsudo netstat -ulnp | grep 1194. - Connection established but no internet
Verify IP forwarding is enabled (sysctl net.ipv4.ip_forwardshould 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 withip route. The VPN subnet should be routed viatun0. Also check that the client’s firewall isn’t blocking ICMP. - OpenVPN service fails to start
Check the log withsudo journalctl -u openvpn-server@server -e. Common causes: missing certificates, wrong paths, or permission errors. Ensure thenobodyuser 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 thesndbufandrcvbufsettings. 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.