Skip to content

WireGuard 安装与配置指南 ​

目录 ​

  1. WireGuard 简介
  2. 服务端部署
  3. 客户端配置
  4. 最佳实践
  5. 故障排查

WireGuard 简介 ​

什么是 WireGuard ​

WireGuard 是近几年计算机网络界最具颠覆性的开源隧道协议。由于其架构极其精简、速度飞快且安全性极高,它在 Linux 5.6 版本中被直接合并进了 Linux 内核主线。

核心设计理念 ​

WireGuard 彻底抛弃了传统 VPN(如 OpenVPN)的复杂配置,引入了 Cryptokey Routing(加密键路由) 概念:

  • 每个设备都有一个公钥和私钥(类似 SSH Key)
  • 通过将公钥添加到对端配置中来完成授权(类似 authorized_keys)
  • 配置极其简单,一个配置文件即可完成全部设置

为什么选择 WireGuard ​

针对"公网服务器 + 家庭动态 IP + 长时间保持长连接"需求,WireGuard 拥有三个天然的绝对优势:

1. 无缝支持"动态 IP 漂移"(连接永不断开) ​

WireGuard 是一种无状态(Stateless)协议。在它的世界里,没有传统意义上的"建立连接"和"断开连接"。

  • 传统 VPN:如果家里公网 IP 变了,TCP 连接断开,客户端必须重新进行握手、身份验证,长连接必然中断。
  • WireGuard:只要服务器收到带有正确密钥和签名的数据包,它就会自动更新对端的 IP 地址,流量会自动漂移过去。

2. 专门对付运营商的「心跳保活」机制 ​

在家庭场景下,最怕运营商因为链路闲置而主动回收 NAT 会话。WireGuard 原生提供了一个杀手级参数:

ini
PersistentKeepalive = 25

每隔 25 秒会自动发送一个微小的探测包,这能死死撑住运营商的 NAT 路由表,保证长连接几个月都不掉线。

3. 内核级运行,性能拉满 ​

  • OpenVPN 等协议:运行在用户态,数据包需要在内核态和用户态之间来回复制,CPU 消耗大,延迟高。
  • WireGuard:直接常驻在 Linux 内核中,几乎不占用多余的内存,吞吐量接近物理网卡的极限,延迟极低。

服务端部署 ​

环境要求 ​

  • 一台有公网 IP 的服务器(Linux)
  • 内核版本 >= 5.6(已内置 WireGuard 支持)

一键脚本部署(推荐) ​

使用一键脚本可以快速完成 WireGuard 的安装、配置和客户端管理。

1. 下载并运行脚本 ​

bash
# 下载脚本
# 通过国内加速镜像下载
curl -L https://ghproxy.net/https://raw.githubusercontent.com/nyr/wireguard-install/master/wireguard-install.sh -o wireguard-install.sh

# 赋予执行权限
chmod +x wireguard-install.sh

# 运行脚本(需要 root 权限)
sudo ./wireguard-install.sh

2. 脚本交互配置 ​

脚本运行后会提示你进行以下配置:

  1. 公网 IP 或域名:输入服务器的公网 IP 或域名(用于客户端配置文件)
  2. 服务器端口:默认 51820(UDP 协议)
  3. 客户端 DNS:默认 8.8.8.8,8.8.4.4
  4. 客户端名称:第一个客户端的名称(例如:my-laptop)

脚本会自动完成以下操作:

  • 安装 WireGuard 及其依赖
  • 生成服务器和客户端的密钥对
  • 创建 /etc/wireguard/wg0.conf 配置文件
  • 配置 IP 转发和网络防火墙规则
  • 启动 WireGuard 服务
  • 生成客户端配置文件(.conf)

3. 添加更多客户端 ​

如果需要添加更多客户端,再次运行脚本:

bash
sudo ./wireguard-install.sh

选择选项 Add a new client,输入客户端名称即可。

4. 查看客户端配置 ​

客户端配置文件保存在:

bash
/etc/wireguard/clients/
├── my-laptop.conf
├── my-phone.conf
└── my-desktop.conf

你可以通过以下方式获取配置文件:

  • 直接读取文件内容:cat /etc/wireguard/clients/my-laptop.conf
  • 使用 SCP/SFTP 下载到本地
  • 生成二维码:qrencode -t ansiutf8 < /etc/wireguard/clients/my-laptop.conf

客户端配置 ​

使用上面的脚本一键安装后会默认添加一个client,生成一个.conf文件。可以把.conf下载到客户端,直接使用

配置文件结构 ​

客户端的配置文件(例如 my-laptop.conf)通常如下:

ini
[Interface]
Address = 10.7.0.x/24, fddd:2c4:2c4:2c4::x/64
DNS = 119.29.29.29, 223.5.5.5
PrivateKey = +MziJe6dOyLU+nJ1LP=

