Install and Configure OpenVPN Server and Client on Linux

How to install, and configure OpenVPN Server and Client on Linux.


Table of Contents

  • Introduction
  • Lab Setup
  • Implementation
  • Reference to read more

Introduction

What is VPN?

  • A VPN (Virtual Private Network) creates an encrypted connection over a public network, such as the internet. It allows devices to communicate securely through a VPN server.
  • A VPN routes your network traffic through a remote server, so websites see the VPN server’s public IP address instead of your original public IP address.

Why do we need a VPN?

When using public Wi-Fi, your network traffic may be exposed to other users on the same network, especially when using insecure connections.

For example, attackers may attempt to intercept unencrypted data, such as:

  • Login credentials
  • Personal information
  • Websites you visit

However, when using a VPN, your traffic is encrypted between your device and the VPN server. This makes it more difficult for other users on the same public network to intercept and read your data.

The main purpose of a VPN is to ensure the secure transmission of data across public networks.
Your data is encrypted before passing through the public internet and decrypted when it reaches the VPN endpoint.

Lab Setup

Download the editable here → Click here to download

For this lab, I will use two Linux machines:

  • Server: Hosts the OpenVPN server and accepts incoming VPN connections.
  • Client: Connects to the OpenVPN server to establish an encrypted VPN tunnel.

Once the VPN connection is established, both machines can securely exchange data through the VPN tunnel. Data is encrypted before transmission and decrypted at the receiving endpoint.

Server:

  • Public IP: 54.169.44.147
  • Private IP: 172.26.1.8
  • Outbound Security Group: Allow all outbound traffic.
  • Inbound Security Group: Allow the required VPN connection traffic (UDP 1194).

Client:

  • Public IP: 47.129.8.130
  • Private IP: 172.26.15.106

Note:

In this lab environment, I use AWS Lightsail, so the client connects to the OpenVPN server using its public IP address.

In a production environment, the VPN server generally needs a publicly reachable endpoint for remote clients. However, communication between the VPN server and internal corporate resources should use private IP addresses whenever possible.

