WireGuard
更新于 2025/09/30
简介
WireGuard 是一个现代化的 VPN 协议,具有以下特点:
- 性能出色,速度快
- 代码简洁,易于审计
- 加密强度高,使用最新的加密技术
- 配置简单,易于部署
官网
安装
Ubuntu/Debian
| Bash |
|---|
| sudo apt update
sudo apt install wireguard
|
CentOS/RHEL
| Bash |
|---|
| sudo yum install epel-release
sudo yum install wireguard-tools
|
其他客户端
官方下载
配置示例
点对点
| Text Only |
|---|
| [客户端1] WireGuard: 192.168.2.2/32
|
|
[客户端2] WireGuard: 192.168.2.3/32
|
客户端1配置 (peer1.conf)
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | [Interface]
# 客户端1 IP
Address = 192.168.2.2/32
PrivateKey = client1PrivateKeyAbcAbcAbc=
# 监听端口(可选,用于被动连接)
ListenPort = 51821
[Peer]
# 客户端2的公钥
PublicKey = client2PublicKeyAbcAbcAbc=
# 允许来自客户端2的流量
AllowedIPs = 192.168.2.3/32
# 客户端2的监听地址和端口
Endpoint = client2.example.com:51822
PersistentKeepalive = 25
|
客户端2配置 (peer2.conf)
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | [Interface]
# 客户端2 IP
Address = 192.168.2.3/32
PrivateKey = client2PrivateKeyAbcAbcAbc=
# 监听端口
ListenPort = 51822
[Peer]
# 客户端1的公钥
PublicKey = client1PublicKeyAbcAbcAbc=
# 允许来自客户端1的流量
AllowedIPs = 192.168.2.2/32
# 客户端1的监听地址和端口
Endpoint = client1.example.com:51821
PersistentKeepalive = 25
|
注意: 点对点模式下,两端都需要有公网IP或端口转发,或者其中一端有固定的公网访问方式。如果都在NAT后面,需要借助打洞技术或中继服务器。
同网段转发
| Text Only |
|---|
| [客户端1] WireGuard: 192.168.2.2/32
|
|
|
[公网] 公网IP: 1.2.3.4
| WireGuard: 192.168.2.1/24
| ListenPort: 51820
|
[客户端2] WireGuard: 192.168.2.3/32
|
服务端配置 (server.conf)
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | [Interface]
# 服务端 IP
Address = 192.168.2.1/24
PrivateKey = serverPrivateKeyAbcAbcAbc=
ListenPort = 51820
[Peer]
# 客户端1
PublicKey = client1PublicKeyAbcAbcAbc=
AllowedIPs = 192.168.2.2/32
[Peer]
# 客户端2
PublicKey = client2PublicKeyAbcAbcAbc=
AllowedIPs = 192.168.2.3/32
|
客户端1配置 (client1.conf)
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | [Interface]
# 客户端1 IP
Address = 192.168.2.2/32
PrivateKey = client1PrivateKeyAbcAbcAbc=
DNS = 8.8.8.8
[Peer]
# 服务端公钥
PublicKey = serverPublicKeyAbcAbcAbc=
# 仅路由同网段流量
AllowedIPs = 192.168.2.0/24
# 服务端地址和端口
Endpoint = server.example.com:51820
PersistentKeepalive = 25
|
客户端2配置 (client2.conf)
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | [Interface]
# 客户端2 IP
Address = 192.168.2.3/32
PrivateKey = client2PrivateKeyAbcAbcAbc=
DNS = 8.8.8.8
[Peer]
# 服务端公钥
PublicKey = serverPublicKeyAbcAbcAbc=
# 仅路由同网段流量
AllowedIPs = 192.168.2.0/24
# 服务端地址和端口
Endpoint = server.example.com:51820
PersistentKeepalive = 25
|
说明: 在同网段转发模式下,客户端之间可以直接通信,所有客户端流量都通过中心服务器转发。
启用IP转发 echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf; sysctl -p
客户端配置 (client1.conf)
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | [Interface]
# 客户端1 IP
Address = 192.168.2.2/32
PrivateKey = client1PrivateKeyAbcAbcAbc=
DNS = 8.8.8.8
[Peer]
# 服务端公钥
PublicKey = serverPublicKeyAbcAbcAbc=
# 路由所有流量到服务端(全局代理)
AllowedIPs = 0.0.0.0/0
# 服务端地址和端口
Endpoint = server.example.com:51820
PersistentKeepalive = 25
|
客户端2配置 (client2.conf)
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | [Interface]
# 客户端2 IP
Address = 192.168.2.3/32
PrivateKey = client2PrivateKeyAbcAbcAbc=
DNS = 8.8.8.8
[Peer]
# 服务端公钥
PublicKey = serverPublicKeyAbcAbcAbc=
# 路由所有流量到服务端(全局代理)
AllowedIPs = 0.0.0.0/0
# 服务端地址和端口
Endpoint = server.example.com:51820
PersistentKeepalive = 25
|
跨网段转发
| Text Only |
|---|
| [客户端1] WireGuard: 192.168.2.2/32
| 想访问192.168.3.x/24
|
|
[服务器] 公网IP: 1.2.3.4
| WireGuard: 192.168.2.1/24
| ListenPort: 51820
|
[客户端2] WireGuard: 192.168.2.3/32
内网IP:192.168.3.3/24
处于192.168.3.x/24局域网
|
服务端配置 (server.conf)
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | [Interface]
# 服务端 IP
Address = 192.168.2.1/24
PrivateKey = serverPrivateKeyAbcAbcAbc=
ListenPort = 51820
[Peer]
# 客户端1
PublicKey = client1PublicKeyAbcAbcAbc=
AllowedIPs = 192.168.2.2/32
[Peer]
# 客户端2 - 作为网关,允许转发其内网流量
PublicKey = client2PublicKeyAbcAbcAbc=
AllowedIPs = 192.168.2.3/32, 192.168.3.0/24
|
说明: 在同网段转发模式下,客户端之间可以直接通信,所有客户端流量都通过中心服务器转发。
启用IP转发 echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf; sysctl -p
客户端1配置 (client1.conf)
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | [Interface]
# 客户端1 IP
Address = 192.168.2.2/32
PrivateKey = client1PrivateKeyAbcAbcAbc=
DNS = 8.8.8.8
[Peer]
# 服务端公钥
PublicKey = serverPublicKeyAbcAbcAbc=
# 路由到客户端2的内网网段
AllowedIPs = 192.168.2.0/24, 192.168.3.0/24
# 服务端地址和端口
Endpoint = server.example.com:51820
PersistentKeepalive = 25
|
客户端2配置 (gateway-client.conf)
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | [Interface]
# 客户端2 IP
Address = 192.168.2.3/32
PrivateKey = client2PrivateKeyAbcAbcAbc=
DNS = 8.8.8.8
# 启用NAT,使其成为内网网关,对原IP是192.168.2.0/24且从eth0出去的报文做SNAT
PostUp = iptables -t nat -A POSTROUTING -o eth0 -s 192.168.2.0/24 -j MASQUERADE;
PostDown = iptables -t nat -D POSTROUTING -o eth0 -s 192.168.2.0/24 -j MASQUERADE;
[Peer]
# 服务端公钥
PublicKey = serverPublicKeyAbcAbcAbc=
# 路由WireGuard网段流量
AllowedIPs = 192.168.2.0/24
# 服务端地址和端口
Endpoint = server.example.com:51820
PersistentKeepalive = 25
|
说明:
- 客户端1通过WireGuard服务器可以访问客户端2内网的192.168.3.x网段
- WireGuard网关的客户端2里AllowedIPs添加192.168.3.0/24
- 客户端2开启IP转发和源NAT转换
Linux 服务端配置
密钥生成
| Bash |
|---|
| # 产生私钥
wg genkey > privatekey
# 产生公钥
wg pubkey < privatekey > publickey
# 或者一键生成
wg genkey | tee privatekey | wg pubkey > publickey
|
完整服务端配置示例
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | [Interface]
# 节点名,仅注释用
Name = wg-gateway
# VPN路由子网
Address = 192.168.2.1/24
# VPN监听端口
ListenPort = 51820
# 本地节点私钥
PrivateKey = localPrivateKeyAbcAbcAbc=
DNS = 1.1.1.1,8.8.8.8
MTU = 1420
[Peer]
# 客户端1
PublicKey = clientPublicKey1AbcAbcAbc=
AllowedIPs = 192.168.2.2/32
[Peer]
# 客户端2
PublicKey = clientPublicKey2AbcAbcAbc=
AllowedIPs = 192.168.2.3/32
|
服务端系统配置
| Bash |
|---|
| # 启用IP转发
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p
# 配置防火墙
sudo ufw allow 51820/udp
# 或者使用 iptables
sudo iptables -A INPUT -p udp --dport 51820 -j ACCEPT
|
启动与暂停
| Bash |
|---|
1
2
3
4
5
6
7
8
9
10
11
12 | # 快速启动
sudo wg-quick up wg0
# 快速关闭
sudo wg-quick down wg0
# 开机自启
sudo systemctl enable wg-quick@wg0
# 查看状态
sudo wg show
# 重启服务
sudo systemctl restart wg-quick@wg0
|
Linux 客户端配置
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | [Interface]
# Name = wg-client
# 本地 wireguard网口IP
Address = 192.168.2.2/32
PrivateKey = clientPrivateKeyAbcAbcAbc=
DNS = 8.8.8.8
[Peer]
# Name = wireguard-server
# 路由所有流量到服务端(全局代理)
AllowedIPs = 0.0.0.0/0
# 或仅路由特定网段(部分代理)
# AllowedIPs = 192.168.2.0/24, 10.0.0.0/8
# wireguard服务器监听IP和端口
Endpoint = xxx.xxx.xxx.xxx:51820
PublicKey = serverPublicKeyAbcAbcAbc=
PersistentKeepalive = 25
|
Windows 客户端配置
| INI |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | [Interface]
# Name = wg-client-windows
# 本地 wireguard网口IP
Address = 192.168.2.3/32
PrivateKey = clientPrivateKeyAbcAbcAbc=
DNS = 8.8.8.8
[Peer]
# Name = wireguard-server
AllowedIPs = 0.0.0.0/0
# wireguard服务器监听IP和端口
Endpoint = xxx.xxx.xxx.xxx:51820
PublicKey = serverPublicKeyAbcAbcAbc=
PersistentKeepalive = 25
|
参数解析
Interface
本地节点配置
| 参数 |
说明 |
示例/说明 |
| Address |
本地wg网口IP,支持IPv4和IPv6 |
格式:IP地址/子网掩码长度 示例:192.168.2.1/24、10.0.0.1/32 |
| ListenPort |
监听对端连接的端口 隧道外层端口,默认51820 |
范围:1-65535 建议使用非标准端口避免封锁 |
| PrivateKey |
本端私钥,base64编码 |
使用 wg genkey 生成 必须保密,不可泄露 |
| DNS |
客户端使用的DNS服务器 |
支持多个DNS,逗号分隔 常用:8.8.8.8,1.1.1.1 |
| MTU |
最大传输单元,建议1420(避免分片) |
默认值会自动探测 网络环境差时可适当降低 |
| PreUp/PostUp |
启动前后执行的命令 如果执行失败,wg网口会被删除并停止 %i 代表接口名 |
常用于配置路由、防火墙规则 |
| PreDown/PostDown |
停止前后执行的动作 如果执行失败,停止会失败 |
用于清理配置 |
Peer
对端节点配置
| 参数 |
说明 |
示例/说明 |
| AllowedIPs |
将目的IP是哪些网段的流量转发到对端 |
0.0.0.0/0 - 所有流量(全局代理)
192.168.2.0/24 - 仅内网流量 支持多个网段,逗号分隔 |
| Endpoint |
对端监听的IP和端口 即对端使用的隧道外层IP和端口 |
格式:IP:端口 或 域名:端口 支持IPv6:[::1]:51820 |
| PublicKey |
对端公钥,base64编码 |
使用 wg pubkey < privatekey 生成 |
| PersistentKeepalive |
心跳间隔(秒),保持NAT穿透 客户端建议设置25,服务端不设置 |
范围:1-65535秒 避免NAT超时断开连接 |
常用命令
| Bash |
|---|
| # 查看状态
sudo wg show
# 查看详细信息
sudo wg show all
# 重新加载配置
sudo wg-quick down wg0
sudo wg-quick up wg0
|
故障排查
| Bash |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | # 查看日志
sudo journalctl -u wg-quick@wg0 -f
# 测试连通性
ping 192.168.2.1
# 检查路由
ip route show
# 检查防火墙
sudo iptables -L -n
sudo ufw status
# 检查端口监听
sudo ss -ulnp | grep 51820
# 抓包分析
sudo tcpdump -i wg0
# 检查内核模块
lsmod | grep wireguard
|
安全建议
- 定期轮换密钥
- 使用强随机数生成器
- 限制AllowedIPs范围
- 启用系统防火墙
- 监控连接日志
- 禁用不必要的服务
- 及时更新系统
已知问题
- 国内UDP丢包严重
- 某些运营商QoS限制UDP流量
- NAT环境下需要PersistentKeepalive
- IPv6支持需要系统配置
- 移动网络切换可能导致断线
相关工具