一、✅ 服务器 wg0.conf 正确示例

[Interface]
Address = 192.168.6.1/24
PrivateKey = <服务器私钥>
ListenPort = 51820

[Peer]
PublicKey = <iOS 客户端公钥>
AllowedIPs = 192.168.6.2/32
PersistentKeepalive = 30  # iOS 可能需要这个保持连接

[Peer]
PublicKey = <PC 客户端公钥>
AllowedIPs = 192.168.6.3/32
PersistentKeepalive = 30  # PC 端可以加这个,避免 NAT 断连

没有 Endpoint,WireGuard 服务器是被动监听的,不主动连接客户端,客户端需要 Endpoint(服务器的公网 IP 或域名),服务器不需要 Endpoint。服务器会自动学习客户端的 IP,当客户端连接服务器时,服务器会自动记录客户端的 公网 IP 和端口,之后服务器就知道如何向客户端发送数据,无需手动指定 Endpoint。

二、✅ PC 客户端 wg0.conf 示例

[Interface]
PrivateKey = <PC 客户端私钥>
Address = 192.168.6.3/24
DNS = 10.10.10.253

[Peer]
PublicKey = <服务器公钥>
AllowedIPs = 0.0.0.0/0  # 全局代理(如果只访问内网,改成 192.168.6.0/24)
Endpoint = a.b.com:51820
PersistentKeepalive = 30  # 避免 NAT 断连

客户端必须有 Endpoint,否则无法找到服务器!

📌 什么时候服务器端需要 Endpoint

  • 只有当服务器端作为客户端去连接另一个 WireGuard 服务器(双向 VPN)时,才需要 Endpoint
  • 一般的服务器端监听模式,不需要 Endpoint
  • 客户端 (wg-client.conf) 必须有 Endpoint,否则找不到服务器
  • 服务器 (wg0.conf) 不需要 Endpoint,因为它是被动监听的

三、✅ 服务器开启 IP 转发

如果 PC 连接后可以访问公网,但不能访问内网,可能是服务器没有启用 IP 转发。执行:

echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

⚠ IPv6 也要打开(如果用 IPv6):

echo "net.ipv6.conf.all.forwarding = 1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

四、✅ 服务器添加 NAT 规则

PC 连接后无法访问 192.168.6.0/24,可能是 缺少 NAT 转换。在服务器上运行:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i wg0 -o eth0 -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o wg0 -m state --state RELATED,ESTABLISHED -j ACCEPT

eth0 替换成你服务器的实际网卡名称(运行 ip a 查看)。

让规则永久生效:

sudo apt install iptables-persistent
sudo netfilter-persistent save

五、✅ 服务器添加 NAT 规则

如果希望 PC 走全局流量并访问内网,可以尝试:

[Peer]
AllowedIPs = 0.0.0.0/0, ::/0, 192.168.6.0/24

这样设置后:

  • 0.0.0.0/0, ::/0 让所有公网流量走 WireGuard
  • 192.168.6.0/24 确保可以访问 WireGuard 服务器的内网设备
如果觉得我的文章对你有用,请随意赞赏