WireGuard 简介
WireGuard 是近年来最受关注的 VPN 协议之一,由 Jason A. Donenfeld 开发,以极简的代码量(约 4000 行)和出色的性能表现闻名。相比传统协议如 OpenVPN 和 IPsec,WireGuard 在连接速度、握手效率和电池续航方面都有显著优势。
核心优势
| 对比维度 | WireGuard | OpenVPN | IPsec |
|---|---|---|---|
| 代码量 | ~4000 行 | ~60000 行 | ~400000 行 |
| 加密算法 | ChaCha20/Poly1305 | AES-256-GCM | AES-256-CBC |
| 握手速度 | 1-RTT | 2-RTT | 2-RTT+ |
| 电池续航 | 优秀 | 一般 | 较差 |
| 内核集成 | Linux 5.6+ | 无 | 内核模块 |
服务端部署
安装 WireGuard
# Ubuntu/Debian
sudo apt update
sudo apt install wireguard
# CentOS/RHEL
sudo yum install epel-release
sudo yum install wireguard-tools
# 生成密钥对
wg genkey | tee privatekey | wg pubkey > publickey
配置文件
# /etc/wireguard/wg0.conf
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = YOUR_SERVER_PRIVATE_KEY
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
[Peer]
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32
客户端配置
Windows/macOS
下载官方客户端:https://www.wireguard.com/install/
配置文件示例
[Interface]
PrivateKey = YOUR_CLIENT_PRIVATE_KEY
Address = 10.0.0.2/24
DNS = 8.8.8.8
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = your-server.com:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
性能优化
- 启用
MTU优化:通常设置为 1420 - 使用
PersistentKeepalive保持 NAT 穿透 - 配置
AllowedIPs分流:仅代理特定 IP 段
发表回复