WireGuard 是目前最先进的 VPN 协议之一,内核级实现,代码量不到 4000 行,配置简单、性能极高。本文从零开始,覆盖点对点、Hub-and-Spoke、Site-to-Site 三种组网模式,实现多台服务器/家庭网络的内网互联。
目录
为什么选择 WireGuard
| 特性 |
WireGuard |
OpenVPN |
IPsec |
| 代码量 |
~4000 行 |
~60万行 |
极复杂 |
| 延迟 |
极低(内核态) |
较高(用户态) |
中等 |
| 配置 |
1 个文件,几分钟 |
证书链复杂 |
繁琐 |
| 加密 |
Curve25519 + ChaCha20 + Poly1305 |
OpenSSL |
多种可选 |
| 漫游 |
原生支持 |
需额外配置 |
有限 |
| 内核集成 |
Linux 5.6+ 内置 |
否 |
部分 |
WireGuard 的核心优势:
- 内核级:直接在 Linux 内核中运行,性能远超用户态 VPN
- 无连接:无传统握手协议,对端离线不影响本端配置
- 加密默认:完美前向保密,所有流量默认加密
- 漫游友好:对端 IP 变化自动重连
工作原理
WireGuard 通过 UDP 传输加密数据包。每个节点有一个私钥和公钥,通信前通过公钥认证 + 加密。
[节点 A: 10.0.0.1/24] ←--- UDP 加密隧道 ---→ [节点 B: 10.0.0.2/24] 私钥: aAA... 私钥: bBB... 公钥: aPUB... 公钥: bPUB...
|
术语解释:
| 术语 |
说明 |
| Interface |
本地 WireGuard 网卡配置(IP、端口、私钥) |
| Peer |
对端节点配置(公钥、允许 IP、端点地址) |
| AllowedIPs |
哪些目标 IP 走 WireGuard 隧道 |
| Endpoint |
对端的公网 IP:端口 |
| PersistentKeepalive |
保活间隔(NAT 场景必备) |
安装 WireGuard
Linux(Debian / Ubuntu)
apt update apt install -y wireguard
wg --version
|
Linux(CentOS / Rocky Linux)
dnf install -y epel-release dnf install -y wireguard-tools
|
macOS
brew install wireguard-tools
|
Windows
下载 WireGuard Windows 客户端 安装即可。
基础配置:点对点连接
这是最简单的模式,两台服务器直接互联。
生成密钥对
在 两台机器上分别执行:
wg genkey | tee privatekey
cat privatekey | wg pubkey | tee publickey
|
节点 A 配置(服务器 A,公网 IP: 1.2.3.4)
创建 /etc/wireguard/wg0.conf:
[Interface] Address = 10.0.0.1/24 ListenPort = 51820 PrivateKey = aAA...(节点 A 的私钥)
MTU = 1420
[Peer] PublicKey = bPUB...(节点 B 的公钥) AllowedIPs = 10.0.0.2/32 Endpoint = 5.6.7.8:51820 PersistentKeepalive = 25
|
节点 B 配置(服务器 B,公网 IP: 5.6.7.8)
创建 /etc/wireguard/wg0.conf:
[Interface] Address = 10.0.0.2/24 ListenPort = 51820 PrivateKey = bBB...(节点 B 的私钥) MTU = 1420
[Peer] PublicKey = aPUB...(节点 A 的公钥) AllowedIPs = 10.0.0.1/32 Endpoint = 1.2.3.4:51820 PersistentKeepalive = 25
|
启动 WireGuard
wg-quick up wg0
wg show
systemctl enable wg-quick@wg0
|
测试连通性
ping 10.0.0.2 ping 10.0.0.1
|
如果 ping 通,点对点连接建立成功。
Hub-and-Spoke 星型组网
用一台公网服务器作为 Hub,多台内网/客户端机器通过它互联。这是最常用的模式,适合多 VPS + 家庭内网组网。
拓扑
[Hub 服务器] 公网 IP: 1.2.3.4 虚拟 IP: 10.0.0.1/24 ┌─────────────┐ │ 10.0.0.1 │ └──────┬──────┘ ┌───────────┼───────────┐ │ │ │ [VPS 香港] [家庭 NAS] [手机/笔记本] 10.0.0.2 10.0.0.3 10.0.0.100
|
Hub 配置
[Interface] Address = 10.0.0.1/24 ListenPort = 51820 PrivateKey = hub私钥 MTU = 1420
[Peer]
PublicKey = hk公钥 AllowedIPs = 10.0.0.2/32
[Peer]
PublicKey = nas公钥 AllowedIPs = 10.0.0.3/32, 192.168.1.0/24
[Peer]
PublicKey = mobile公钥 AllowedIPs = 10.0.0.100/32
|
Hub 的 AllowedIPs 填写每个 Spoke 的虚拟 IP。如果 Spoke 后面还有局域网,一并加上(如 192.168.1.0/24)。
Spoke 配置(以 VPS 香港为例)
[Interface] Address = 10.0.0.2/24 PrivateKey = hk私钥 MTU = 1420
[Peer] PublicKey = hub公钥 AllowedIPs = 10.0.0.0/24 Endpoint = 1.2.3.4:51820 PersistentKeepalive = 25
|
开启 IP 转发(所有节点)
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.conf sysctl -p
|
Spoke 互通
通过 Hub 转发,香港 VPS 可以直接访问家庭 NAS 的内网 IP:
ping 10.0.0.3 ping 192.168.1.100
|
Site-to-Site 站点到站点组网
将两个局域网通过 WireGuard 打通,实现异地局域网互联。
拓扑
[Site A: 上海] [Site B: 香港] ┌──────────────┐ ┌──────────────┐ │ PC-A: │ │ PC-B: │ │ 192.168.1.10 │ │ 192.168.2.20 │ │ │ WireGuard │ │ │ Router-A │◄─────────────►│ Router-B │ │ 192.168.1.1 │ 10.0.0.1 │ 192.168.2.1 │ │ WG: 10.0.0.1 │ ←→ │ WG: 10.0.0.2 │ └──────────────┘ └──────────────┘
|
Site A 配置
[Interface] Address = 10.0.0.1/24 ListenPort = 51820 PrivateKey = siteA私钥 MTU = 1420 PostUp = iptables -A FORWARD -i wg0 -j ACCEPT PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
[Peer] PublicKey = siteB公钥 AllowedIPs = 10.0.0.2/32, 192.168.2.0/24 Endpoint = siteB公网IP:51820 PersistentKeepalive = 25
|
Site B 配置
[Interface] Address = 10.0.0.2/24 ListenPort = 51820 PrivateKey = siteB私钥 MTU = 1420 PostUp = iptables -A FORWARD -i wg0 -j ACCEPT PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
[Peer] PublicKey = siteA公钥 AllowedIPs = 10.0.0.1/32, 192.168.1.0/24 Endpoint = siteA公网IP:51820 PersistentKeepalive = 25
|
路由表同步
在 Site A 的局域网设备 上,添加去往 Site B 的路由:
ip route add 192.168.2.0/24 via 192.168.1.1
|
或在路由器上添加静态路由,指向运行 WireGuard 的网关。
验证
路由转发与 NAT
AllowedIPs 详解
AllowedIPs 既是对端的路由规则,也是防火墙规则:
| 写法 |
含义 |
10.0.0.2/32 |
仅允许对端 WireGuard IP |
10.0.0.0/24 |
整个 WireGuard 子网走隧道 |
10.0.0.2/32, 192.168.1.0/24 |
对端 IP + 对端局域网 |
0.0.0.0/0 |
全流量走隧道(VPN 模式) |
全流量 VPN 模式(远程办公)
[Interface] Address = 10.0.0.100/24 DNS = 10.0.0.1 PrivateKey = 客户端私钥
[Peer] PublicKey = 服务器公钥 AllowedIPs = 0.0.0.0/0 Endpoint = 服务器IP:51820 PersistentKeepalive = 25
|
结合 PostUp 添加 NAT 规则,让客户端流量通过服务器上网。
Docker 环境下的配置
Docker 默认创建的 iptables 规则可能干扰 WireGuard。解决方案:
PostUp = iptables -I FORWARD -i wg0 -j ACCEPT PostUp = iptables -I FORWARD -o wg0 -j ACCEPT PostUp = iptables -t nat -I POSTROUTING -o wg0 -j MASQUERADE
|
或者使用 wg-quick 的 Table = auto 功能自动管理路由表。
性能调优
MTU 优化
WireGuard 默认 MTU 为 1420,可根据链路调整:
ping -M do -s 1472 -c 5 10.0.0.2 ping -M do -s 1412 -c 5 10.0.0.2 ping -M do -s 1372 -c 5 10.0.0.2
MTU = 1420
|
| 网络类型 |
推荐 MTU |
| 以太网(无额外封装) |
1420 |
| PPPoE(ADSL 拨号) |
1412 |
| 4G / 5G 蜂窝 |
1372 |
| 叠加 IPsec |
1280 |
多队列
多核 CPU 启用多队列:
ethtool -L eth0 combined 4
|
内核参数优化
net.core.rmem_default = 262144 net.core.wmem_default = 262144 net.core.rmem_max = 4194304 net.core.wmem_max = 4194304 net.ipv4.conf.all.rp_filter = 0 net.ipv4.conf.default.rp_filter = 0 net.ipv4.tcp_congestion_control = bbr
|
安全加固
1. 防火墙规则
iptables -A INPUT -p udp --dport 51820 -j ACCEPT iptables -A INPUT -i wg0 -j ACCEPT iptables -A FORWARD -i wg0 -j ACCEPT
ufw allow 51820/udp
|
2. 限制 Peer 访问
在 AllowedIPs 中仅放行必要的 IP 段,不要滥用 0.0.0.0/0。
3. 密钥管理
wg genkey | tee new_privatekey cat new_privatekey | wg pubkey
wg setconf wg0 /etc/wireguard/wg0.conf
|
4. 使用 Pre-shared Keys(PSK)
在配置中添加对称密钥,增加一层加密:
wg genpsk > psk
[Peer] PresharedKey = 上面生成的 PSK
|
5. 日志监控
modprobe wireguard-dynamic-debug echo module wireguard +p > /sys/kernel/debug/dynamic_debug/control dmesg -w | grep wireguard
journalctl -u wg-quick@wg0 -f
|
PVE 9.2 SDN + WireGuard 集成
PVE 9.2 新增 WireGuard Fabric 支持,可在 Proxmox WebUI 中直接配置 WireGuard 隧道作为 SDN 底层网络。
配置步骤
- Datacenter → SDN → Fabrics → Add →
WireGuard
- 配置 WireGuard Fabric:
- 系统自动生成每个节点的密钥对
- 将 Fabric 关联到 VXLAN Zone,实现跨节点虚拟机二层互通
优势
- WebUI 集中管理,无需手动编辑配置文件
- 自动密钥轮换
- 与 PVE SDN 生态(VXLAN、EVPN)深度集成
- 加密的节点间迁移网络
常用管理命令
wg-quick up wg0 wg-quick down wg0 systemctl restart wg-quick@wg0
wg show wg show wg0 wg showconf wg0
wg show wg0 transfer
watch -n1 wg show
wg set wg0 peer <公钥> endpoint <新IP:端口>
wg set wg0 peer <公钥> remove
|
systemd 服务管理
systemctl enable wg-quick@wg0 systemctl status wg-quick@wg0 journalctl -u wg-quick@wg0 -f
|
排错指南
Q1:wg show 显示对端一直没握手(latest handshake 很久)
iptables -L -n | grep 51820
ping 对端公网IP
|
Q2:能 Ping 通虚拟 IP,但访问对端局域网不通
sysctl net.ipv4.ip_forward
iptables -L FORWARD -n
|
Q3:速度慢
iperf3 -c 对端IP -p 5201
iperf3 -c 10.0.0.2 -p 5201
|
Q4:断线重连慢
Q5:Docker 环境中 WireGuard 不工作
docker run --network host ...
|
总结
WireGuard 的组网模式可以概括为:
点对点 → 两台机器直接互联(基础) ↓ Hub-and-Spoke → 中心节点转发,多 Spoke 互联(最常用) ↓ Site-to-Site → 两个局域网全互联(生产环境) ↓ PVE SDN WireGuard → 虚拟化平台隧道网络(高级)
|
选择指南:
| 场景 |
推荐模式 |
| 两台 VPS 互联 |
点对点 |
| 多台 VPS + 家庭设备 |
Hub-and-Spoke |
| 分公司/异地办公室 |
Site-to-Site |
| PVE 集群跨公网 SDN |
PVE 9.2 WireGuard Fabric |
WireGuard 用极简的设计实现了极高的性能和安全性。配合 PVE 9.2 的原生 SDN 集成,可以轻松构建跨公网的虚拟化集群网络。
本文最后更新于 2026 年 6 月,对应 WireGuard 1.0 稳定版。