13 KiB
13 KiB
OpenVPN 实战指南
企业级开源 VPN 解决方案,实现安全远程访问。
1. OpenVPN 简介
1.1 什么是 VPN
VPN(Virtual Private Network,虚拟专用网络):
- 在公共网络上建立的安全加密通道
- 实现远程安全访问公司内网资源
- 保证数据传输的机密性和完整性
1.2 OpenVPN 特点
| 特点 | 说明 |
|---|---|
| 开源免费 | GPL 协议,源码开放 |
| 跨平台 | Windows/Linux/macOS/Android/iOS |
| 安全性高 | 支持 AES、BF-CBC 等加密算法 |
| 简单易用 | 配置简洁,客户端友好 |
| 协议优势 | 基于 TLS/SSL |
| 多种认证 | 支持证书、用户名密码、双因子 |
1.3 OpenVPN 与其他 VPN 对比
| VPN 方案 | 协议 | 安全性 | 复杂度 | 适用场景 |
|---|---|---|---|---|
| OpenVPN | SSL/TLS | 高 | 中 | 通用远程访问 |
| IPSec | IKEv2 | 高 | 高 | 企业级 |
| PPTP | PPP | 低 | 低 | 简单场景(已废弃) |
| L2TP/IPSec | L2TP | 高 | 中 | 兼容性好 |
| WireGuard | WireGuard | 高 | 低 | 现代方案 |
1.4 原理架构
┌──────────────────────────────────────────────┐
│ Internet │
│ (公网/不安全的网络) │
└──────────────────┬───────────────────────────┘
│
┌──────────────────▼───────────────────────────┐
│ OpenVPN Server │
│ ┌─────────────┐ ┌─────────────────────┐ │
│ │ TLS/SSL │ │ 虚拟网卡 tun/tap │ │
│ │ 加密通道 │ │ 10.8.0.0/24 │ │
│ └─────────────┘ └─────────────────────┘ │
└──────────────────┬───────────────────────────┘
│
┌──────────────────▼───────────────────────────┐
│ 内网(安全网络) │
│ 192.168.1.0/24 │
│ 文件服务器/数据库/内部系统 │
└──────────────────────────────────────────┘
1.5 工作流程
客户端 服务端
│ │
│ 发起连接请求 │
│ ─────────────────────>│
│ │
│ 返回证书 │
│ <─────────────────────│
│ │
│ 验证证书 + 配置IP │
│ ─────────────────────>│
│ │
│ 建立 TLS 通道 │
│ <─────────────────────│
│ │
│ 启用虚拟网卡 │
│ <─────────────────────│
│ │
│ 数据加密传输 │
│◄─────────────────────►│
2. OpenVPN 部署
2.1 环境准备
# 系统要求
CentOS 7/8 / Ubuntu 20.04+
内存:512MB+
CPU:1核+
# 关闭 SELinux(如需要)
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# 关闭防火墙(或放行)
systemctl stop firewalld
systemctl disable firewalld
# 放行端口
firewall-cmd --permanent --add-port=1194/udp
firewall-cmd --reload
2.2 安装 OpenVPN
# CentOS/RHEL
yum install -y openvpn easy-rsa
# Ubuntu/Debian
apt update
apt install -y openvpn easy-rsa
# 检查版本
openvpn --version
2.3 配置证书环境
# 复制证书工具
cp -r /usr/share/easy-rsa /etc/openvpn/easy-rsa
cd /etc/openvpn/easy-rsa
# 初始化 PKI
./easyrsa init-pki
# 创建 CA
./easyrsa build-ca nopass
# 创建服务端证书
./easyrsa build-server-full server nopass
# 创建客户端证书
./easyrsa build-client client1 nopass
# 生成 Diffie-Hellman 参数
./easyrsa gen-dh
# 生成 TLS Auth 密钥(防止 DoS 攻击)
openvpn --genkey secret /etc/openvpn/easy-rsa/ta.key
证书文件说明:
pki/ca.crt:CA 证书(客户端和服务端都需要)pki/issued/server.crt:服务端证书pki/private/server.key:服务端私钥pki/issued/client1.crt:客户端证书pki/private/client1.key:客户端私钥pki/dh.pem:DH 参数ta.key:TLS Auth 密钥
2.4 服务端配置
# 创建配置文件
cat > /etc/openvpn/server.conf << 'EOF'
# 端口和协议
port 1194
proto udp
dev tun
# 证书和密钥
ca /etc/openvpn/easy-rsa/pki/ca.crt
cert /etc/openvpn/easy-rsa/pki/issued/server.crt
key /etc/openvpn/easy-rsa/pki/private/server.key
dh /etc/openvpn/easy-rsa/pki/dh.pem
tls-auth /etc/openvpn/easy-rsa/ta.key 0
# 加密算法
cipher AES-256-GCM
auth SHA256
# 网络配置
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist /etc/openvpn/ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
# 客户端可以访问内网
client-config-dir /etc/openvpn/ccd
route 192.168.1.0 255.255.255.0
# 权限和日志
keepalive 10 120
persist-key
persist-tun
status /etc/openvpn/openvpn-status.log
log /etc/openvpn/openvpn.log
verb 3
# 压缩(可选,兼容老客户端)
compress lz4-v2
push "compress lz4-v2"
EOF
2.5 客户端配置(生成配置)
# 创建客户端配置目录
mkdir -p /etc/openvpn/ccd
# 创建客户端配置
cat > /etc/openvpn/ccd/client1 << 'EOF'
# 为客户端分配固定 IP
ifconfig-push 10.8.0.10 10.8.0.9
EOF
2.6 启动服务
# 启动服务
systemctl start openvpn@server
systemctl enable openvpn@server
# 检查状态
systemctl status openvpn@server
ip addr show tun0
2.7 路由和转发配置
# 开启 IP 转发
echo 1 > /proc/sys/net/ipv4/ip_forward
# 永久生效
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
# 配置 NAT(如果需要访问内网)
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
# 永久保存
iptables-save > /etc/sysconfig/iptables
2.8 客户端配置(Linux)
# 安装客户端
apt install -y openvpn
# 复制配置文件
# 方法1:从服务端拉取
scp root@server:/etc/openvpn/easy-rsa/pki/issued/client1.crt /etc/openvpn/client.crt
scp root@server:/etc/openvpn/easy-rsa/pki/private/client1.key /etc/openvpn/client.key
scp root@server:/etc/openvpn/easy-rsa/pki/ca.crt /etc/openvpn/ca.crt
# 创建客户端配置
cat > /etc/openvpn/client.conf << 'EOF'
client
dev tun
proto udp
remote your-server-ip 1194
resolv-retry infinite
nobind
persist-key
persist-tun
remote-cert-tls server
cipher AES-256-GCM
auth SHA256
# 证书
ca /etc/openvpn/ca.crt
cert /etc/openvpn/client.crt
key /etc/openvpn/client.key
# TLS Auth
tls-auth /etc/openvpn/ta.key 1
verb 3
EOF
# 复制 TLS Auth
scp root@server:/etc/openvpn/easy-rsa/ta.key /etc/openvpn/ta.key
# 启动连接
openvpn --config /etc/openvpn/client.conf
# 或使用 systemd
systemctl start openvpn@client
2.9 客户端配置(Windows)
-
从服务端下载证书文件:
ca.crtclient1.crtclient1.keyta.key(可选)
-
下载 OpenVPN 客户端:
-
创建客户端配置
client.ovpn:
client
dev tun
proto udp
remote your-server-ip 1194
resolv-retry infinite
nobind
remote-cert-tls server
cipher AES-256-GCM
auth SHA256
ca ca.crt
cert client1.crt
key client1.key
tls-auth ta.key 1
verb 3
- 导入配置:
- 将证书和配置放入
C:\Program Files\OpenVPN\config\ - 右键托盘图标 → 连接
- 将证书和配置放入
2.10 客户端配置(macOS)
# 使用 Homebrew 安装
brew install openvpn
# 或下载 Tunnelblick:https://tunnelblick.net/
# 导入配置文件(.ovpn)
open -a Tunnelblick client.ovpn
2.11 客户端配置(iOS/Android)
- iOS:下载 OpenVPN Connect 应用,导入
.ovpn配置 - Android:下载 OpenVPN for Android 或 OpenVPN Connect
3. OpenVPN 管理
3.1 日常管理命令
# 查看连接状态
systemctl status openvpn@server
# 重启服务
systemctl restart openvpn@server
# 查看日志
tail -f /etc/openvpn/openvpn.log
# 查看连接的用户
cat /etc/openvpn/openvpn-status.log
3.2 查看连接用户
# 方法1:查看状态文件
cat /etc/openvpn/openvpn-status.log
# 方法2:通过管理接口
echo "status" | nc localhost 1194
# 方法3:检查虚拟网卡
ip addr show tun0
3.3 日志分析
# 实时查看日志
tail -f /etc/openvpn/openvpn.log
# 查看错误日志
grep -i error /etc/openvpn/openvpn.log
# 查看连接日志
grep -i "client connect" /etc/openvpn/openvpn.log
3.4 添加新用户
cd /etc/openvpn/easy-rsa
# 生成新客户端证书
./easyrsa build-client newuser nopass
# 导出证书
# /etc/openvpn/easy-rsa/pki/issued/newuser.crt
# /etc/openvpn/easy-rsa/pki/private/newuser.key
# /etc/openvpn/easy-rsa/pki/ca.crt
3.5 撤销用户
cd /etc/openvpn/easy-rsa
# 撤销证书
./easyrsa revoke newuser
# 生成新的 CRL
./easyrsa gen-crl
# 重启服务
systemctl restart openvpn@server
3.6 配置文件优化
# 调整日志级别
verb 3 # 详细(生产环境建议 3)
# 调整 MTU
tun-mtu 1500
# 调整 keepalive
keepalive 10 120 # 每 10 秒检测,120 秒超时
# 压缩设置
compress lz4-v2 # 启用压缩
3.7 性能优化
# 调整缓冲区大小
sndbuf 393216
rcvbuf 393216
# 多线程(如果 CPU 多核)
multihome
# 连接数限制
max-clients 100
3.8 安全加固
# 限制客户端 IP
# 在 ccd 目录中配置
cat > /etc/openvpn/ccd/client1 << 'EOF'
ifconfig-push 10.8.0.10 10.8.0.9
# 绑定特定 IP
iroute 192.168.10.0 255.255.255.0
EOF
# 启用 LZO 压缩
compress lz4-v2
# 使用强加密算法
cipher AES-256-GCM
auth SHA256
# 定期更换证书
# 每年更换一次 CA 和服务端证书
3.9 常见问题处理
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 连接失败 | 端口被阻 | 检查防火墙/放行 1194 端口 |
| 连接成功但无法访问内网 | 未开启 IP 转发 | 开启 net.ipv4.ip_forward |
| 连接后无法解析 DNS | 未推送 DNS | 配置 push "dhcp-option DNS" |
| 证书过期 | 证书过期 | 重新签发证书 |
| 速度慢 | MTU 问题 | 调整 tun-mtu |
3.10 配置文件示例
# 生产环境配置
cat > /etc/openvpn/server.conf << 'EOF'
port 1194
proto udp
dev tun
dev-type tun
# 证书
ca ca.crt
cert server.crt
key server.key
dh dh.pem
tls-auth ta.key 0
# 加密
cipher AES-256-GCM
auth SHA256
# 网络
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 223.5.5.5"
push "dhcp-option DNS 119.29.29.29"
# 客户端配置目录
client-config-dir ccd
# 路由
route 192.168.1.0 255.255.255.0
# 权限
keepalive 10 120
persist-key
persist-tun
user nobody
group nobody
# 日志
status openvpn-status.log
log-append openvpn.log
verb 3
compress lz4-v2
push "compress lz4-v2"
EOF
4. 常用命令速查
4.1 服务端
| 场景 | 命令 |
|---|---|
| 启动 | systemctl start openvpn@server |
| 停止 | systemctl stop openvpn@server |
| 重启 | systemctl restart openvpn@server |
| 查看状态 | systemctl status openvpn@server |
| 查看日志 | tail -f /etc/openvpn/openvpn.log |
4.2 客户端
| 场景 | 命令 |
|---|---|
| 启动连接 | openvpn --config client.conf |
| 后台运行 | openvpn --config client.conf --daemon |
| 测试配置 | openvpn --config client.conf --test-crypto |
| 查看连接 | cat /etc/openvpn/openvpn-status.log |
4.3 证书操作
| 场景 | 命令 |
|---|---|
| 生成客户端证书 | ./easyrsa build-client name nopass |
| 撤销证书 | ./easyrsa revoke name |
| 生成 CRL | ./easyrsa gen-crl |
| 查看证书 | ./easyrsa show cert name |
5. 小结
| 类别 | 关键点 |
|---|---|
| 协议 | UDP(推荐)/TCP |
| 端口 | 1194 |
| 加密 | AES-256-GCM + SHA256 |
| 网络 | 10.8.0.0/24 |
| 认证 | 证书 + TLS |
| 客户端 | Windows/macOS/Linux/Android/iOS |
生产环境建议:使用 UDP 协议、配置证书 + 密码双因子认证、定期更换证书。