OpenVPN
OpenVPN Configuration Guide: From Topology to Tunnel
This guide provides a step-by-step walkthrough for deploying a secure OpenVPN infrastructure using an isolated Certificate Authority (CA) and a dedicated VPN Gateway.
1. Network Topology & Environment Setup
To simulate a real-world enterprise environment, we establish two distinct network zones within VMware:
- The Internet (Public Zone): Where your physical Ubuntu host workstation resides and where the external interface of the VPN server is exposed.
- The LAN (Protected Enterprise Zone): An isolated network housing the internal Certificate Authority (CA) and internal resources.
[ THE INTERNET / HOST PC ]
|
(VMware NAT / Bridged)
|
[ WAN Interface: ens192 ]
[ VM 1 ]
(OpenVPN Server / Router)
[ LAN Interface: ens34 ] -------- Static IP: 10.0.0.1
|
|---------------------- (Virtual LAN Segment - VMnet2)
|
[ LAN Interface: ens160 ] -------- Static IP: 10.0.0.10
[ VM 2 ]
(Certificate Authority Server)
VM 1: VPN Server (AlmaLinux 8)
This virtual machine acts as the gateway between the public internet and the private network. It requires two network interfaces:
- Interface 1 (WAN): Configure as Bridged (preferred for local routing) or NAT. It receives an IP from your physical router DHCP pool (e.g., 192.168.1.X), mapping it as a publicly accessible server.
- Interface 2 (LAN): Configure as Host-Only or assign it to a specific LAN Segment (e.g., VMnet2). This zone uses the 10.0.0.0/24 subnet. The server will use static IP 10.0.0.1.
VM 2: Certificate Authority (AlmaLinux 8)
In production environments, the CA remains entirely air-gapped or hidden deep within the internal LAN. It requires only one network interface:
- Interface 1 (LAN): Configure to use the identical Host-Only or LAN Segment (VMnet2) as VM 1. The machine uses static IP 10.0.0.10, with its Default Gateway pointing directly to the VPN Server (10.0.0.1).
2. Initial Network Configuration
Executing Network Configuration on VM 1 (VPN Server)
Configure the internal static IP address for the LAN interface:
# Configure the internal static IP address for the LAN interface
sudo nmcli connection modify ens192 ipv4.addresses 10.0.0.1/24 ipv4.method manual
sudo nmcli connection up ens192
Executing Network Configuration on VM 2 (CA Server)
Configure the static network configurations and route traffic through the gateway:
# Configure static IP assignment and define the default gateway pointing to the VPN server
sudo nmcli connection modify ens160 ipv4.addresses 10.0.0.10/24 ipv4.gateway 10.0.0.1 ipv4.method manual
# Append the public upstream DNS resolver to the interface profile
sudo nmcli connection modify ens160 ipv4.dns "8.8.8.8"
sudo nmcli connection up ens160
3. Certificate Authority (CA) Initialization (VM 2)
All CA tasks must be carried out inside a highly restricted runtime environment.
# Install the EPEL repository to access community-supported security packages
sudo dnf install epel-release -y
# Install the Easy-RSA cryptographic helper scripts
sudo dnf install easy-rsa -y
# Establish a secured working directory dedicated to the CA infrastructure
mkdir ~/openvpn-ca
chmod 700 ~/openvpn-ca
# Create symbolic links mirroring the system Easy-RSA scripts into the operational directory
ln -s /usr/share/easy-rsa/3/* ~/openvpn-ca/
cd ~/openvpn-ca
# Initialize a clean Public Key Infrastructure (PKI) architecture
./easyrsa init-pki
# Generate the master private key (ca.key) and the root certificate (ca.crt)
./easyrsa build-ca
4. OpenVPN Server Environment Setup (VM 1)
Package and PKI Setup
Initialize a completely independent PKI storage layer strictly to generate local keys and signing requests.
# Install the required software packages for the VPN server node
sudo dnf install epel-release -y
sudo dnf install openvpn easy-rsa -y
# Establish and lock down the local server management workspace
mkdir ~/openvpn-server
chmod 700 ~/openvpn-server
cd ~/openvpn-server
# Initialize the local independent PKI workspace
/usr/share/easy-rsa/3/easyrsa init-pki
- Generating Keypairs and Cryptographic Parameters
Execute keypair generation under your standard user session to keep accurate file permissions.
# Generate the server private key and certificate signing request without a passphrase
/usr/share/easy-rsa/3/easyrsa gen-req server nopass
# Generate the Diffie-Hellman mathematical parameters for secure key exchange
/usr/share/easy-rsa/3/easyrsa gen-dh
# Generate an additional shared secret key for TLS-Crypt packet authentication
openvpn --genkey secret tls-crypt.key
5. Certificate Signing Pipeline
To validate the server credentials, the Certificate Signing Request (server.req) must be copied securely to the CA Server, signed, and sent back.
[ VM 1: VPN Server ] [ VM 2: CA Server ]
│ │
│ ─── scp pki/reqs/server.req ────────────────────> │ (Import & Sign Request)
│ │
│ <── scp pki/issued/server.crt & pki/ca.crt ─────── │ (Return Signed Assets)
- Step 1: Export CSR to CA Server (Execute on VM 1)
# Transfer the local server certificate signing request to the CA server temporary directory
scp pki/reqs/server.req [email protected]:/tmp/
- Step 2: Sign the Request (Execute on VM 2)
cd ~/openvpn-ca
# Import the transferred request file into the master CA database
./easyrsa import-req /tmp/server.req server
# Sign the request using the explicit 'server' role profile template
./easyrsa sign-req server server
Provide the master CA passphrase when prompted to confirm authorization.
- Step 3: Return Validated Assets to Gateway (Execute on VM 2)
# Transfer the newly issued public server certificate back to the gateway server
scp pki/issued/server.crt [email protected]:/tmp/
# Transfer the master public root CA certificate to the gateway server
scp pki/ca.crt [email protected]:/tmp/
- 6. OpenVPN Server Configuration
- Final Asset Placement (Execute on VM 1)
Move all required components into the locked system configuration directories:
# Relocate imported CA and Server certificates into the authoritative system directory
sudo cp /tmp/ca.crt /etc/openvpn/server/
sudo cp /tmp/server.crt /etc/openvpn/server/
# Relocate locally generated keys and cryptographic parameters
sudo cp /home/tomik/openvpn-server/pki/private/server.key /etc/openvpn/server/
sudo cp /home/tomik/openvpn-server/pki/dh.pem /etc/openvpn/server/
sudo cp /home/tomik/openvpn-server/tls-crypt.key /etc/openvpn/server/
- Production Configuration File
Create the main engine configuration file: sudo nano /etc/openvpn/server/server.conf.
port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh.pem
tls-crypt tls-crypt.key
server 10.8.0.0 255.255.255.0
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
keepalive 10 120
cipher AES-256-GCM
auth SHA256
user nobody
group nobody
persist-key
persist-tun
status /var/log/openvpn-status.log
verb 3
Detailed Configuration Breakdown: What We Are Doing and Why
port 1194andproto udp: Instructs the server on which network port and protocol to listen to. UDP is ideal for VPN connections—it is significantly faster than TCP because it does not waste time acknowledging every packet. OpenVPN manages data reliability independently inside the tunnel.dev tun: Creates a virtual network interface of the TUN type (Network Tunnel, Layer 3 of the OSI model). This means the tunnel will transmit raw IP packets in a routed network setup. The alternative isdev tap(Layer 2 - Ethernet frames), which is rarely used as it transmits unnecessary network noise (broadcasts).- File Paths (
ca ca.crt, etc.): Since the configuration file is located in the same directory as the keys and certificates, you do not need to specify long absolute paths. The server pulls these files directly from/etc/openvpn/server/. server 10.8.0.0 255.255.255.0: The server creates an entirely new, virtual internal network specifically for the VPN. It claims the first IP address (10.8.0.1) for itself and automatically assigns addresses from this range (e.g.,10.8.0.2,10.8.0.3) to connected users, such as your Ubuntu laptop.push "redirect-gateway def1...": Thepushdirective forces this setting onto the client upon a successful connection. This specific line overrides the default gateway on the client’s laptop. As a result, all internet traffic from your laptop is routed securely through the VPN server.push "dhcp-option DNS 8.8.8.8": Forces the client to use Google’s public DNS servers for domain name resolution.user nobodyandgroup nobody: A critical security feature. Although the OpenVPN service must initially start with root privileges to create the virtualtun0interface in the kernel, this command drops its privileges to the least privileged user in the system (nobody) immediately after a successful boot. If a vulnerability is found in OpenVPN and the server is compromised, the attacker will be locked within the restricted permissions of thenobodyuser instead of gaining full control over the operating system.keepalive 10 120: The server sends a control “ping” to the client every 10 seconds. If the client fails to respond within 120 seconds (e.g., due to a Wi-Fi dropout or a dead battery), the server safely closes the tunnel to prevent ghost connections from cluttering its memory.cipher AES-256-GCM: Applies modern, extremely secure, and fast hardware-accelerated encryption using the AES-GCM standard
- Applying Routing Persistence and Launching the Service
To ensure smooth operations across reboots, apply persistent kernel forwarding settings and set up the system services
# Persist IPv4 forwarding configuration across system reboots
sudo echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/99-openvpn.conf
sudo sysctl --system
# Open the public UDP listening port on the firewall layer
sudo firewall-cmd --zone=public --add-port=1194/udp --permanent
sudo firewall-cmd --zone=public --add-masquerade --permanent
sudo firewall-cmd --reload
# Enable and immediately launch the targeted systemd OpenVPN service instance
sudo systemctl enable --now openvpn-server@server
Verify that the network runtime engine generated the virtual interface correctly:
sudo systemctl status openvpn-server@server
ip a
_Look for an active state and a new tun0 interface assigned to 10.8.0.1
- 7. Client Provisioning & Verification
Step 1: Initialize Workspace (Execute on Host Workstation)
# Update local package indexes and install OpenVPN along with Easy-RSA utilities
sudo apt update
sudo apt install openvpn easy-rsa -y
# Build a secured staging area for the local client configuration profiles
mkdir ~/openvpn-client
chmod 700 ~/openvpn-client
cd ~/openvpn-client
# Initialize a separate local PKI database structure
/usr/share/easy-rsa/3/easyrsa init-pki
# Generate a local client private key and corresponding signing certificate request
/usr/share/easy-rsa/3/easyrsa gen-req client1 nopass
- Step 2: Cross-System Signing Pipeline
Coordinate files across platforms to sign the client certificate request:
# [ON VM 1] Fetch the signing request file from the host workstation
scp [USER_ON_UBUNTU]@192.168.0.112:~/openvpn-client/pki/reqs/client1.req /tmp/
# [ON VM 1] Pass the request directly over to the isolated CA server
scp /tmp/client1.req [email protected]:/tmp/
# [ON VM 2] Import and sign the client credentials against the master authority
cd ~/openvpn-ca
./easyrsa import-req /tmp/client1.req client1
./easyrsa sign-req client client1
# [ON VM 2] Return the issued certificate profile back to the gateway server
scp pki/issued/client1.crt [email protected]:/tmp/
# [ON VM 1] Push all valid production assets back down to the host workstation
scp /tmp/client1.crt [USER_ON_UBUNTU]@192.168.0.112:/tmp/
scp /etc/openvpn/server/ca.crt [USER_ON_UBUNTU]@192.168.0.112:/tmp/
scp /etc/openvpn/server/tls-crypt.key [USER_ON_UBUNTU]@192.168.0.112:/tmp/
- Step 3: Assembling the All-in-One Profile (Execute on Host Workstation)
Consolidate certificates and keys directly into a single unified client profile:
cp /tmp/ca.crt ~/openvpn-client/
cp /tmp/client1.crt ~/openvpn-client/
cp /tmp/tls-crypt.key ~/openvpn-client/
cd ~/openvpn-client
nano client.ovpn
Populate the configuration structure as follows:
client
dev tun
proto udp
# Here you have to set IP address of your physical machine in case for VM i set interface as NAT
# If set as NAT you have to set on VMWare port forwarding see picture below:
# If you set bridge then not necessary configure port forwarding
remote 192.168.X.X 1194
resolv-retry infinite
nobind
persist-key
persist-tun
cipher AES-256-GCM
auth SHA256
verb 3
<ca>
[Insert text content from ca.crt here]
</ca>
<cert>
[Insert text content from client1.crt here]
</cert>
<key>
[Insert text content from pki/private/client1.key here]
</key>
<tls-crypt>
[Insert text content from tls-crypt.key here]
</tls-crypt>
- 8. Launch and Connection Verification
Fire up the secure communication tunnel directly from the client console:
sudo openvpn --config client.ovpn
Open a secondary distinct shell terminal on your host workstation and test end-to-end connectivity across the virtual networks:
# Test internal connection to the virtual VPN interface gateway
ping 10.8.0.1
# Test secure routed path straight through to the hidden internal CA server
ping 10.0.0.10
To run it more comfortable we can build on Ubuntu laptop OpenVPN client as a service:
Configuring OpenVPN as a Background Service via Systemd (Step-by-Step)
Open a terminal session on your Ubuntu host workstation and execute these three deployment commands:
# 1. Relocate your functional client.ovpn profile into the authoritative system directory and change its file extension to .conf
sudo cp ~/openvpn-client/client.ovpn /etc/openvpn/client/client.conf
# 2. Launch the OpenVPN client engine immediately as an unattended background service
sudo systemctl start openvpn-client@client
# 3. Configure the network daemon instance to initialize automatically during host system reboots (Optional)
sudo systemctl enable openvpn-client@client
OpenVPN Infrastructure Cleanup Guide
This step-by-step guide walks you through completely wiping out the OpenVPN configuration, certificates, security policies, and package implementations across all environments to return your systems to their original state.
1. Cleanup on VM 1 (VPN Server / Router)
Step 1: Terminate and Disable Services
Stop the active OpenVPN runtime process and remove it from the system startup queue.
# Terminate the running OpenVPN server instance immediately
sudo systemctl stop openvpn-server@server
# Disable the service instance from initializing during system boot
sudo systemctl disable openvpn-server@server
Step 2: Remove Packages and Purge Files
Wipe out the core binary files and completely erase all sensitive system and user workspaces.
# Uninstall the OpenVPN package from the system
sudo dnf remove openvpn -y
# Securely remove all cryptographic assets and configuration profiles from the system directory
sudo rm -rf /etc/openvpn/
# Erase the isolated user staging area and local PKI structures
rm -rf ~/openvpn-server/
Step 3: Revert Network Routing & Firewall Rules
Remove the packet forwarding permissions and erase custom access rules from the active firewall profile.
# Remove the persistent kernel packet forwarding rule file
sudo rm -f /etc/sysctl.d/99-openvpn.conf
# Force immediate runtime synchronization of kernel variables to disable forwarding
sudo sysctl --system
# Strip the OpenVPN port validation rule from the public firewall profile
sudo firewall-cmd --zone=public --remove-port=1194/udp --permanent
# Strip the Network Address Translation (Masquerading) rule from the public zone
sudo firewall-cmd --zone=public --remove-masquerade --permanent
# Force an immediate reload of the network security rule engine to commit changes
sudo firewall-cmd --reload
2. Cleanup on VM 2 (Certificate Authority Server)
Since the CA server acts strictly as an offline certificate store, it has no active system service daemons. You only need to remove the operational directories.
# Completely erase the corporate master CA operational workspace and private keys
rm -rf ~/openvpn-ca/
# Flush any temporary signing files or transfer objects out of the shared directory
sudo rm -f /tmp/server.req /tmp/client1.req
3. Cleanup on the Physical Client Node (Ubuntu Workstation)
Step 1: Terminate Background Services
Shut down the unattended client process and prevent it from trying to initialize on subsequent workstation boots.
# Terminate the active OpenVPN background client connection service
sudo systemctl stop openvpn-client@client
# Prevent the automated client daemon from launching during system reboots
sudo systemctl disable openvpn-client@client
Step 2: Uninstall Software and Purge Local Directories
Remove the networking client binaries and clean out all localized operational footprints.
# Purge the OpenVPN configuration profile deployment file from the system directory
sudo rm -f /etc/openvpn/client/client.conf
# Uninstall the local OpenVPN software binaries from the host package engine
sudo apt purge openvpn -y
sudo apt autoremove -y
# Wipe out your local staging area, user keys, and downloaded certificates
rm -rf ~/openvpn-client/
# Flush any incoming or outgoing transfer artifacts out of the host directory
rm -f /tmp/ca.crt /tmp/client1.crt /tmp/tls-crypt.key
4. Verification Check
To confirm that the execution was completely successful and no ghost artifacts remain on your host workstation, run the following verification checks:
# Confirm that the virtual tunnel interface 'tun0' has been successfully destroyed
ip a show dev tun0
# Verify that the background daemon processes are no longer active or tracked
systemctl status openvpn-client@client
Note: If the terminal outputs Device "tun0" does not exist, your networking stack has reverted to its original state.