WireGuard VPN 配置完全指南

WireGuard VPN 配置完全指南

Someone Lv5

WireGuard 是一款现代、高效且安全的 VPN 协议,以其简洁的代码量(仅约 4000 行)、先进的加密算法和出色的性能表现,已成为 Linux 内核原生支持的 VPN 解决方案。本文将从零开始,详细介绍 WireGuard 的安装、配置和管理。

WireGuard 核心优势

WireGuard 相比传统 VPN(OpenVPN、IPsec)具有以下显著优势:

特性WireGuardOpenVPNIPsec
代码量~4,000 行~600,000+ 行~400,000+ 行
内核集成Linux 5.6+ 原生支持需用户态实现内核模块,配置复杂
加密算法Noise 协议框架 (Curve25519 + ChaCha20 + Poly1305 + BLAKE2s)OpenSSL 多种算法多种算法,协商复杂
连接建立延迟< 100ms(UDP 打洞直连)1-5s(TLS 握手)2-10s(IKE 协商)
配置复杂度极简(密钥对 + 对端配置)复杂(CA 证书体系)复杂(证书或 PSK)
漫游支持原生支持(基于公钥识别连接)需额外配置MOBIKE 扩展支持

工作原理

WireGuard 基于以下核心设计理念:

  • UDP 封装:所有流量封装在 UDP 包中,默认端口 51820
  • 密钥认证:使用 Curve25519 椭圆曲线密钥对进行身份验证
  • 加密隧道:每个数据包使用 ChaCha20 流密码加密 + Poly1305 MAC 认证
  • 无连接状态:服务端不维护连接状态,靠对端公钥识别身份
  • Cryptokey Routing:基于加密密钥的路由表,将公钥与允许的 IP 范围关联

安装 WireGuard

Ubuntu/Debian 系列

1
2
3
4
5
6
7
8
9
# Ubuntu 20.04+ / Debian 11+
sudo apt update
sudo apt install wireguard

# 如果内核版本低于 5.6,需要安装内核模块
sudo apt install wireguard-dkms

# 安装命令行工具(通常已包含)
sudo apt install wireguard-tools

RHEL/CentOS/Rocky/Alma 系列

1
2
3
4
5
6
# CentOS 8+ / Rocky Linux 8+ / AlmaLinux 8+
sudo dnf install elrepo-release epel-release
sudo dnf install kmod-wireguard wireguard-tools

# RHEL 9 / Rocky Linux 9+(内核 5.14 内置支持)
sudo dnf install wireguard-tools

验证安装

1
2
3
4
5
6
# 检查 WireGuard 内核模块
sudo modprobe wireguard
lsmod | grep wireguard

# 检查工具版本
wg --version

密钥生成

WireGuard 使用 Curve25519 椭圆曲线密钥对。每台机器都需要生成自己的密钥对。

1
2
3
4
5
6
7
8
9
10
11
# 生成私钥(并设置安全权限)
wg genkey | sudo tee /etc/wireguard/private.key
sudo chmod 600 /etc/wireguard/private.key

# 从私钥导出公钥
sudo cat /etc/wireguard/private.key | wg pubkey | sudo tee /etc/wireguard/public.key

# 一行命令同时生成并显示
# 私钥保存到文件,公钥输出到终端
umask 077
wg genkey | tee /etc/wireguard/private.key | wg pubkey | tee /etc/wireguard/public.key

安全提示:私钥文件必须设置为 600 权限,建议存储在 /etc/wireguard/ 目录下。

基础网络拓扑

本节以最常见的站点到站点(Hub-and-Spoke)拓扑为例:一台服务器作为 VPN 网关(Hub),客户端设备通过 WireGuard 连接到服务器,实现安全的远程访问。

网络规划

角色公网地址VPN 地址接口
VPN 服务器(Hub)203.0.113.1010.0.0.1/24wg0
客户端 A(笔记本)动态 / NAT10.0.0.2/24wg0
客户端 B(手机)动态 / NAT10.0.0.3/24wg0

服务端配置

服务端位于 /etc/wireguard/wg0.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[Interface]
# 本机 VPN 地址
Address = 10.0.0.1/24

# 监听端口
ListenPort = 51820

# 本机私钥(使用实际生成的私钥)
PrivateKey = <服务端私钥>

# 可选:启用 IP 转发后的 NAT
# 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

# --- 客户端 A ---
[Peer]
# 客户端的公钥
PublicKey = <客户端A的公钥>
# 允许该客户端使用的 VPN IP 地址
AllowedIPs = 10.0.0.2/32

