
OpenVPN is an open-source, full-featured VPN solution that enables secure site-to-site and point-to-point connections. OpenVPN creates encrypted tunnels using TLS (Transport Layer Security) to secure data transmission over untrusted networks such as the Internet between clients and servers. It supports multiple encryption algorithms, including AES-256, to encrypt traffic and protect network communication from man-in-the-middle and eavesdropping attacks.
This article explains how to install OpenVPN on Ubuntu 24.04 and configure it to create secure end-to-end encrypted connections between the VPN server and client devices.
Before you begin, you need to:
OpenVPN is available in the default package repositories on Ubuntu 24.04. Follow the steps below to update the APT package manager index and install OpenVPN on your server.
Update the APT package index.
Install OpenVPN.
Verify the installed OpenVPN version.
Your output should be similar to the one below.
OpenVPN 2.6.12 is installed based on the above output.
OpenVPN requires a server certificate, private key, and encryption files signed by a trusted certificate authority (CA) to enable VPN tunnel connections. Easy-RSA is a certificate authority management tool for applications like OpenVPN that issue digital certificates, including server certificates and private key pairs. Follow the steps below to install Easy-RSA, create the public key infrastructure (PKI), build a certificate authority, and generate a new server certificate and private key.
Install Easy-RSA.
Navigate to your user's home directory.
Create a new easy-rsa directory.
Link the /usr/share/easy-rsa directory to easy-rsa to access the Easy-RSA script and package files.
List the easy-rsa directory and verify the linked files.
Output:
Change to the easy-rsa directory.
Create a new vars configuration using a text editor such as nano.
Add the following certificate authority configurations to the file.
Save and close the file.
The above configuration specifies the organizational information for building your CA, including the country, city, administrative email, and unit details.
Initialize the PKI using the easy-rsa script.
Output:
Build the CA to generate the root public certificate and private key pair.
Enter a strong passphrase for signing certificate requests and repeat it when prompted to secure the CA.
Enter a common name for the CA, such as OpenVPN-CA.
Verify the generated ca.crt CA certificate path.
Generate a new server certificate request. Replace vpnserver with your desired server common name.
Press Enter when prompted to verify the common name.
Verify the generated public certificate request and private key paths when successful.
Sign the server certificate request using the CA.
Enter yes and press Enter when prompted to verify the certificate request.
Enter your CA passphrase when prompted to sign the server certificate request.
Your output should look like the one below when successful.
List the pki/issued directory to verify the generated server certificate.
Output:
Generate a ta.key HMAC signature file to enable TLS verification and authentication on the OpenVPN server.
List your working directory files to verify the generated ta.key file.
Output:
Create a strong Diffie-Hellman parameters file to secure key exchange for encrypted OpenVPN sessions.
Output:
List the pki directory to verify the generated dh.pem file.
Output:
Copy the ca.crt, vpnserver.key, vpnserver.crt, ta.key, and dh.pem files to the /etc/openvpn directory.
OpenVPN uses server and client configurations in the /etc/openvpn directory to create tunnel interfaces and the respective systemd services. /etc/openvpn contains the server configurations you can manage with the openvpn@ service, while /etc/openvpn/server contains additional configurations manageable with the openvpn-server@ service. Follow the steps below to create a new OpenVPN server configuration in the /etc/openvpn directory to enable a VPN tunnel interface on your server.
Copy the sample /usr/share/doc/openvpn/examples/sample-config-files/server.conf OpenVPN server configuration template to the /etc/openvpn directory.
Navigate to the /etc/openvpn directory.
Open the copied server.conf file.
Optional: Remove ; to uncomment the local directive and replace a.b.c.d with the server IP address OpenVPN should use to listen for incoming connections. Replace 192.0.2.100 with your server's actual public IP address.
Find the dev directive and verify the default OpenVPN tunnel type.
tun creates routed IP tunnels, while tap creates Ethernet tunnels.
Find the ca, cert, and key options, then replace the default ca.crt,server.crt, and server.key values with the actual paths to your certificate authority, server certificate, and server private key files.
Within the above configuration:
ca: Specifies the certificate authority (CA) path.cert: Specifies the server's public certificate path.key: Specifies the private key used to verify the server's public certificate and establish secure VPN connections.Find the dh directive and replace dh2048.pem with your actual Diffie-Hellman file path.
Find the data-ciphers directive, remove ; to uncomment it to enable OpenVPN to use strong, modern ciphers for encryption, then add data-ciphers-fallback AES-256-CBC as a fallback.
Add the following auth directive below data-ciphers to specify the HMAC digest algorithm, such as SHA512, for authenticating each packet.
Find the server directive and specify the VPN subnet to assign client addresses. For example, change the default 10.8.0.0 subnet to 10.10.10.0.
Find the ;push "redirect-gateway def1 bypass-dhcp" directive and remove ; to uncomment it, redirecting all traffic through the VPN.
Find the dhcp-option directives and replace the default addresses with your preferred DNS servers, such as 8.8.8.8 and 1.1.1.1, to enable faster name resolution, then remove ; to uncomment the options.
Find the tls-auth directive, uncomment it, replace ta.key with your actual key path, and keep 0 as the direction.
Find the user and group pair, replace openvpn with nobody and nogroup respectively to run OpenVPN with reduced privileges, then remove ; to uncomment the options.
Save and close the server.conf file.
Your modified server.conf file should look like the one below.
Test the OpenVPN server configuration and verify it runs without errors.
Your output should be similar to the one below when the configuration test is successful.
Follow the steps below to enable IP forwarding on the server, allowing OpenVPN clients to access the Internet through the VPN.
Open the /etc/sysctl.conf file to enable IP forwarding on the server.
Find the # net.ipv4.ip_forward=1 directive and remove # to uncomment it.
Save and close the file.
The above net.ipv4.ip_forward=1 configuration enables IP forwarding, allowing OpenVPN clients to route traffic through the VPN.
Apply the /etc/sysctl.conf configuration changes.
Output:
Run the following command to verify the public network interface on your server.
Note the public interface name like enp1s0 in your output, similar to the one below.
Check the UFW status and verify that it's installed.
Run the following command to install UFW and allow SSH traffic if it's not installed.
Open the /etc/ufw/before.rules file to enable NAT through the firewall.
Add the following POSTROUTING policy configuration before the *filter section. Replace enp1s0 with your actual public interface name.
Save and close the file.
The above firewall configuration modifies the default POSTROUTING policy in the nat table to masquerade all traffic from the 10.10.10.0/24 VPN subnet through the server's enp1s0 public network interface.
Open the /etc/ufw/sysctl.conf file to enable IP forwarding through UFW.
Find the #net/ipv4/ip_forward=1 directive and remove # to uncomment it.
Save and close the file.
The above net/ipv4/ip_forward=1 configuration enables IP forwarding through the firewall to route packets between the OpenVPN tun interface and other interfaces on the server.
Open the /etc/default/ufw file to allow forwarded packets through UFW.
Find the DEFAULT_FORWARD_POLICY directive and change the default value from DROP to ACCEPT.
Save and close the file.
The above configuration enables UFW to allow forwarded packets through the firewall.
Reload UFW to apply the firewall configuration changes.
Follow the steps below to allow connections to the OpenVPN server through the default firewall configuration.
Allow incoming connections to the tun0 OpenVPN interface.
Allow outgoing connections from the tun0 interface.
Allow network connections to the 1194 OpenVPN server port.
Reload UFW to apply the firewall configuration changes.
Check the UFW status to verify the active firewall rules.
Output:
OpenVPN uses systemd to manage the VPN interfaces based on the server configurations in the /etc/openvpn directory. Follow the steps below to manage the OpenVPN service based on your server configuration and verify the VPN tunnel interface details.
Enable the OpenVPN service to start automatically at boot.
Output:
Start the OpenVPN service.
View the OpenVPN service status and verify that it runs without errors.
Output:
Verify that the tun0 OpenVPN interface is active and correctly configured on the server.
Output:
OpenVPN requires a valid client certificate and private key pair to connect to the VPN server. Follow the steps below to create a new client certificate and private key to use in your VPN client configuration.
Create a new keys directory in /etc/openvpn/client to store the client encryption keys.
Navigate to the easy-rsa directory.
Generate a new certificate request using the easyrsa script. Replace vpnclient1 with your desired client name.
Press Enter when prompted to verify your client's common name.
Verify the generated private key and public certificate request paths in your output similar to the one below.
Import the signing request to generate a new client certificate.
Enter yes and press Enter when prompted to verify the client's common name to sign the certificate.
Enter your CA passphrase to sign the certificate.
Verify the generated client certificate path in your output similar to the one below.
Copy the vpnclient1.crt client certificate to the /etc/openvpn/client/keys directory.
Move the vpnclient1.key private key to the /etc/openvpn/client/keys directory.
Follow the steps below to create a new client configuration to connect to the OpenVPN server.
Copy the sample /usr/share/doc/openvpn/examples/sample-config-files/client.conf OpenVPN client configuration to /etc/openvpn/client.
Navigate to the /etc/openvpn/client directory.
Rename the client.conf configuration to vpnclient1.ovpn.
Choose the OpenVPN client configuration format based on:
ovpn: For client configurations compatible with GUI OpenVPN clients for Windows, macOS, Android, or IOS devices.conf: For client configurations compatible with scripts and CLI tools like systemd. Depending on your configuration, you can start the client configuration using a systemd service like openvpn-client@vpnclient.service.Open the vpnclient1.ovpn configuration.
Verify the following directives:
client: OpenVPN configuration type.proto udp: Client protocol. Value should match the server's configuration.Find the remote directive and replace my-server-1 1194 with your OpenVPN server's IP address and port. Replace 192.0.2.100 with your actual server IP.
Find the ca, cert, and key options, then add # before each option to comment them.
Find the data-ciphers directive, remove ; to uncomment it, and verify that the cryptographic values match the OpenVPN server configuration.
Add the following auth directive on a new line to match the OpenVPN server configuration.
Save and close the vpnclient1.ovpn file.
Your modified vpnclient1.ovpn file should look like the one below.
Run the following command to append values from the ca.crt, vpnclient.crt, vpnclient.key, and ta.key files to vpnclient1.ovpn.
View the vpnclient1.ovpn file and verify your configuration's appended ca, certificate,private key, and tls values.
Copy the vpnclient1.ovpn configuration to your user's home directory.
Follow the steps below to test the VPN server using the OpenVPN Connect application for GUI devices.
Download and install OpenVPN Connect for your device from the official website. For example, OpenVPN Connect for Windows.
Open a new terminal session on your local workstation.
Change the working directory to your user's home directory.
Connect to the OpenVPN server using SFTP. Replace linuxuser with your actual user.
List the directory files and verify that the vpnclient1.ovpn client configuration is available.
Download the vpnclient1.ovpn file to your local workstation.
Output:
Launch OpenVPN Connect from your applications menu.
Click UPLOAD FILE on the Get Connected page.
Click Browse to find and open the downloaded vpnclient1.ovpn client configuration in your filesystem.
Verify that the Server Hostname matches your OpenVPN IP and click Connect.
Verify that your OpenVPN profile changes to Connected and monitor the connection statistics.
Run the following command in your terminal and verify that the VPN server IP is your OpenVPN server's active public IP address.
Test the connection to any network attached to the VPN server or the Internet using a domain like google.com to verify that it is successful.
Output:
OpenVPN may return connection errors depending on your server and client configurations. Follow the steps below to troubleshoot common OpenVPN errors.
You have installed OpenVPN on Ubuntu 24.04, configured the server, and connected a VPN client. You can use OpenVPN to connect multiple clients using different VPN tunnels and server configurations with unique port numbers. For more information, visit the OpenVPN documentation.
0 Comments
Be the first to comment and share your perspective with the community.