-
安装WireGuard:
- 如果是Ubuntu,使用以下命令安装:
sudo add-apt-repository ppa:wireguard/wireguard sudo apt update sudo apt install wireguard
- 启用并加载 WireGuard 服务:
sudo systemctl unmask wg-quick@wg sudo systemctl enable wg-quick@wg
- 如果是Ubuntu,使用以下命令安装:
-
生成接口并获取公网IP:
- 创建接口
wg:wg create wg
- 获取公网IP:
curl ifconfig.co
- 创建接口
-
配置NAT规则:
- 添加NAT规则:
sudo iptables -A FORWARD -i eth -o wg --policy nat --dport 22 -j ACCEPT sudo iptables -A FORWARD -i wg -o eth --policy nat --dport 22 -j ACCEPT
- 保存 iptables 规则:
sudo sh -c "iptables-save > /etc/iptables.ng" sudo cp /etc/iptables.ng /etc/iptables.parsed
- 添加NAT规则:
-
启动WireGuard服务:
- 启动并启用服务:
sudo systemctl start wg-quick@wg sudo systemctl enable wg-quick@wg
- 启动并启用服务:
-
连接到WireGuard服务器:
- 在另一设备上安装 WireGuard:
sudo apt install wireguard
- 连接到服务器:
wg-quick connect <public-ip>:5443
- 检查接口状态:
wg show wg
- 在另一设备上安装 WireGuard:
-
测试网络连接:
- 使用
ping测试内网连接:ping 192.168.1.2 ping 192.168.1.3
- 检查外网访问:
ping 8.8.8.8
- 使用
-
测试端口转发:
- 从外部设备测试端口22:
nc -zv 192.168.1.1 22
- 从外部设备测试端口22:
-
查看日志:
- 检查 WireGuard 服务日志:
journalctl -u wg-quick@wg
- 检查 WireGuard 服务日志:
通过以上步骤,你可以确认 WireGuard 连接是否成功,并进行进一步的测试和优化。