# --- 客户端 B ---
[Peer]
PublicKey = <客户端B的公钥>
AllowedIPs = 10.0.0.3/32

启用 IP 转发

服务端需要开启 IP 转发才能让 VPN 客户端通过服务端访问外部网络:

1
2
3
4
5
6
# 临时启用
sudo sysctl -w net.ipv4.ip_forward=1

# 永久启用
echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

客户端配置

Linux 客户端配置

客户端位于 /etc/wireguard/wg0.conf(或任意路径):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[Interface]
# 本机 VPN 地址
Address = 10.0.0.2/24

# 本机私钥
PrivateKey = <客户端私钥>

# 可选:指定 DNS 服务器(将所有 DNS 查询通过 VPN 转发)
# DNS = 1.1.1.1, 8.8.8.8

# 服务端配置
[Peer]
# 服务端公钥
PublicKey = <服务端公钥>
# 服务端公网地址和端口
Endpoint = 203.0.113.10:51820
# 通过 VPN 路由的流量范围(0.0.0.0/0 = 全流量隧道)
AllowedIPs = 10.0.0.0/24
# 保持连接的保活间隔(秒,用于 NAT 场景)
PersistentKeepalive = 25

AllowedIPs 详解:该参数既充当路由表又充当防火墙规则。

  • 10.0.0.0/24:仅路由 VPN 子网流量
  • 0.0.0.0/0:路由所有 IPv4 流量(全隧道模式)
  • 0.0.0.0/0, ::/0:路由所有 IPv4 和 IPv6 流量

Windows 客户端配置

  1. WireGuard 官网 下载 Windows 客户端
  2. 安装后点击「添加空隧道」
  3. 在接口页填入客户端配置内容
  4. 保存并启用

macOS 客户端配置

1
2
3
4
5
6
# 使用 Homebrew 安装
brew install wireguard-tools

# 客户端配置文件可放在任意位置
# 然后通过 macOS WireGuard 应用导入或命令行启动
sudo wg-quick up /path/to/wg0.conf

手机客户端配置

iOS 和 Android 均可从应用商店搜索 WireGuard 安装,然后通过扫描二维码或导入配置文件快速配置。生成二维码的方法:

1
2
3
4
5
# 安装 qrencode
sudo apt install qrencode

# 生成客户端配置文件的二维码
qrencode -t ansiutf8 < /etc/wireguard/client.conf

启动与管理

启动 WireGuard 接口

1
2
3
4
5
6
7
# 使用 wg-quick 启动(推荐)
sudo wg-quick up wg0

# 使用 systemd 管理(wg0 接口对应 wg-quick@wg0)
sudo systemctl start wg-quick@wg0
sudo systemctl enable wg-quick@wg0 # 开机自启
sudo systemctl status wg-quick@wg0

查看连接状态

1
2
3
4
5
6
7
8
# 查看所有 WireGuard 接口状态
sudo wg show

# 查看特定接口的详细信息
sudo wg show wg0

# 查看传输统计
sudo wg show wg0 transfer

输出示例:

1
2
3
4
5
6
7
8
9
10
interface: wg0
public key: tIxJxxVqCvm...Kx1x7QFxkN0=
private key: (hidden)
listening port: 51820

peer: qa3x7yJzLp...Fg2x9yJzLo=
endpoint: 192.168.1.100:54321
allowed ips: 10.0.0.2/32
latest handshake: 1 minute, 23 seconds ago
transfer: 2.45 MiB received, 1.23 MiB sent

停止与删除

1
2
3
4
5
6
7
8
# 停止接口
sudo wg-quick down wg0

# 禁用开机自启
sudo systemctl disable wg-quick@wg0

# 完全删除接口
sudo ip link delete wg0

高级配置

配置 1:全流量隧道(VPN 代理所有流量)

客户端配置:

1
2
3
4
5
6
7
8
9
10
[Interface]
Address = 10.0.0.2/24
PrivateKey = <客户端私钥>
DNS = 1.1.1.1, 8.8.8.8

[Peer]
PublicKey = <服务端公钥>
Endpoint = 203.0.113.10:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25

服务端需要额外配置 NAT(网络地址转换):

1
2
3
4
[Interface]
...
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

配置 2:点对点直连(Site-to-Site)

两台服务器直接互联的场景:

服务器 A/etc/wireguard/wg0.conf):

1
2
3
4
5
6
7
8
9
[Interface]
Address = 10.0.1.1/30
PrivateKey = <服务器A的私钥>

[Peer]
PublicKey = <服务器B的公钥>
Endpoint = 198.51.100.20:51820
AllowedIPs = 10.0.2.0/24, 10.0.1.0/30
PersistentKeepalive = 25

