解决 WireGuard 报错 Handshake did not complete after 5 seconds 修复
排查 WireGuard 隧道无法建立报 Handshake did not complete after 5 seconds,分析 UDP 监听、NAT 穿越与 PersistentKeepalive。
问题现象
启动 WireGuard 接口后,客户端无法收发数据,执行 wg show 显示:
peer: <Public_Key>
endpoint: 198.51.100.1:51820
latest handshake: (never)
transfer: 0 B received, 1.48 KiB sent
触发根因
- 服务端的 UDP 51820 端口被云服务商安全组或 iptables 拦截;
- 对端处于 NAT 内网之后且未配置保活:路由器 NAT 会话在几十秒后超时关闭;
- 两端的 Public Key 与 Private Key 错位配对。
解决步骤
在服务端放行 UDP 端口
确保云安全组与操作系统防火墙放行 UDP 流量:
sudo ufw allow 51820/udp
在客户端配置 PersistentKeepalive
编辑客户端 wg0.conf,在 [Peer] 块添加保活探针:
[Peer]
PublicKey = <Server_Public_Key>
Endpoint = 198.51.100.1:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
验证确认
重启接口并检查状态:
sudo wg-quick down wg0 && sudo wg-quick up wg0
sudo wg show
看到 latest handshake: 2 seconds ago 且两端 received / sent 流量持续递增。


