Setting up an open source VPN gives you secure, private internet access without relying on commercial services, using freely available software you can audit and modify yourself.
What You'll Need
- A server (physical machine, VPS, or cloud instance) running a Linux distribution like Ubuntu 22.04 LTS or Debian 12
- Root or sudo access to the server
- A domain name pointing to your server’s public IP (optional but recommended for easier client setup)
- A client device (Windows, macOS, Linux, iOS, or Android) to connect from
- Basic familiarity with the Linux command line
Step-by-Step
- Update your server’s package list and install dependencies: run
sudo apt update && sudo apt install -y openvpn easy-rsaon Ubuntu/Debian. On CentOS/RHEL, usesudo yum install -y openvpn easy-rsaordnffor newer versions. - Set up the Easy-RSA PKI directory:
make-cadir ~/openvpn-ca, thencd ~/openvpn-ca. Edit thevarsfile to set your KEY_COUNTRY, KEY_PROVINCE, KEY_CITY, KEY_ORG, and KEY_EMAIL values. - Load the variables and build the CA:
source varsfollowed by./clean-alland./build-ca. Press Enter to accept defaults or enter your details when prompted. - Generate the server certificate and key:
./build-key-server server. Again, accept defaults or enter a challenge password if desired (leave blank for no password). - Generate Diffie-Hellman parameters:
./build-dh. This may take a few minutes. - Generate an HMAC signature to strengthen the server’s TLS integrity verification:
openvpn --genkey --secret ta.key. - Copy the generated files to the OpenVPN directory:
sudo cp ~/openvpn-ca/keys/{ca.crt,server.crt,server.key,ta.key,dh2048.pem} /etc/openvpn/. - Create the server configuration file:
sudo nano /etc/openvpn/server.confand add: port 1194 proto udp dev tun ca ca.crt cert server.crt key server.key dh dh2048.pem tls-auth ta.key 0 topology subnet 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 nogroup persist-key persist-tun status openvpn-status.log verb 3 Save and exit (Ctrl+O, Enter, Ctrl+X in nano). - Enable IP forwarding: edit
/etc/sysctl.confand uncomment or addnet.ipv4.ip_forward=1. Apply changes withsudo sysctl -p. - Configure the firewall (using UFW on Ubuntu):
sudo ufw allow 1194/udpandsudo ufw allow OpenSSH. Then enable forwarding by editing/etc/ufw/before.rulesand adding after the *filter line: *nat :POSTROUTING ACCEPT [0:0] -A POSTROUTING -s 10.8.0.0/8 -o eth0 -j MASQUERADE COMMIT Replaceeth0with your server’s public interface (find it withip route get 8.8.8.8). - Disable UFW temporarily to avoid conflicts during setup:
sudo ufw disable, then re-enable after rules are in place:sudo ufw enable. - Start and enable the OpenVPN service:
sudo systemctl start openvpn@serverandsudo systemctl enable openvpn@server. Check status withsudo systemctl status openvpn@server. - Generate client certificates:
cd ~/openvpn-ca,source vars, then./build-key client1(replace client1 with a name for your device). Leave challenge password blank unless required. - Create the client configuration file:
mkdir ~/client-configs/files,cp ~/openvpn-ca/keys/{ca.crt,client1.crt,client1.key} ~/client-configs/files/, andcp /etc/openvpn/ta.key ~/client-configs/files/. - Create a base client config:
nano ~/client-configs/base.confwith: client dev tun proto udp remote YOUR_SERVER_IP 1194 resolv-retry infinite nobind user nobody group nogroup persist-key persist-tun remote-cert-tls server tls-auth ta.key 1 cipher AES-256-CBC auth SHA256 verb 3 ReplaceYOUR_SERVER_IPwith your server’s public IP or domain name. - Make a script to generate unified .ovpn files:
nano ~/client-configs/make_config.shwith: #!/bin/bash KEY_DIR=~/openvpn-ca/keys OUTPUT_DIR=~/client-configs/files BASE_CONFIG=~/client-configs/base.conf cat ${BASE_CONFIG} \ <(echo -e '') \ ${KEY_DIR}/ca.crt \ <(echo -e ' \n') \ ${KEY_DIR}/${1}.crt \ <(echo -e ' \n') \ ${KEY_DIR}/${1}.key \ <(echo -e ' \n') \ ${KEY_DIR}/ta.key \ <(echo -e ' ') \ > ${OUTPUT_DIR}/${1}.ovpn Save, then make executable:chmod +x ~/client-configs/make_config.sh. - Generate the client .ovpn file:
cd ~/client-configs && ./make_config.sh client1. The fileclient1.ovpnwill be in~/client-configs/files/. - Transfer the .ovpn file to your client device (via email, USB, or secure copy). Install an OpenVPN client app (OpenVPN Connect for iOS/Android, Tunnelblick for macOS, or official OpenVPN client for Windows/Linux). Import the .ovpn file and connect.
- Verify the connection: check your public IP (e.g., via
curl ifconfig.me) — it should now match your server’s IP. Also confirm DNS leak protection by visitingdnsleaktest.com.
Common Mistakes
- Forgetting to enable IP forwarding in sysctl, which prevents traffic from routing through the VPN.
- Using the wrong network interface in the POSTROUTING rule (e.g., eth0 when the server uses ens5), causing NAT to fail.
- Not opening UDP port 1194 in the firewall, leading to connection timeouts.
- Using TCP instead of UDP in client/server configs when UDP is intended, causing poor performance or failed handshakes.
- Neglecting to set correct file permissions on keys and configs, leading to permission denied errors when starting the service.
- Using outdated Easy-RSA versions or skipping the
source varsstep, resulting in failed certificate generation.
Tips
- Use a domain name with Dynamic DNS (like DuckDNS or No-IP) if your server has a changing public IP, so clients don’t need reconfiguration.
- Enable TLS-auth (as shown) to defend against DoS attacks and port scanning — it adds a shared secret HMAC key.
- Consider using TCP port 443 instead of UDP 1194 if you need to bypass restrictive firewalls (e.g., in censored networks), though performance may suffer.
- Regularly update your server and OpenVPN to patch security vulnerabilities — enable automatic security updates if available.
- Monitor the OpenVPN log (
/var/log/openvpn.logor via journalctl) for connection issues or suspicious activity. - For mobile clients, enable compression and keepalive settings to maintain connections on unstable networks.
Troubleshooting
- If the client cannot connect, first check server status:
sudo systemctl status openvpn@server. Look for errors in the log. - Verify the server is listening:
sudo netstat -tulnp | grep 1194orss -tulnp | grep 1194. If not, the service failed to start. - Check firewall rules:
sudo ufw statusorsudo iptables -L -n -vto ensure UDP 1194 is allowed and NAT is active. - On the client, try connecting with increased verbosity: add
verb 4to the .ovpn file or use the app’s log feature to see handshake details. - If you see TLS handshake failures, confirm the ta.key is correctly included and that the server and client both have the same key.
- If connected but no internet access, verify IP forwarding (
cat /proc/sys/net/ipv4/ip_forwardshould return 1) and that the POSTROUTING rule matches your outgoing interface. - For DNS leaks, ensure the
push "dhcp-option DNS"lines are in the server config and that the client accepts pushed options (most do by default). - If the server log shows
Auth Username/Password verification failed for user, you may have accidentally enabled auth-user-pass without setting it up — remove it from config if not using certificates. - As a last resort, temporarily disable the firewall (
sudo ufw disable) to test if it’s blocking traffic — re-enable immediately after testing.
Frequently Asked Questions
Is OpenVPN still secure in 2024?
Yes, OpenVPN remains secure when properly configured with strong encryption (AES-256, SHA256) and up-to-date versions. It has undergone extensive audits and is widely trusted.
Can I use OpenVPN on a Raspberry Pi?
Yes, OpenVPN runs well on Raspberry Pi OS (32-bit or 64-bit). Performance is adequate for light to moderate use; consider WireGuard for higher throughput on Pi 4 or later.
Do I need a static IP for my VPN server?
Not strictly — you can use a dynamic DNS service to map a changing IP to a hostname, which clients can use in their .ovpn files.
Is it safe to use public Wi-Fi with my own OpenVPN server?
Yes, connecting to your own VPN on public Wi-Fi encrypts your traffic between your device and the server, protecting it from local network eavesdropping.
How many clients can connect simultaneously?
The server configuration supports multiple concurrent connections; the limit is mainly bandwidth and server resources. Each client needs a unique certificate.
Should I use UDP or TCP for OpenVPN?
UDP is preferred for better performance and lower latency. Use TCP only if UDP is blocked (e.g., by strict firewalls) and you need to tunnel over port 443 to mimic HTTPS traffic.