[Peer]
PublicKey = yCU79K9Dcm7Zd/lJ3A=
PresharedKey = V0hjM5er/k=
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = x.x.x.x:51820
PersistentKeepalive = 25

注意:AllowedIPs默认是0.0.0.0/0,这会修改系统的默认路由表,把所有的流量都走W7转发出去会导致网络不通,这个时候需要修改一下AllowedIPs为VPN的私有网段。如:

AllowedIPs = 10.7.0.0/16, fddd:2c4:2c4:2c4::/64

Linux 安装客户端 ​

Ubuntu / Debian:

bash
sudo apt update
sudo apt install wireguard wireguard-tools

CentOS / Rocky Linux / OpenCloudOS:

bash
sudo dnf install wireguard-tools -y

配置文件路径

linux默认配置文件路径为:/etc/wireguard/wg0.conf

启动 WireGuard

bash
sudo systemctl start wg-quick@wg0
  1. 设置开机自启:
bash
sudo systemctl enable wg-quick@wg0

验证连接 ​

bash
# 查看 WireGuard 状态
sudo wg show

# 测试连接
ping 10.8.0.1

# 查看路由
ip route show

Windows 客户端 ​

安装 ​

  1. 访问 WireGuard 官网
  2. 下载 Windows 安装包并安装

配置 ​

  1. 打开 WireGuard 客户端
  2. 点击"添加隧道" → "从文件导入"
  3. 选择配置文件(例如 my-laptop.conf)
  4. 导入成功后,选中隧道,点击"激活"

验证连接 ​

powershell
# 在 PowerShell 中测试连接
ping 10.8.0.1

macOS 客户端 ​

安装 ​

方法一:App Store

  1. 打开 App Store
  2. 搜索 "WireGuard"
  3. 下载并安装

方法二:Homebrew

bash
brew install --cask wireguard

配置 ​

  1. 打开 WireGuard 客户端
  2. 点击"+" → "从文件导入"
  3. 选择配置文件
  4. 导入成功后,点击开关激活

验证连接 ​

bash
# 在终端中测试连接
ping 10.8.0.1

Android 客户端 ​

安装 ​

  1. 打开 Google Play Store
  2. 搜索 "WireGuard"
  3. 下载并安装官方客户端

配置 ​

方法一:扫描二维码

  1. 在服务器上生成二维码:
bash
sudo qrencode -t ansiutf8 < /etc/wireguard/clients/my-phone.conf
  1. 打开 WireGuard 客户端
  2. 点击"+" → "扫描二维码"
  3. 扫描服务器上显示的二维码

方法二:从文件导入

  1. 将配置文件传输到手机(例如通过邮件、云盘)
  2. 打开 WireGuard 客户端
  3. 点击"+" → "从文件导入"
  4. 选择配置文件

验证连接 ​

  1. 导入成功后,点击开关激活
  2. 打开浏览器,访问 http://10.8.0.1(如果服务器有 Web 服务)

iOS 客户端 ​

安装 ​

  1. 打开 App Store
  2. 搜索 "WireGuard"
  3. 下载并安装官方客户端

配置 ​

方法一:扫描二维码

  1. 在服务器上生成二维码(参考 Android 部分)
  2. 打开 WireGuard 客户端
  3. 点击"+" → "扫描二维码"

方法二:从文件导入

  1. 将配置文件传输到手机(例如通过 AirDrop、iCloud Drive)
  2. 打开 WireGuard 客户端
  3. 点击"+" → "从文件导入"

验证连接 ​

  1. 导入成功后,点击开关激活
  2. 打开浏览器,访问内网资源

Docker 客户端(用于家庭软路由/NAS) ​

如果你想在家庭环境的设备上使用 Docker 运行 WireGuard 客户端,可以按照以下方式部署:

目录结构 ​

wireguard-client/
├── docker-compose.yml
└── config/
    └── wg0.conf      <-- 将客户端配置文件放入

docker-compose.yml ​

yaml
version: '3.8'

services:
  wg-client:
    image: lscr.io/linuxserver/wireguard:latest
    container_name: wg-client
    restart: unless-stopped
    cap_add:
      - NET_ADMIN
      - SYS_MODULE
    sysctls:
      - net.ipv4.conf.all.src_valid_mark=1
      - net.ipv4.ip_forward=1
    volumes:
      # 将包含 wg0.conf 的 config 文件夹映射到容器内
      - ./config:/config
      # 映射宿主机的内核模块目录,确保容器能调用 WireGuard 内核
      - /lib/modules:/lib/modules
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Asia/Shanghai

启动客户端 ​

bash
cd wireguard-client
docker compose up -d

验证连接 ​

bash
# 查看容器日志
docker logs -f wg-client

# 进入容器测试连接
docker exec -it wg-client ping 10.8.0.1

最佳实践 ​

1. 保持长连接稳定 ​

在客户端配置文件中添加:

ini
[Peer]
PublicKey = ...
AllowedIPs = 0.0.0.0/0
Endpoint = 你的服务器公网IP:51820
# 每 25 秒发送一个保活包,确保 NAT 穿透
PersistentKeepalive = 25

2. 安全性建议 ​

  • 定期轮换密钥:定期更新客户端和服务端的密钥对
  • 使用强密钥:WireGuard 的密钥是随机生成的,确保使用 wg genkey 生成
  • 限制客户端数量:定期清理不再使用的客户端配置
  • 防火墙规则:只开放必要的端口(51820/UDP)
  • 禁用 root 登录:确保服务器不允许 root 用户直接登录

3. 性能优化 ​

  • MTU 调整:如果遇到网络不稳定,可以尝试调整 MTU 值(通常设置为 1420 或更小)
  • 使用内核原生 WireGuard:确保 Linux 内核版本 >= 5.6,以获得最佳性能
  • 选择合适的 DNS:根据地理位置选择延迟最低的 DNS 服务器
  • 启用 QoS:如果网络带宽有限,可以配置 QoS 策略

4. 多客户端管理 ​

如果需要在服务器上管理多个客户端,可以在 /etc/wireguard/wg0.conf 中添加多个 [Peer] 段:

ini
[Interface]
PrivateKey = <服务器私钥>
Address = 10.8.0.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
# 客户端 1
PublicKey = <客户端1公钥>
AllowedIPs = 10.8.0.2/32
PersistentKeepalive = 25

[Peer]
# 客户端 2
PublicKey = <客户端2公钥>
AllowedIPs = 10.8.0.3/32
PersistentKeepalive = 25

[Peer]
# 客户端 3
PublicKey = <客户端3公钥>
AllowedIPs = 10.8.0.4/32
PersistentKeepalive = 25

每个客户端的配置文件中的 Address 需要对应上述 AllowedIPs。


故障排查 ​

1. 无法连接 ​

检查清单:

  1. 检查服务器防火墙/安全组是否放行了 UDP 51820 端口
  2. 检查 WireGuard 服务是否正常运行:sudo wg show
  3. 检查服务器公网 IP 是否正确(如果 IP 变化,需要更新客户端配置)
  4. 检查客户端和服务端的系统时间是否同步(误差不能太大)

调试命令:

bash
# 在服务器端查看 WireGuard 状态
sudo wg show

# 在服务器端查看接口状态
ip addr show wg0

# 在服务器端查看路由
ip route show

# 在客户端测试连接
ping 10.8.0.1

2. 连接成功但无法访问内网资源 ​

检查清单:

  1. 检查服务端的 net.ipv4.ip_forward 是否设置为 1:sysctl net.ipv4.ip_forward
  2. 检查服务端的防火墙规则是否正确(NAT 转发)
  3. 检查客户端的 AllowedIPs 是否配置正确
  4. 如果需要访问整个内网,设置 AllowedIPs = 0.0.0.0/0

调试命令:

bash
# 在服务器端查看 NAT 规则
sudo iptables -t nat -L -n -v

# 在客户端查看路由
ip route show

# 在客户端测试 DNS
nslookup google.com

3. 连接频繁断开 ​

检查清单:

  1. 检查 PersistentKeepalive 是否设置(建议设置为 25)
  2. 检查运营商 NAT 超时时间,适当调整保活间隔
  3. 查看服务端和客户端的系统日志:journalctl -u wg-quick@wg0 -f

优化建议:

  • 增加 PersistentKeepalive 的频率(例如设置为 10 秒)
  • 检查网络稳定性(例如使用 mtr 工具)
  • 尝试更换服务器端口(有些 ISP 会限制 UDP 流量)

4. 速度慢 ​

检查清单:

  1. 检查 MTU 设置:默认是 1420,如果网络环境有特殊限制,可以尝试调小
  2. 检查服务器和客户端的带宽限制
  3. 检查是否有 QoS 策略影响速度

优化建议:

在客户端配置文件中添加 MTU 设置:

ini
[Interface]
PrivateKey = ...
Address = 10.8.0.2/24
DNS = 8.8.8.8
# 调整 MTU(如果出现分包问题)
MTU = 1280

5. 日志查看 ​

服务器端日志:

bash
# 查看 WireGuard 服务日志
sudo journalctl -u wg-quick@wg0 -f

# 查看系统日志
sudo dmesg | grep wireguard

客户端日志:

  • Windows/macOS:在客户端界面中查看日志
  • Linux:sudo journalctl -u wg-quick@wg0 -f
  • Android/iOS:在客户端设置中启用日志,然后查看

总结 ​

WireGuard 是目前最先进、最高效的 VPN 协议之一,特别适合需要稳定长连接、动态 IP 的场景。直接在宿主机上部署 WireGuard 可以获得最佳的性能和稳定性。

推荐阅读: