Lessons

OpenVPN Setup

Level: Expert Module: Security & Authentication 8 min read Lesson 52 of 66

Overview

  • What you will learn: How OpenVPN creates secure TLS tunnels over UDP or TCP, how to build a Public Key Infrastructure (PKI) with easy-rsa for certificate-based authentication, configure the OpenVPN server (server.conf) and client (client.conf), understand the difference between routing (tun) and bridging (tap) modes, push routes and DNS settings to clients, harden the VPN with tls-crypt, and manage client certificates.
  • Prerequisites: Lesson 51 (SSSD and Active Directory Integration), understanding of IP routing, firewalls, and X.509 certificates
  • Estimated reading time: 30 minutes

Introduction

A Virtual Private Network (VPN) extends a private network across a public network, allowing remote users and branch offices to access internal resources as if they were directly connected to the LAN. OpenVPN is an open-source, SSL/TLS-based VPN solution that runs in user space and can traverse NAT and firewalls.

Unlike IPsec, which operates at the kernel level and requires complex configuration, OpenVPN uses the well-understood TLS protocol for key exchange and channel encryption. It authenticates peers using X.509 certificates issued by a private Certificate Authority (CA), providing strong mutual authentication without relying on pre-shared keys.

In this lesson you will build a complete OpenVPN infrastructure: create a CA and issue certificates with easy-rsa, configure the server and client, choose between routing and bridging, push network settings to clients, and apply security hardening best practices.

OpenVPN Architecture

TLS Tunnels

OpenVPN establishes a TLS session between the client and server. This session handles authentication (via certificates) and key negotiation. Once the TLS session is established, OpenVPN creates a virtual network interface (tun or tap) and encrypts all traffic flowing through it using the negotiated keys.

Routing (tun) vs Bridging (tap)

  • Routing mode (tun): Creates a Layer 3 (IP) tunnel. The VPN server routes packets between the VPN subnet and the internal network. This is the recommended mode for most deployments — it is more efficient and easier to secure.
  • Bridging mode (tap): Creates a Layer 2 (Ethernet) tunnel. The VPN client appears to be on the same broadcast domain as the server’s LAN. Required for non-IP protocols or when broadcast/multicast is needed.

Building the PKI with easy-rsa

Every OpenVPN deployment needs a Certificate Authority (CA) to issue certificates. The easy-rsa package provides a simple command-line CA for this purpose.

# Install easy-rsa
$ sudo apt install easy-rsa

# Create a PKI directory
$ make-cadir ~/openvpn-ca
$ cd ~/openvpn-ca

# Edit vars for your organization (optional)
# nano vars
# set_var EASYRSA_REQ_COUNTRY   "TW"
# set_var EASYRSA_REQ_PROVINCE  "Taipei"
# set_var EASYRSA_REQ_CITY      "Taipei"
# set_var EASYRSA_REQ_ORG       "Example Inc"
# set_var EASYRSA_REQ_EMAIL     "admin@example.com"
# set_var EASYRSA_KEY_SIZE      2048
# set_var EASYRSA_ALGO          ec
# set_var EASYRSA_CURVE         secp384r1

# Initialize the PKI
$ ./easyrsa init-pki

# Build the CA (prompts for CA passphrase)
$ ./easyrsa build-ca
# Common Name: Example-CA

# Generate the server certificate and key
$ ./easyrsa gen-req server nopass
$ ./easyrsa sign-req server server

# Generate Diffie-Hellman parameters (takes a few minutes)
$ ./easyrsa gen-dh

# Generate a TLS key for additional security
$ openvpn --genkey secret ta.key

# Generate a client certificate
$ ./easyrsa gen-req client1 nopass
$ ./easyrsa sign-req client client1

PKI Files Summary

# Files needed on the SERVER:
# ~/openvpn-ca/pki/ca.crt            — CA certificate
# ~/openvpn-ca/pki/issued/server.crt — Server certificate
# ~/openvpn-ca/pki/private/server.key — Server private key
# ~/openvpn-ca/pki/dh.pem            — Diffie-Hellman parameters
# ~/openvpn-ca/ta.key                — TLS auth key

# Files needed on each CLIENT:
# ~/openvpn-ca/pki/ca.crt             — CA certificate
# ~/openvpn-ca/pki/issued/client1.crt — Client certificate
# ~/openvpn-ca/pki/private/client1.key — Client private key
# ~/openvpn-ca/ta.key                  — TLS auth key

Server Configuration