Implementation

  1. Install OpenVPN on the Server
    • First, check whether OpenVPN is already installed.
      rpm -qa | grep openvpn
    • If it is not installed, install the EPEL repository.
      dnf install -y epel-release
    • Install OpenVPN
      dnf install -y openvpn
    • Verify the installation
      openvpn --version
  2. Install and Configure Easy-RSA

    Easy-RSA is a tool used to create and manage the Public Key Infrastructure (PKI), including the Certificate Authority (CA), private keys, certificate requests, and certificates.

    • Install Easy-RSA:
      dnf install -y easy-rsa
    • Check the available Easy-RSA versions:
      ls /usr/share/easy-rsa/
    • Copy the required version into the OpenVPN directory. Adjust 3.2.1 if you installed version different:
      >mkdir -p /etc/openvpn/easy-rsa
      
      cp -rv /usr/share/easy-rsa/3.2.1/* /etc/openvpn/easy-rsa/
    • Go to the Easy-RSA directory:
      cd /etc/openvpn/easy-rsa

      Important Note:

      Keys and certificates are essential for establishing a trusted and secure VPN connection.

      • CA certificate (ca.crt): Identifies the trusted Certificate Authority used to verify certificates.
      • Private key (.key): Proves ownership of a certificate and must remain secret.
      • Certificate (.crt): Identifies the server or client and is signed by the CA.
      • DH parameters (dh.pem): Support Diffie-Hellman key exchange for establishing shared secrets.

      These components help OpenVPN authenticate both parties and establish an encrypted connection.

    Initialize the PKI

    ./easyrsa init-pki

    This creates the pki directory, which stores the certificates, private keys, and other PKI information.

    Create the Certificate Authority

    For a lab environment without a CA password:

    ./easyrsa build-ca nopass

    This generates:

    • pki/ca.crt – CA public certificate.
    • pki/private/ca.key – CA private key.

    The CA acts as a trusted authority that signs and verifies server and client certificates.

    Security note: For production, protect the CA private key with a strong passphrase and preferably keep it on a separate, offline machine. Never distribute ca.key to clients.

    Generate the Server Certificate and Private Key

    ./easyrsa gen-req server nopass

    This generates:

    • pki/private/server.key – Server private key.
    • pki/reqs/server.req – Certificate signing request (CSR).

    The CSR contains the information needed by the CA to issue a server certificate.

    Sign the Server Certificate

    ./easyrsa sign-req server server

    Confirm the signing request when prompted.

    This generates the signed server certificate: pki/issued/server.crt

    The CA signs the certificate to establish trust in the server’s identity.

    Generate Diffie-Hellman Parameters

    ./easyrsa gen-dh

    This generates: pki/dh.pem

    DH parameters support the Diffie-Hellman Key exchange mechanism.

    Generate the Client Certificate and Private Key

    For this example, we will name the client client

    ./easyrsa gen-req client nopass

    This generates the client’s private key and certificate signing request.

    Sign the client certificate:

    ./easyrsa sign-req client client

    This generates: pki/issued/client.crt

    You can use different client names, such as client1, client2, or user1. Each client should have its own certificate and private key.

    Generate an Additional TLS Authentication Key

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

    The ta.key file provides an additional layer of protection for the TLS control channel by authenticating incoming packets and helping protect against certain attacks.

    Both the server and client need a copy of this key.

    Copy the Required Server Files

    Create a directory for the server certificates and keys:

    mkdir -p /etc/openvpn/server

    Copy the required file:

    cp pki/ca.crt /etc/openvpn/server/
    cp pki/issued/server.crt /etc/openvpn/server/
    cp pki/private/server.key /etc/openvpn/server/
    cp pki/dh.pem /etc/openvpn/server/
    cp /etc/openvpn/ta.key /etc/openvpn/server/
  3. Configure the OpenVPN Server
    • Copy the Sample Configuration

      First, locate the available sample configuration files:

      find /usr/share/doc/openvpn/ -name server.conf

      Copy the sample configuration into the server directory:

      cp /usr/share/doc/openvpn/sample/sample-config-files/server.conf \
      /etc/openvpn/server/server.conf

      The sample configuration provides a starting point with common OpenVPN server settings.

    • Edit the Server Configuration
      vi /etc/openvpn/server/server.conf

      Find and update the certificate and key paths:

      port 1194
      proto udp
      dev tun
      
      ca /etc/openvpn/server/ca.crt
      cert /etc/openvpn/server/server.crt
      key /etc/openvpn/server/server.key
      dh /etc/openvpn/server/dh.pem
      
      tls-auth /etc/openvpn/server/ta.key 0

      Make sure the paths match where you copied your files.

      The important directives are:

      Directive Purpose
      port 1194 OpenVPN listening port
      proto udp Uses UDP protocol
      dev tun Creates a routed VPN interface
      ca Specifies the CA certificate
      cert Specifies the server certificate
      key Specifies the server private key
      dh Specifies DH parameters
      tls-auth Enables additional TLS control-channel authentication
  4. Enable IP Forwarding

    IP forwarding allows the Linux server to forward packets between network interfaces.

    This is important when the VPN server needs to route client traffic to another network or the internet.

    Back up the configuration:

    cp -v /etc/sysctl.conf /etc/sysctl.conf.bak

    Edit the file

    vi /etc/sysctl.conf

    Add:

    net.ipv4.ip_forward = 1

    Apply the changes:

    sysctl -p

    Verify:

    sysctl net.ipv4.ip_forward
  5. Set File Permissions

    Protect the private kyes and configuration files.

    chmod 600 /etc/openvpn/server/server.key
    chmod 600 /etc/openvpn/server/ta.key
    chmod 600 /etc/openvpn/server/server.conf

    The private keys should only be accessible to authorized users, especially root and the OpenVPN service.

  6. Start the OpenVPN Server
    • Enable and start the OpenVPN server instance
      systemctl enable --now openvpn-server@server
    • Check the status
      systemctl status openvpn-server@server
    • Check the VPN interface
      ip addr show tun0

      OpenVPN creates a virtual network interface called tun0.

      This interface handles routed VPN traffic between the server and connected clients.

  7. Configure the OpenVPN Client

    Now move to the client machine.

    Install OpenVPN

    • Check whether OpenVPN is installed:
      rpm -qa | grep openvpn
    • Install the required packages:
      dnf install -y epel-release
      dnf install -y openvpn

    Create the Client Configuration Directory

    mkdir -p /etc/openvpn/client

    Copy the Required Certificates and keys

    • From the client machine, retrieve the required files from the server using SCP.
      1. Generate SSH key pair on the client

        Run this on the client machine:

        ssh-keygen -t ed25519 -f ~/.ssh/openvpn_server_key

        It generates two files:

        File Purpose
        openvpn_server_key Private key (keep on client)
        openvpn_server_key.pub Public key (copy to server)

        You can press Enter to leave the passphrase empty, or set one for additional protection.

      2. Copy the public key to the OpenVPN server

        In the /root/.ssh/authorized_keys

      Retrieve the required files from the server using SCP.

      scp -i ~/.ssh/openvpn_server_key root@54.169.44.147:/etc/openvpn/server/ca.crt /etc/openvpn/client/
      scp -i ~/.ssh/openvpn_server_key root@54.169.44.147:/etc/openvpn/easy-rsa/pki/issued/client.crt /etc/openvpn/client/
      scp -i ~/.ssh/openvpn_server_key root@54.169.44.147:/etc/openvpn/easy-rsa/pki/private/client.key /etc/openvpn/client/
      scp -i ~/.ssh/openvpn_server_key root@54.169.44.147:/etc/openvpn/ta.key /etc/openvpn/client/
      

      54.169.44.147 : is SERVER_IP.

      However, the second command assumes you have kept the PKI under /etc/openvpn/easy-rsa/pki/.

      Verify the files

      ls -la /etc/openvpn/client/

      Security note: Only transfer the client certificate and its corresponding private key. Never transfer ca.key or server.key to the client.

    Copy the Sample Client Configuration

    • Locate the sample configuration
      find /usr/share/doc/openvpn/ -name client.conf
    • Copy it
      cp /usr/share/doc/openvpn/sample/sample-config-files/client.conf \
      /etc/openvpn/client/client.conf

    Edit the Client Configuration

    vi /etc/openvpn/client/client.conf

    Configure the connection details:

    client
    dev tun
    proto udp
    
    remote 54.169.44.147 1194
    
    ca /etc/openvpn/client/ca.crt
    cert /etc/openvpn/client/client.crt
    key /etc/openvpn/client/client.key
    
    tls-auth /etc/openvpn/client/ta.key 1
    
    remote-cert-tls server

    Replace 54.169.44.147 with your actual OpenVPN server public IP address or DNS name.

    Notice the difference:

    • Server uses tls-auth ... 0.
    • Client uses tls-auth ... 1.

    These values identify the direction of the TLS authentication key.

    Keep the other appropriate client directives from the sample configuration.

    Set Client File Permissions

    chmod 600 /etc/openvpn/client/client.key
    chmod 600 /etc/openvpn/client/ta.key
    chmod 600 /etc/openvpn/client/client.conf
  8. Connect the Client to the VPN Server
    • Start the VPN connection manually:
      openvpn --config /etc/openvpn/client/client.conf

      This starts OpenVPN in the foreground and displays connection logs in the terminal.

      If you press Ctrl + C.

      • OpenVPN stops.
      • The VPN connection is disconnected.
      • The tun0 interface is removed (assuming no other OpenVPN process uses it).
      • Your client no longer sends traffic through that VPN tunnel.
    • Check the VPN interface
      ip addr show tun0
    • Ctr + C on the terminal that running OpenVPN in the foreground then run ip addr show tun0 to check the VPN interface again
    • For persistent client connections, you can use: (OpenVPN running in the background)
      systemctl enable --now openvpn-client@client
  9. Verify the VPN Connection

    From the, client ping the VPN server’s tunnel IP:

    ping 10.8.0.1

Reference to read more