服务器 B/etc/wireguard/wg0.conf):

1
2
3
4
5
6
7
8
9
[Interface]
Address = 10.0.1.2/30
PrivateKey = <服务器B的私钥>

[Peer]
PublicKey = <服务器A的公钥>
Endpoint = 203.0.113.10:51820
AllowedIPs = 10.0.1.0/24, 10.0.1.0/30
PersistentKeepalive = 25

配置 3:多客户端自动路由(Hub 模式完全配置)

服务端允许客户端之间通信:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <服务端私钥>
# 允许转发
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
# NAT 到外网
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
# 客户端 A
PublicKey = <客户端A公钥>
AllowedIPs = 10.0.0.2/32

[Peer]
# 客户端 B
PublicKey = <客户端B公钥>
AllowedIPs = 10.0.0.3/32

客户端 A 若想与客户端 B 通信,需要在 AllowedIPs 中加入 B 的地址或整个 VPN 子网:

1
2
3
[Peer]
...
AllowedIPs = 10.0.0.0/24 # 允许访问整个 VPN 子网

配置 4:PreSharedKey(预共享密钥)强化

在密钥对认证的基础上,额外增加对称密钥层,提供抗量子计算的前向安全性:

1
2
# 生成预共享密钥(每个 Peer 对可配置不同 PSK)
wg genpsk > /etc/wireguard/psk-peer-a.key

服务端配置中添加:

1
2
3
4
[Peer]
PublicKey = <客户端A公钥>
PresharedKey = <客户端A的预共享密钥>
AllowedIPs = 10.0.0.2/32

客户端对应配置:

1
2
3
4
5
[Peer]
PublicKey = <服务端公钥>
PresharedKey = <与服务端一致的预共享密钥>
Endpoint = 203.0.113.10:51820
AllowedIPs = 10.0.0.0/24

防火墙配置

iptables / nftables

1
2
3
4
5
6
# 允许 WireGuard UDP 端口
sudo iptables -A INPUT -p udp --dport 51820 -j ACCEPT

# 允许 VPN 子网间的转发(Hub 模式)
sudo iptables -A FORWARD -i wg0 -j ACCEPT
sudo iptables -A FORWARD -o wg0 -j ACCEPT

UFW

1
2
3
4
5
6
# 允许 WireGuard 端口
sudo ufw allow 51820/udp

# 如果使用全流量隧道,允许转发
sudo sed -i 's/DEFAULT_FORWARD_POLICY="DROP"/DEFAULT_FORWARD_POLICY="ACCEPT"/' /etc/default/ufw
sudo ufw reload

性能调优

MTU 优化

WireGuard 默认 MTU 为 1420 字节。在某些网络环境下(如 PPPoE),调整 MTU 可以提升性能:

1
2
3
[Interface]
Address = 10.0.0.1/24
MTU = 1280 # 小 MTU 值更稳定,适合移动网络

常见 MTU 参考值:

网络环境推荐 MTU说明
以太网(默认)1420标准 WireGuard 默认值
PPPoE(ADSL)1412PPPoE 头部额外 8 字节开销
移动网络(4G/5G)1280蜂窝网络小 MTU,减少分片
VPN over VPN1200多层隧道,预留更多头部空间

UDP 缓冲区调优

1
2
3
4
5
6
7
8
9
10
# 查看当前 UDP 接收/发送缓冲区大小
sysctl net.core.rmem_default net.core.wmem_default

# 建议生产环境增大缓冲区(减少丢包)
sudo sysctl -w net.core.rmem_max=26214400
sudo sysctl -w net.core.wmem_max=26214400

# 永久生效
echo "net.core.rmem_max = 26214400" | sudo tee -a /etc/sysctl.conf
echo "net.core.wmem_max = 26214400" | sudo tee -a /etc/sysctl.conf

多队列支持(多核 CPU)

对于高带宽场景,创建多个 WireGuard 接口利用多核:

1
2
3
# 绑定到指定 CPU 核心
echo "2" | sudo tee /sys/class/net/wg0/queues/rx-0/rps_cpus
echo "2" | sudo tee /sys/class/net/wg0/queues/tx-0/xps_cpus

错误排查

