How to Install and Configure OpenVPN on Ubuntu

Learn how to install and configure OpenVPN on Ubuntu with this step-by-step guide.

Updated: 09 Mar, 25 by jean lou 9 Min

List of content you will read in this article:

VPN networks are established to maintain a secured private connection. OpenVPN, an open-source VPN solution, provides excellent security measures to guarantee that your online activities are hidden from unwanted eyes. 

This will give you a clear understanding of how to install and configure OpenVPN on your Ubuntu system, irrespective of whether you need a VPN for personal use or work-from-home setups. It will culminate in having a fully functional VPN that protects your online security and privacy.

OpenVPN is one of the most widely used open-source VPN (Virtual Private Network) solutions for secured communication and encrypted data transmission over the internet today. It provides a private tunnel for information between your device and a remote server. Thus, the information stays completely safe behind the prying eyes of hackers, ISPs, and other third parties

Strong encryption and customizable configurations make OpenVPN an extremely reliable solution for anyone trying to safeguard their online privacy and security. 

It is a great choice for secure and well-configured access to sensitive data, bypassing geographical restrictions, or securing your remote workplace to prove that OpenVPN has everything. Inside this single post, one is given the surefire way of setting up OpenVPN on Windows-based systems.

  • Strong Encryption: OpenVPN uses AES-256 encryption and other advanced security protocols to protect your data from cyber threats.
  • Cross-Platform Compatibility: It works on various operating systems, including Windows, macOS, Linux, Android, and iOS, ensuring seamless connectivity across devices.
  • Bypass Geo-Restrictions: OpenVPN allows users to access region-locked content by masking their IP addresses and routing traffic through secure servers.
  • Secure Remote Access: Businesses can use OpenVPN to provide employees with secure access to internal networks from remote locations.
  • Open-Source & Customizable: Since OpenVPN is open-source, users can audit the code, customize configurations, and integrate it with other security tools as needed.
  • Reliable Performance: OpenVPN supports UDP and TCP protocols, ensuring a stable and efficient VPN connection based on user preferences.

If you want to learn about the differences between VPN and VPS, read the article below:

VPN vs VPS

Setting up OpenVPN on Ubuntu allows you to establish a secure and encrypted connection to a virtual private network. Follow this step-by-step guide to install and configure OpenVPN on your Ubuntu system.

Update Your System

Before installing OpenVPN, ensure your system is up to date by running the following commands:

sudo apt update && sudo apt upgrade -y

Updating system before installing OpenVPN on Ubuntu

This updates the package list and installs the latest security patches.

Install OpenVPN and Easy-RSA

To install OpenVPN and the Easy-RSA package (used for managing security certificates), run:

sudo apt install openvpn easy-rsa -y

Install OpenVPN and Easy-RSA

Set Up the Certificate Authority (CA)

Create a directory for the CA:

make-cadir ~/openvpn-ca

cd ~/openvpn-ca

Edit the vars file using a text editor like Nano:

nano vars

Modify the following lines to match your organization’s details:

export KEY_COUNTRY="US"

export KEY_PROVINCE="CA"

export KEY_CITY="SanFrancisco"

export KEY_ORG="MONOVM"

export KEY_EMAIL="admin@MONOVM.com"

export KEY_OU="MyOrganizationalUnit"

Save the file and apply the variables:

source vars

Clean any existing certificates (if any) and build the CA:

./clean-all

./build-ca

Build Certificate Authority

Generate Server Certificates and Keys

Create the server key and certificate:

./build-key-server server

Press Enter through the prompts and confirm with "yes" when asked to sign the certificate.

Generate Diffie-Hellman parameters for encryption:

./build-dh

Generate the HMAC key to add a security layer:

openvpn --genkey --secret keys/ta.key

Configure the OpenVPN Server

Copy the sample configuration file:

sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/

cd /etc/openvpn/

sudo gunzip server.conf.gz

Edit the OpenVPN server configuration:

sudo nano server.conf

Find and modify the following lines:

ca ca.crt

cert server.crt

key server.key

dh dh.pem

tls-auth ta.key 0

Save the file and exit.

Enable IP Forwarding

Open the sysctl configuration file:

sudo nano /etc/sysctl.conf