# Copy PKI files to /etc/openvpn/server/
$ sudo cp ~/openvpn-ca/pki/ca.crt /etc/openvpn/server/
$ sudo cp ~/openvpn-ca/pki/issued/server.crt /etc/openvpn/server/
$ sudo cp ~/openvpn-ca/pki/private/server.key /etc/openvpn/server/
$ sudo cp ~/openvpn-ca/pki/dh.pem /etc/openvpn/server/
$ sudo cp ~/openvpn-ca/ta.key /etc/openvpn/server/

# /etc/openvpn/server/server.conf
port 1194
proto udp
dev tun

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

# VPN subnet — clients get IPs from this range
server 10.8.0.0 255.255.255.0

# Maintain a record of client-to-virtual-IP associations
ifconfig-pool-persist ipp.txt

# Push routes to clients (access the internal 10.0.1.0/24 network)
push "route 10.0.1.0 255.255.255.0"

# Push DNS servers to clients
push "dhcp-option DNS 10.0.1.1"
push "dhcp-option DNS 8.8.8.8"

# Redirect all client traffic through the VPN (full tunnel)
# push "redirect-gateway def1 bypass-dhcp"

# Allow client-to-client communication
# client-to-client

# TLS hardening
tls-crypt ta.key
auth SHA256
cipher AES-256-GCM
tls-version-min 1.2

# Keepalive and timeout
keepalive 10 120

# Run as unprivileged user
user nobody
group nogroup

# Persist keys and tunnel across restarts
persist-key
persist-tun

# Logging
status /var/log/openvpn/status.log
log-append /var/log/openvpn/server.log
verb 3

Enable IP Forwarding and Firewall

# Enable IP forwarding
$ echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/99-openvpn.conf
$ sudo sysctl -p /etc/sysctl.d/99-openvpn.conf

# Configure NAT (masquerade VPN traffic)
$ sudo iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

# Make iptables rules persistent
$ sudo apt install iptables-persistent
$ sudo netfilter-persistent save

# Start OpenVPN
$ sudo systemctl enable --now openvpn-server@server

Client Configuration

# /etc/openvpn/client/client.conf (or client1.ovpn for GUI clients)
client
dev tun
proto udp

remote vpn.example.com 1194
resolv-retry infinite
nobind

# Run as unprivileged user
user nobody
group nogroup

persist-key
persist-tun

ca ca.crt
cert client1.crt
key client1.key

# TLS hardening (must match server)
tls-crypt ta.key
auth SHA256
cipher AES-256-GCM

# Verify server certificate
remote-cert-tls server

verb 3
# Test the connection
$ sudo openvpn --config /etc/openvpn/client/client.conf

# Or use systemd
$ sudo systemctl start openvpn-client@client

# Verify the tunnel
$ ip addr show tun0
$ ping 10.8.0.1    # VPN server
$ ping 10.0.1.10   # Internal server via VPN

Certificate Revocation

# Revoke a compromised or departed client's certificate
$ cd ~/openvpn-ca
$ ./easyrsa revoke client1
$ ./easyrsa gen-crl

# Copy the CRL to the server
$ sudo cp ~/openvpn-ca/pki/crl.pem /etc/openvpn/server/

# Add to server.conf
# crl-verify crl.pem

# Restart the server
$ sudo systemctl restart openvpn-server@server

Key Takeaways

  • OpenVPN creates TLS-encrypted tunnels using X.509 certificates for mutual authentication — no passwords or pre-shared keys are needed for the transport layer.
  • Build a private CA with easy-rsa to issue server and client certificates; keep the CA private key offline or on a dedicated secure machine.
  • Routing mode (tun) is recommended for most deployments; bridging mode (tap) is needed only for Layer 2 protocols or broadcast-dependent applications.
  • Use tls-crypt to add an additional layer of encryption and HMAC authentication to the TLS control channel, preventing fingerprinting and DoS attacks.
  • Revoke compromised client certificates with easy-rsa and distribute the CRL to the server to immediately block access.

What’s Next

In the next lesson you will learn about WireGuard VPN — a modern, high-performance VPN that uses cryptokey routing, minimal configuration, and state-of-the-art cryptography built directly into the Linux kernel.

繁體中文