问题现象可能原因解决方案
wg show 无对端显示服务端未运行或端口不可达检查 systemctl status wg-quick@wg0;确认端口放行
Handshake 始终显示 0 秒防火墙阻止了 UDP 51820检查 iptables/ufw;用 telnet/nmap 测试端口
可以 ping 通 VPN IP 但无法访问外网IP 转发未开启或 NAT 未配置检查 net.ipv4.ip_forward=1;检查 PostUp NAT 规则
客户端能连接但无法相互通信服务端 AllowedIPs 未包含子网服务端 AllowedIPs 设为 10.0.0.0/24
连接频繁断开NAT 超时 / PersistentKeepalive 未设置设置 PersistentKeepalive = 25
速度慢MTU 不匹配或 UDP 缓冲区不足调小 MTU(1280);增大 rmem_max
重启后无法自动连接systemd 服务未启用sudo systemctl enable wg-quick@wg0

调试命令

1
2
3
4
5
6
7
8
9
10
11
12
# 实时查看 WireGuard 日志
sudo journalctl -f -u wg-quick@wg0

# 测试端口连通性(从客户端)
nc -uv 203.0.113.10 51820
# 输入任意字符后按 Ctrl+C,服务端应显示握手

# 抓包调试
sudo tcpdump -i eth0 udp port 51820 -n

# 查看路由表
ip route show table all | grep wg0

安全最佳实践

  1. 最小权限原则:每个客户端只允许访问所需的 IP 范围,使用最精确的 AllowedIPs
  2. 定期轮换密钥:建议每 6-12 个月更换一次密钥对
  3. 使用 PreSharedKey:为每个 Peer 配置独立的预共享密钥,增强前向安全性
  4. 防火墙白名单:仅允许已知对端 IP 访问 WireGuard 端口
  5. 日志审计:启用审计日志记录连接事件
  6. 禁用未使用的接口:不使用 WireGuard 时关闭接口
  7. 内核更新:保持内核版本最新,WireGuard 的安全性与内核版本紧密相关
  8. 限制监听地址:如果服务器有多网卡,指定仅监听内网 IP
1
2
3
4
[Interface]
# 仅在内网 IP 上监听(暴露面更小)
ListenPort = 51820
BindAddress = 10.0.0.1:51820

一键部署脚本

以下是一个完整的服务端快速部署脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
# WireGuard 服务端一键部署脚本
# 适用于 Ubuntu 20.04+ / Debian 11+

set -e

# 配置
VPN_SUBNET="10.0.0.0/24"
SERVER_VPN_IP="10.0.0.1/24"
SERVER_PUBLIC_INTERFACE=$(ip route get 8.8.8.8 | awk '{print $5}' | head -n1)
WG_PORT=51820

echo "=== WireGuard 服务端部署 ==="

# 1. 安装
echo "[1/5] 安装 WireGuard..."
apt update -qq
apt install -y wireguard qrencode

# 2. 启用 IP 转发
echo "[2/5] 启用 IP 转发..."
sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf

# 3. 生成密钥
echo "[3/5] 生成服务端密钥对..."
umask 077
wg genkey | tee /etc/wireguard/server.key | wg pubkey | tee /etc/wireguard/server.pub

# 4. 生成配置
echo "[4/5] 生成服务端配置..."
cat > /etc/wireguard/wg0.conf << EOF
[Interface]
Address = ${SERVER_VPN_IP}
ListenPort = ${WG_PORT}
PrivateKey = $(cat /etc/wireguard/server.key)
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ${SERVER_PUBLIC_INTERFACE} -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o ${SERVER_PUBLIC_INTERFACE} -j MASQUERADE
EOF

# 5. 启动并启用
echo "[5/5] 启动 WireGuard..."
systemctl start wg-quick@wg0
systemctl enable wg-quick@wg0

echo "=== 部署完成 ==="
echo "服务端公钥: $(cat /etc/wireguard/server.pub)"
echo "服务端地址: $(curl -s ifconfig.me):${WG_PORT}"
echo ""
echo "客户端配置示例:"
cat << EOF
[Interface]
Address = 10.0.0.2/24
PrivateKey = <客户端私钥>
DNS = 1.1.1.1

[Peer]
PublicKey = $(cat /etc/wireguard/server.pub)
Endpoint = $(curl -s ifconfig.me):${WG_PORT}
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF

总结

WireGuard 以其简洁的设计、卓越的性能和强大的安全性,已成为现代 VPN 部署的首选方案。无论是搭建远程办公 VPN、连接多云环境,还是保护个人隐私,WireGuard 都能胜任。本文覆盖了从安装配置到生产部署的全流程,希望能帮助读者快速上手并深入掌握 WireGuard 的使用。

本文由AI辅助生成,内容仅供参考

  • 标题: WireGuard VPN 配置完全指南
  • 作者: Someone
  • 创建于 : 2026-06-12 16:01:00
  • 更新于 : 2026-06-18 08:39:57
  • 链接: https://demo-blog.qusite.cn/2026-06-12-wireguard-vpn-guide/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。