Uncomment the following line:

net.ipv4.ip_forward=1

Apply the changes:

sudo sysctl -p

Start and Enable OpenVPN Service

Start the OpenVPN service and enable it to run on boot:

sudo systemctl start openvpn@server

sudo systemctl enable openvpn@server

Start and Enable OpenVPN Service

Check the status to ensure it’s running correctly:

sudo systemctl status openvpn@server

Generate Client Certificates and Keys

Navigate back to the CA directory:

cd ~/openvpn-ca

Build a client certificate:

./build-key client1

Press Enter through the prompts and confirm with "yes" when asked to sign the certificate.

Copy the necessary client files to /etc/openvpn/client-configs:

mkdir -p ~/client-configs/keys

cp ~/openvpn-ca/keys/{ca.crt,client1.crt,client1.key,ta.key} ~/client-configs/keys/

Configure the OpenVPN Client

Copy the client configuration template:

cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/client-configs/base.conf

Edit the configuration file:

nano ~/client-configs/base.conf

Update the remote directive with your server’s IP address:

remote your-server-ip 1194

Ensure the following settings are included:

ca ca.crt

cert client1.crt

key client1.key

tls-auth ta.key 1

Save and exit.

Connect to the OpenVPN Server

Transfer the client configuration files (client.ovpn) to your local machine, then start the VPN connection using:

sudo openvpn --config client.ovpn

If using a GUI-based client (like OpenVPN Connect), import the .ovpn file and connect.

If you want to learn how to install Wireguard VPN on Ubuntu, read the article below:
Wireguard VPN on Ubuntu

While configuring OpenVPN on Ubuntu, you may face more than one obnoxious issue that might arise from the connectivity settings, configuration, as well as firewall settings. Most common OpenVPN problems include not starting, authentication errors, not resolving DNS, or misconfigured routing. 

Most of these problems usually occur because of an incorrect server configuration, firewalls, or missing certificates. Below are some of the most common OpenVPN problems and how to solve them.

  • OpenVPN Service Not Starting
    Check the service status using:

sudo systemctl status openvpn@server

If the service fails, check logs for errors:

sudo journalctl -u openvpn@server --no-pager

Fix missing or incorrect configurations in /etc/openvpn/server.conf.

  • Client Unable to Connect to Server
    Verify that the OpenVPN port (default: 1194) is open:

sudo ufw allow 1194/udp

Ensure the server is running and reachable by testing with:

ping your-server-ip

  • Authentication Failures (Invalid Credentials or Certificate Errors)
    Ensure that the client certificate and key match the server’s CA:

ls ~/client-configs/keys/

Regenerate client keys if needed using:

./build-key client1

  • No Internet Access After Connecting to VPN
    Ensure IP forwarding is enabled:

sudo sysctl net.ipv4.ip_forward

If disabled, enable it in /etc/sysctl.conf and reload settings:

sudo sysctl -p

  • DNS Resolution Issues
    Manually set a DNS server in the OpenVPN configuration by adding:

push "dhcp-option DNS 8.8.8.8"

push "dhcp-option DNS 1.1.1.1"

Restart OpenVPN for changes to take effect:

sudo systemctl restart openvpn@server

  • Firewall Blocking VPN Traffic
    Check firewall rules using:

sudo ufw status

Allow OpenVPN traffic if missing:

sudo ufw allow 1194/udp

sudo ufw allow OpenSSH

sudo ufw enable                                              

Conclusion

It conveys online security, privacy, and remote reaching through an OpenVPN installation on Ubuntu. The installation of step-by-step execution with necessary firewall and network settings and resolving common related problems ensures a stable and reliable run of the VPN connection. 

Be it remote work security, getting past geo-surveillance, or protecting private data, OpenVPN can do the work well. Proper setup and maintenance can provide a seamless yet private experience online.

Run sudo systemctl status openvpn@server to check the service status.

OpenVPN uses UDP port 1194 by default, but it can be changed in the configuration.

Use sudo systemctl restart openvpn@server to restart the OpenVPN server.

Yes, but each device requires a separate client certificate and key for authentication.

Check network stability, and firewall settings, and switch between UDP and TCP modes in OpenVPN.