23- 虚拟私人网络(VPN)(WireGuard / OpenVPN) 🔒
前提条件 ✅
- 已完成 Web 服务、数据库、邮件服务基础配置
- 以 ops 用户登录(具有 sudo 权限)
- 系统运行在 Debian 12 Bookworm
- 防火墙已放行 VPN 端口(WireGuard 默认 51820/udp,OpenVPN 默认 1194/udp 或 443/tcp)
- 全局约定:主机名 LinuxDC,IP 192.168.1.100(VPN 服务器)
- 建议:准备一台客户端设备(手机/电脑)测试连接
详细步骤 🛠️
- WireGuard(推荐现代生产首选:轻量、高性能、配置简单)
安装:
sudo apt update
sudo apt install -y wireguard
生成服务器密钥对:
wg genkey | tee /etc/wireguard/private.key | wg pubkey > /etc/wireguard/public.key
chmod 600 /etc/wireguard/private.key
创建服务器配置文件:
sudo nano /etc/wireguard/wg0.conf
内容示例:
[Interface]
Address = 10.66.66.1/24
PrivateKey = <服务器 private.key 内容>
ListenPort = 51820
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o ens33 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o ens33 -j MASQUERADE
# 客户端示例(后续添加)
#[Peer]
#PublicKey = <客户端公钥>
#AllowedIPs = 10.66.66.2/32
启用 IP 转发:
sudo sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.d/99-vpn.conf
sudo sysctl --system
启动 WireGuard:
sudo wg-quick up wg0
sudo systemctl enable wg-quick@wg0
sudo wg show
防火墙放行:
sudo ufw allow 51820/udp comment 'WireGuard'
sudo ufw reload
- 添加客户端(手机/电脑)
生成客户端密钥对(在客户端设备或服务器上生成后复制):
wg genkey | tee client-private.key | wg pubkey > client-public.key
在服务器 wg0.conf 添加 Peer:
[Peer]
PublicKey = <客户端 public.key 内容>
AllowedIPs = 10.66.66.2/32
重启:
sudo wg-quick down wg0 && sudo wg-quick up wg0
客户端配置文件(client.conf):
[Interface]
PrivateKey = <客户端 private.key>
Address = 10.66.66.2/24
DNS = 8.8.8.8, 1.1.1.1
[Peer]
PublicKey = <服务器 public.key>
Endpoint = 192.168.1.100:51820 # 或你的公网 IP:端口
AllowedIPs = 0.0.0.0/0 # 全流量通过 VPN
PersistentKeepalive = 25
客户端导入(Windows/macOS/Linux 用 WireGuard 官方客户端,Android/iOS 用官方 App)
- OpenVPN(传统方案,兼容性强,支持 TCP)
安装:
sudo apt install -y openvpn easy-rsa
设置证书权威(CA):
make-cadir ~/openvpn-ca
cd ~/openvpn-ca
nano vars # 修改 KEY_COUNTRY 等信息
source vars
./clean-all
./build-ca
./build-key-server server
./build-dh
./build-key client1
配置服务器:
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
关键修改:
port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh2048.pem
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
user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
verb 3
复制证书:
sudo cp ~/openvpn-ca/keys/{ca.crt,server.crt,server.key,dh2048.pem} /etc/openvpn/
启动:
sudo systemctl enable openvpn@server
sudo systemctl start openvpn@server
客户端 .ovpn 文件需包含证书和配置。
- 推荐生产选择
- 优先 WireGuard:速度快、代码少、审计好、配置简单
- OpenVPN:需要 TCP 443 穿透、兼容老设备时使用
实践任务 🎯
- 安装并启动 WireGuard,创建 wg0 接口,确认 wg show 显示 ListenPort 51820
- 生成一对客户端密钥,在服务器添加 Peer,客户端导入配置并连接测试(ping 10.66.66.1)
- (可选)安装 OpenVPN,生成证书,启动 server 实例,确认端口 1194 监听
- 从客户端访问内部资源(例如 ping 192.168.1.100),确认 VPN 隧道正常
自测问题 ❓
- WireGuard 的 AllowedIPs 参数在服务器端和客户端分别代表什么含义?
- 为什么 WireGuard 推荐使用 UDP 而 OpenVPN 支持 TCP 和 UDP?
- 在 PostUp/PostDown 脚本中为什么要添加 iptables MASQUERADE 规则?
- 如何让 WireGuard 客户端只路由特定网段(分流)而不是全流量?
总结 📌
WireGuard 是现代 VPN 的首选,配置极简、性能极高;OpenVPN 兼容性更广。
本章搭建了基本的站点到站点/远程访问 VPN,为后续高可用、混合云、远程运维提供安全隧道基础。
生产中建议结合 DNS(如 internal DNS)、证书管理(Let's Encrypt 或自建 CA)、fail2ban 监控 VPN 日志。