安装 WireGuard

安装 WireGuard:

sudo apt update
sudo apt install wireguard

生成 WireGuard 服务器配置文件

创建 WireGuard 服务器的配置文件:

sudo wgserver --eth eth --address 192.168.4./24

服务器配置文件会出现在 /etc/wg/ 目录中,wg.conf

设置服务器权限

确保服务器配置文件权限适当:

sudo chmod 600 /etc/wg/wg.conf

生成客户端配置文件

在另一台计算机上,运行以下命令,生成客户端配置文件:

wgclient -i 192.168.4.1 -o myclient.key

设置 IP 转发(服务器)

编辑 sysctl.conf 并添加以下内容:

sudo nano /etc/sysctl.conf

添加以下行:

net.ipv4.ip_forward=1
net.ipv6.dev=1

然后重启网络接口:

sudo reboot -f

设置 IP 转发(客户端)

在客户端上,同样编辑 sysctl.conf 并添加:

sudo nano /etc/sysctl.conf

添加以下行:

net.ipv4.ip_forward=1

然后重启网络接口:

sudo reboot -f

启动 WireGuard 服务

在服务器上启动 WireGuard 服务:

sudo systemctl unmask wg-quick@wg.service
sudo systemctl enable wg-quick@wg.service
sudo systemctl start wg-quick@wg.service

连接客户端

在客户端上连接到服务器:

sudo wg-quick up wg

测试连接(服务器)

检查服务器状态:

sudo wg show

测试连接(客户端)

检查客户端连接状态:

sudo wg show server

测试网络连接

测试通过 WireGuard 是否能访问服务器:

ping 192.168.4.1

或者通过 SSH 访问:

ssh 192.168.4.1

备用配置(可选)

  • 生成固定私钥:

    wgserver --eth eth --address 192.168.4./24 --fixed-private-key fixed-private-key
  • 绑定特定端口:

    sudo wg-quick bind 192.168.4.1:51820

使用 wireguardctl (可选)

管理客户端连接:

sudo apt install wireguardctl
wireguardctl c peer peer @myclient.key

故障排除

  • 检查日志:

    服务器:tail -f /var/log/auth.log

    客户端:tail -f /var/log/wireguard/wg.log

  • 检查防火墙:

    sudo iptables -L -n
  • 重启服务:

    服务器:

    sudo systemctl restart wg-quick@wg.service

    客户端:

    sudo systemctl restart wg-quick@wg.service

通过以上步骤,您应该能够成功配置并连接到 WireGuard 服务器,确保所有步骤正确执行,并根据需要调整配置以满足您的网络需求。

配置 WireGuard 的步骤如下

扫码添加小飞机VPN网络工具官方微信

扫码添加小飞机VPN网络工具官方微信

400-638-2751
扫码添加小飞机VPN网络工具官方微信

扫码添加小飞机VPN网络工具官方微信

网站地图