What are the steps to set up a secure VPN server using OpenVPN on Ubuntu?

12 June 2024

Setting up a secure VPN server can seem daunting, but with OpenVPN on Ubuntu, the process becomes manageable. This article guides you through each step, ensuring your VPN server is up and running smoothly, providing secure and encrypted connections for your network.

Virtual Private Networks (VPNs) are essential for creating secure connections over public networks. OpenVPN is a robust and open-source solution that offers various encryption methods and security features. By setting up an OpenVPN server on Ubuntu, you can secure your internet traffic and protect sensitive data.

OpenVPN supports different protocols and offers AES-GCM encryption, ensuring your data is encrypted and secure. This guide will walk you through installing OpenVPN, configuring the server and client, and managing the VPN connection.

Installing OpenVPN on Ubuntu

To begin, you must install OpenVPN on your Ubuntu server. This step ensures that you have the software necessary to create and manage your VPN server. Here’s how:

Step 1: Update Your Ubuntu System

Before installing OpenVPN, update your system’s package list and upgrade existing packages.

sudo apt update && sudo apt upgrade -y

Step 2: Install OpenVPN

Now, install OpenVPN and Easy-RSA, a utility for generating SSL keys and certificates.

sudo apt install openvpn easy-rsa -y

Step 3: Set Up Easy-RSA

Easy-RSA helps manage your certificate infrastructure, crucial for a secure VPN setup.

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

Edit the vars file in the openvpn-ca directory to suit your network configuration.

Step 4: Build the Certificate Authority

Initialize the Public Key Infrastructure (PKI) and build the certificate authority (CA).

source vars

This creates a root CA certificate used to sign other certificates.

Configuring the OpenVPN Server

With OpenVPN installed, you can now configure the VPN server. This section will cover generating server keys, creating configuration files, and setting up the server.

Step 1: Generate Server Keys and Certificates

Generate the server key and certificate signing request (CSR).

./build-key-server server

Next, generate Diffie-Hellman parameters, which are necessary for key exchange.


Step 2: Create the Server Configuration File

Copy a sample OpenVPN configuration file and edit it to match your settings.

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

Modify the server.conf file to include your CA, server certificate, server key, and Diffie-Hellman parameters.

ca ca.crt
cert server.crt
key server.key
dh dh2048.pem

Step 3: Enable IP Forwarding and Adjust Firewall Settings

Enable IP forwarding to allow traffic through the VPN.

echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

Adjust the UFW firewall to allow OpenVPN traffic.

sudo ufw allow 1194/udp
sudo ufw allow OpenSSH
sudo ufw enable

Creating and Importing Client Configurations

For clients to connect to the VPN, they must have a unique configuration file. This section details the steps to create and import these files.

Step 1: Build Client Keys and Certificates

Generate a client key and CSR for each client.

./build-key client1

Step 2: Create Client Configuration Files

Create a directory to store client configuration files.

mkdir -p ~/client-configs/files
chmod 700 ~/client-configs/files

Copy and edit a sample client configuration file.

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

Edit the base.conf file with the server’s public IP address and port.

Step 3: Generate .ovpn Files for Clients

Combine the client key, certificate, and configuration into a single .ovpn file.

cd ~/client-configs
./make_config.sh client1

Step 4: Import Configuration Files to Clients

Transfer the .ovpn files to your client devices securely. Import the files using the network manager or OpenVPN client of your choice.

Managing and Testing Your VPN Connection

After setting up the server and client configurations, it is crucial to manage and test your VPN connection.

Step 1: Start the OpenVPN Server

Start the OpenVPN server service.

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

Step 2: Testing the VPN Connection

Connect using one of your client devices and import the .ovpn file. Initiate the connection and verify that traffic is routed through the VPN.

Step 3: Check Server Logs

Monitor the server logs to ensure the VPN operates correctly.

sudo journalctl -u openvpn@server

By following the steps outlined in this guide, you have successfully set up a secure OpenVPN server on Ubuntu. You can now provide encrypted, secure connections for your network, protecting your data from potential threats. This setup not only secures your internet traffic but also offers flexibility in managing and connecting multiple clients. With OpenVPN, you achieve a robust, reliable, and secure VPN solution.

Setting up a VPN server using OpenVPN on Ubuntu involves several steps, from installing the necessary software to configuring the server and clients. Following these detailed instructions ensures a secure and functional VPN, providing peace of mind for users seeking to protect their online activities.

Copyright 2024. All Rights Reserved