常见VPN协议对比
| 协议 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
| OpenVPN | 跨平台、配置灵活、安全性高 | 配置复杂、性能中等 | 通用远程访问 |
| WireGuard | 高性能、现代加密、简单配置 | 需较新内核(Linux) | 高速低延迟需求 |
| IPsec/L2TP | 原生支持(移动设备) | 配置复杂、NAT穿透问题 | 企业级兼容性要求 |
| SoftEther | 多协议支持、高性能 | 资源占用较高 | 多功能集成服务器 |
OpenVPN服务器搭建(Ubuntu为例)
安装依赖
sudo apt update sudo apt install openvpn easy-rsa
配置CA和证书
make-cadir ~/openvpn-ca cd ~/openvpn-ca
编辑 vars 文件设置证书信息,然后生成:
source vars ./clean-all ./build-ca # 生成CA ./build-key-server server # 服务器证书 ./build-key client1 # 客户端证书 ./build-dh # Diffie-Hellman参数 openvpn --genkey --secret ta.key # TLS-auth密钥
服务器配置
复制文件到OpenVPN目录:
cd ~/openvpn-ca/keys sudo cp ca.crt server.crt server.key ta.key dh2048.pem /etc/openvpn/server/
创建配置文件 /etc/openvpn/server/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 tls-auth ta.key 0 cipher AES-256-CBC comp-lzo user nobody group nogroup persist-key persist-tun status openvpn-status.log verb 3
启动服务
sudo systemctl enable --now openvpn-server@server sudo sysctl -w net.ipv4.ip_forward=1 # 启用IP转发
配置防火墙(UFW)
sudo ufw allow 1194/udp sudo ufw allow OpenSSH sudo ufw enable
WireGuard服务器搭建
安装(Ubuntu)
sudo apt install wireguard resolvconf
生成密钥对
wg genkey | sudo tee /etc/wireguard/private.key | wg pubkey | sudo tee /etc/wireguard/public.key chmod 600 /etc/wireguard/private.key
配置服务器(/etc/wireguard/wg0.conf)
[Interface] Address = 10.0.0.1/24 ListenPort = 51820 PrivateKey = <服务器私钥> PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE [Peer] PublicKey = <客户端公钥> AllowedIPs = 10.0.0.2/32
启动服务
sudo systemctl enable --now wg-quick@wg0
客户端配置示例
[Interface] PrivateKey = <客户端私钥> Address = 10.0.0.2/24 DNS = 8.8.8.8 [Peer] PublicKey = <服务器公钥> Endpoint = <服务器IP>:51820 AllowedIPs = 0.0.0.0/0 PersistentKeepalive = 25
安全与优化建议
-
强化安全:
- 使用TLS-auth(OpenVPN)或PSK(IPsec)防DoS攻击。
- 限制VPN访问IP(防火墙规则)。
- 定期更新证书和密钥。
-
性能优化:
- WireGuard优先用于高带宽场景。
- 启用TCP BBR(Linux内核参数优化)。
-
日志监控:
- 检查
/var/log/syslog(OpenVPN)或journalctl -u wg-quick@wg0(WireGuard)。
- 检查
故障排查
- 连接失败:检查端口开放(
telnet <IP> <PORT>)、防火墙规则。 - DNS泄漏:客户端配置中明确指定DNS服务器。
- 速度慢:尝试更换协议(如UDP改TCP)或调整MTU。
根据需求选择协议:OpenVPN适合通用场景,WireGuard适合高性能需求,IPsec适合企业兼容性,如需图形化管理,可考虑SoftEtherVPN或商业方案。