概述

  • 學習目標:OpenVPN 如何透過 UDP 或 TCP 建立安全的 TLS 通道、如何使用 easy-rsa 建立公鑰基礎設施(PKI)進行憑證認證、設定 OpenVPN 伺服器(server.conf)和客戶端(client.conf)、了解路由(tun)和橋接(tap)模式的區別、向客戶端推送路由和 DNS 設定、使用 tls-crypt 強化 VPN,以及管理客戶端憑證。
  • 先決條件:第 51 課(SSSD 和 Active Directory 整合),了解 IP 路由、防火牆和 X.509 憑證
  • 預計閱讀時間:30 分鐘

簡介

虛擬專用網路(VPN)將專用網路延伸到公共網路上,允許遠端使用者和分支機構存取內部資源。OpenVPN 是一個基於 SSL/TLS 的開源 VPN 解決方案,在使用者空間運行,可以穿越 NAT 和防火牆。

與在核心層級運作且需要複雜設定的 IPsec 不同,OpenVPN 使用 TLS 協定進行金鑰交換和通道加密。它使用由私有憑證授權機構(CA)發行的 X.509 憑證來認證對等方。

OpenVPN 架構

路由(tun)與橋接(tap)

  • 路由模式(tun):建立第 3 層(IP)通道。推薦大多數部署使用——更高效且更容易保護。
  • 橋接模式(tap):建立第 2 層(乙太網路)通道。當需要非 IP 協定或廣播/多播時使用。

使用 easy-rsa 建立 PKI

# 安裝 easy-rsa
$ sudo apt install easy-rsa

# 建立 PKI 目錄
$ make-cadir ~/openvpn-ca
$ cd ~/openvpn-ca

# 初始化 PKI
$ ./easyrsa init-pki

# 建立 CA
$ ./easyrsa build-ca

# 產生伺服器憑證和金鑰
$ ./easyrsa gen-req server nopass
$ ./easyrsa sign-req server server

# 產生 Diffie-Hellman 參數
$ ./easyrsa gen-dh

# 產生 TLS 金鑰
$ openvpn --genkey secret ta.key

# 產生客戶端憑證
$ ./easyrsa gen-req client1 nopass
$ ./easyrsa sign-req client client1

伺服器設定

# /etc/openvpn/server/server.conf 主要設定
port 1194
proto udp
dev tun
server 10.8.0.0 255.255.255.0
push "route 10.0.1.0 255.255.255.0"
push "dhcp-option DNS 10.0.1.1"
tls-crypt ta.key
cipher AES-256-GCM
keepalive 10 120

# 啟用 IP 轉發
$ echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/99-openvpn.conf
$ sudo sysctl -p /etc/sysctl.d/99-openvpn.conf

# 設定 NAT
$ sudo iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

# 啟動 OpenVPN
$ sudo systemctl enable --now openvpn-server@server

客戶端設定

# client.conf 主要設定
client
dev tun
proto udp
remote vpn.example.com 1194
tls-crypt ta.key
cipher AES-256-GCM
remote-cert-tls server

# 測試連線
$ sudo openvpn --config /etc/openvpn/client/client.conf
$ ping 10.8.0.1

憑證撤銷

# 撤銷已洩漏的客戶端憑證
$ ./easyrsa revoke client1
$ ./easyrsa gen-crl
$ sudo cp ~/openvpn-ca/pki/crl.pem /etc/openvpn/server/
# 在 server.conf 中添加:crl-verify crl.pem

重點摘要

  • OpenVPN 使用 X.509 憑證進行相互認證來建立 TLS 加密通道——傳輸層不需要密碼或預共享金鑰。
  • 使用 easy-rsa 建立私有 CA 來發行伺服器和客戶端憑證;將 CA 私鑰離線保管或存放在專用安全機器上。
  • 路由模式(tun)推薦用於大多數部署;橋接模式(tap)僅在需要第 2 層協定或依賴廣播的應用程式時使用。
  • 使用 tls-crypt 為 TLS 控制通道添加額外的加密和 HMAC 認證層,防止指紋識別和 DoS 攻擊。
  • 使用 easy-rsa 撤銷已洩漏的客戶端憑證,並將 CRL 分發到伺服器以立即阻止存取。

下一步

在下一課中,您將學習 WireGuard VPN——一種使用密碼金鑰路由、最少設定和最先進密碼學的現代高效能 VPN,直接內建於 Linux 核心中。

日本語

