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)

# WireGuard 已内置于 Linux 5.6+ 内核
# 只需安装用户态工具
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 优化
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   # 在节点 A 上
ping 10.0.0.1 # 在节点 B 上

如果 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

# 开启 IP 转发(重要)
# 同时需要在 sysctl 中设置 net.ipv4.ip_forward = 1

[Peer]
# VPS 香港
PublicKey = hk公钥
AllowedIPs = 10.0.0.2/32

[Peer]
# 家庭 NAS
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:

# 在 VPS 香港上
ping 10.0.0.3 # 访问家庭 NAS
ping 192.168.1.100 # 访问 NAS 后的局域网设备(如果配置了路由)

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; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[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; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
PublicKey = siteA公钥
AllowedIPs = 10.0.0.1/32, 192.168.1.0/24
Endpoint = siteA公网IP:51820
PersistentKeepalive = 25

路由表同步

Site A 的局域网设备 上,添加去往 Site B 的路由:

# 在 PC-A 上
ip route add 192.168.2.0/24 via 192.168.1.1

或在路由器上添加静态路由,指向运行 WireGuard 的网关。

验证

# 在 Site A 的 PC-A 上
ping 192.168.2.20 # 直通 Site B 的内网设备

路由转发与 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。解决方案:

# 在 wg0.conf 中添加 PostUp 规则
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

# 或者让 WireGuard 容器使用 host 网络模式

或者使用 wg-quickTable = auto 功能自动管理路由表。


性能调优

MTU 优化

WireGuard 默认 MTU 为 1420,可根据链路调整:

# 测试不同 MTU(在两端执行)
ping -M do -s 1472 -c 5 10.0.0.2 # 有线网络
ping -M do -s 1412 -c 5 10.0.0.2 # PPPoE
ping -M do -s 1372 -c 5 10.0.0.2 # 蜂窝网络

# 在 [Interface] 中设置
MTU = 1420
网络类型 推荐 MTU
以太网(无额外封装) 1420
PPPoE(ADSL 拨号) 1412
4G / 5G 蜂窝 1372
叠加 IPsec 1280

多队列

多核 CPU 启用多队列:

ethtool -L eth0 combined 4     # 物理网卡多队列
# WireGuard 自动利用多队列(内核态优势)

内核参数优化

# /etc/sysctl.d/99-wireguard.conf
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. 防火墙规则

# 只放行 WireGuard 端口
iptables -A INPUT -p udp --dport 51820 -j ACCEPT
iptables -A INPUT -i wg0 -j ACCEPT
iptables -A FORWARD -i wg0 -j ACCEPT

# 或者使用 ufw
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)

在配置中添加对称密钥,增加一层加密:

# 生成 PSK
wg genpsk > psk

# Peer 中引用
[Peer]
PresharedKey = 上面生成的 PSK

5. 日志监控

# 查看 WireGuard 日志(内核级)
modprobe wireguard-dynamic-debug
echo module wireguard +p > /sys/kernel/debug/dynamic_debug/control
dmesg -w | grep wireguard

# 或使用 systemd journal
journalctl -u wg-quick@wg0 -f

PVE 9.2 SDN + WireGuard 集成

PVE 9.2 新增 WireGuard Fabric 支持,可在 Proxmox WebUI 中直接配置 WireGuard 隧道作为 SDN 底层网络。

配置步骤

  1. Datacenter → SDNFabricsAddWireGuard
  2. 配置 WireGuard Fabric:
    • 端口:51820
    • 节点列表:选择参与隧道的节点
  3. 系统自动生成每个节点的密钥对
  4. 将 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

# 更新 Peer 配置(不重启)
wg set wg0 peer <公钥> endpoint <新IP:端口>

# 删除 Peer
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 很久)

# 检查防火墙是否放行 UDP 51820
iptables -L -n | grep 51820

# 检查对端 Endpoint 配置是否正确
# 确认公网 IP 可达
ping 对端公网IP

# 检查 NAT 环境是否需 PersistentKeepalive
# 两端都设置 PersistentKeepalive = 25

Q2:能 Ping 通虚拟 IP,但访问对端局域网不通

# 检查 IP 转发是否开启
sysctl net.ipv4.ip_forward

# 检查 iptables FORWARD 规则
iptables -L FORWARD -n

# 检查 AllowedIPs 是否包含对端局域网段
# 例如:AllowedIPs = 10.0.0.2/32, 192.168.2.0/24

# 检查对端是否需要添加回程路由
# Site B 也需要在路由表中知道 Site A 的局域网段

Q3:速度慢

# 测试裸 TCP 速度(先排除 WireGuard)
iperf3 -c 对端IP -p 5201

# 测试 WireGuard 隧道速度
iperf3 -c 10.0.0.2 -p 5201

# 检查 MTU
# 尝试降低 MTU 到 1280

# 检查 CPU 是否瓶颈
# WireGuard 是 CPU 密集型,弱 CPU 建议单条隧道

Q4:断线重连慢

# 降低 PersistentKeepalive 间隔
PersistentKeepalive = 10

# 检查 UDP 超时设置
# 某些运营商 UDP 超时短(如移动宽带)

Q5:Docker 环境中 WireGuard 不工作

# 方案一:使用 host 网络模式
docker run --network host ...

# 方案二:开放必要端口
# 注意 Docker 的 iptables 规则会覆盖 WireGuard 的
# 在 PostUp 中手动插入规则到 DOCKER-USER 链

# 方案三:将 WireGuard 运行在宿主机,而非容器内

总结

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 稳定版。