How to Install OpenVPN and Configure a Secure Server on Ubuntu 22.04 LTS

Securing remote access to your infrastructure is a critical component of maintaining a robust and protected network. OpenVPN is a powerful, open-source VPN solution that helps encrypt and secure data transmission between clients and servers. By setting up OpenVPN on Ubuntu 22.04 LTS, DevOps engineers can safeguard network communications, prevent unauthorized access, and ensure data confidentiality. In this guide, we’ll walk you through the process of installing OpenVPN and configuring a secure server for seamless, protected connections.

Prerequisites

Before you begin, make sure you have:

  • Administrative access to the server
  • Basic knowledge of Linux command-line operations, networking, and package management

Step-by-Step Guide

Step 1: Update the System and Install Dependencies

Start by updating the system package list and installing essential dependencies:

sudo apt update && sudo apt install -y openvpn easy-rsa

These packages will provide the tools needed for OpenVPN and certificate management.

Step 2: Set Up the Public Key Infrastructure (PKI)

Create a directory for the PKI and initialize it:

make-cadir ~/openvpn-ca
cd ~/openvpn-ca

Edit the vars file to set up certificate details:

nano vars

Modify the vars file with your organization’s information:

export KEY_COUNTRY="US"
export KEY_PROVINCE="CA"
export KEY_CITY="San Francisco"
export KEY_ORG="MyOrg"
export KEY_EMAIL="[email protected]"
export KEY_OU="MyUnit"
export KEY_NAME="server"

Load the variables and clean any pre-existing keys:

source vars
./clean-all

Generate the Certificate Authority (CA):

./build-ca

Step 3: Create the Server Certificate, Key, and Encryption Files

Build the server certificate and key:

./build-key-server server

Generate the Diffie-Hellman parameters:

./build-dh

Create the HMAC signature for TLS authentication:

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

Copy the server certificates and keys to the OpenVPN directory:

sudo cp keys/{server.crt,server.key,ca.crt,dh2048.pem,ta.key} /etc/openvpn/

Step 4: Configure the OpenVPN Server

Create a new configuration file for the server:

sudo nano /etc/openvpn/server.conf

Add the following configuration:

port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh2048.pem
auth SHA256
tls-auth ta.key 0
cipher AES-256-CBC
persist-key
persist-tun
user nobody
group nogroup
status openvpn-status.log
verb 3

This configuration sets up OpenVPN to use UDP, implements AES-256-CBC encryption, and enables TLS authentication for added security.

Step 5: Adjust Server Networking

Enable IP forwarding by modifying the sysctl.conf file:

sudo nano /etc/sysctl.conf

Uncomment or add:

net.ipv4.ip_forward=1

Apply the changes:

sudo sysctl -p

Add firewall rules to allow traffic through the VPN:

sudo ufw allow 1194/udp
sudo ufw allow OpenSSH

Enable and reload UFW:

sudo ufw enable
sudo ufw reload

Step 6: Start and Enable the OpenVPN Service

Start OpenVPN and enable it to run on boot:

sudo systemctl start openvpn@server
sudo systemctl enable openvpn@server

Check the status to confirm it’s running:

sudo systemctl status openvpn@server

Best Practices for Securing Your VPN

  • Use strong encryption: Ensure AES-256 is used for secure data encryption.
  • Regularly rotate certificates: Update certificates periodically to prevent potential compromises.
  • Monitor VPN logs: Regularly check /var/log/syslog and other logs for suspicious activity.
  • Configure user access: Use role-based access control (RBAC) to manage which users have VPN access.

Troubleshooting Tips

  • OpenVPN service not starting: Check the configuration files for errors using sudo journalctl -xe.
  • Connectivity issues: Ensure that port 1194 is open and that UFW rules are properly configured.
  • Certificate validation errors: Verify that the CA, server certificates, and keys are correctly generated and stored in /etc/openvpn/.

Conclusion

In this guide, we covered the process of installing and configuring OpenVPN on Ubuntu 22.04 LTS. This setup helps establish secure, encrypted communication between your server and connected clients, protecting your network from unauthorized access and maintaining data privacy. By following best practices and regularly updating configurations, you can ensure your VPN server remains secure and reliable.

Next Steps

  • Integrate OpenVPN into containerization platforms such as Docker for more flexibility.
  • Automate VPN setup and management using infrastructure-as-code (IaC) tools like Ansible or Terraform.
  • Scale your VPN solution to handle more clients or additional sites for distributed teams.

With OpenVPN configured on your Ubuntu 22.04 LTS server, you’re now well-equipped to protect your network and ensure secure data transmission.