概要

  • 学習内容:OpenVPN が UDP または TCP 上で安全な TLS トンネルを作成する仕組み、easy-rsa による PKI(公開鍵基盤)の構築と証明書ベースの認証、OpenVPN サーバー(server.conf)とクライアント(client.conf)の設定、ルーティング(tun)とブリッジ(tap)モードの違い、クライアントへのルートと DNS 設定のプッシュ、tls-crypt による VPN の強化、クライアント証明書の管理。
  • 前提条件:レッスン51(SSSD と Active Directory 統合)、IP ルーティング、ファイアウォール、X.509 証明書の理解
  • 推定読了時間:30分

はじめに

VPN(仮想プライベートネットワーク)はプライベートネットワークをパブリックネットワーク上に拡張し、リモートユーザーや拠点が内部リソースにアクセスできるようにします。OpenVPN は SSL/TLS ベースのオープンソース VPN ソリューションで、ユーザースペースで動作し、NAT やファイアウォールを通過できます。

カーネルレベルで動作し複雑な設定が必要な IPsec とは異なり、OpenVPN は TLS プロトコルを鍵交換とチャネル暗号化に使用します。プライベート CA が発行した X.509 証明書でピアを認証します。

OpenVPN アーキテクチャ

ルーティング(tun)とブリッジ(tap)

  • ルーティングモード(tun):レイヤー3(IP)トンネルを作成。ほとんどのデプロイメントに推奨。効率的で保護が容易。
  • ブリッジモード(tap):レイヤー2(イーサネット)トンネルを作成。非 IP プロトコルやブロードキャスト/マルチキャストが必要な場合に使用。

easy-rsa による PKI の構築

# easy-rsa のインストール
$ sudo apt install easy-rsa

# PKI ディレクトリの作成
$ make-cadir ~/openvpn-ca
$ cd ~/openvpn-ca

# PKI の初期化
$ ./easyrsa init-pki

# CA の構築
$ ./easyrsa build-ca

# サーバー証明書と鍵の生成
$ ./easyrsa gen-req server nopass
$ ./easyrsa sign-req server server

# Diffie-Hellman パラメータの生成
$ ./easyrsa gen-dh

# TLS 鍵の生成
$ openvpn --genkey secret ta.key

# クライアント証明書の生成
$ ./easyrsa gen-req client1 nopass
$ ./easyrsa sign-req client client1

サーバー設定

# /etc/openvpn/server/server.conf の主要設定
port 1194
proto udp
dev tun
server 10.8.0.0 255.255.255.0
push "route 10.0.1.0 255.255.255.0"
push "dhcp-option DNS 10.0.1.1"
tls-crypt ta.key
cipher AES-256-GCM
keepalive 10 120

# IP フォワーディングの有効化
$ echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/99-openvpn.conf
$ sudo sysctl -p /etc/sysctl.d/99-openvpn.conf

# NAT の設定
$ sudo iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

# OpenVPN の起動
$ sudo systemctl enable --now openvpn-server@server

クライアント設定

# client.conf の主要設定
client
dev tun
proto udp
remote vpn.example.com 1194
tls-crypt ta.key
cipher AES-256-GCM
remote-cert-tls server

# 接続のテスト
$ sudo openvpn --config /etc/openvpn/client/client.conf
$ ping 10.8.0.1

証明書の失効

# 漏洩したクライアント証明書の失効
$ ./easyrsa revoke client1
$ ./easyrsa gen-crl
$ sudo cp ~/openvpn-ca/pki/crl.pem /etc/openvpn/server/
# server.conf に追加:crl-verify crl.pem

重要ポイント

  • OpenVPN は相互認証のために X.509 証明書を使用して TLS 暗号化トンネルを作成する。トランスポート層にパスワードや事前共有鍵は不要。
  • easy-rsa でプライベート CA を構築してサーバーとクライアントの証明書を発行する。CA 秘密鍵はオフラインまたは専用の安全なマシンに保管する。
  • ルーティングモード(tun)がほとんどのデプロイメントに推奨。ブリッジモード(tap)はレイヤー2プロトコルやブロードキャスト依存のアプリケーションにのみ必要。
  • tls-crypt を使用して TLS 制御チャネルに追加の暗号化と HMAC 認証レイヤーを追加し、フィンガープリンティングと DoS 攻撃を防止する。
  • easy-rsa で漏洩したクライアント証明書を失効させ、CRL をサーバーに配布してアクセスを即座にブロックする。

次のステップ

次のレッスンでは、WireGuard VPN について学びます。暗号鍵ルーティング、最小限の設定、Linux カーネルに直接組み込まれた最新の暗号技術を使用する、モダンで高性能な VPN を学びます。

You Missed