WireGuard VPN 服务器部署指南

WireGuard 是一个现代化、高性能的 VPN 协议,相比 OpenVPN 更轻量、更快速。本文记录了在阿里云 ECS 上部署 WireGuard VPN 服务器的完整过程,包括服务器端配置、客户端配置、防火墙设置以及常见问题排查。

为什么选择 WireGuard?

WireGuard 相比传统 VPN 方案的优势:

  • 性能优异:内核级实现,延迟更低
  • 配置简洁:无需复杂的证书管理,只需公钥/私钥配对
  • 安全性高:使用现代加密协议(ChaCha20、Poly1305 等)
  • 跨平台:支持 Linux、Windows、macOS、iOS、Android

服务器端部署

1. 安装 WireGuard

1
2
3
4
5
# CentOS/RHEL
yum install wireguard-tools

# Ubuntu/Debian
apt install wireguard

2. 生成密钥对

WireGuard 使用公钥加密来验证身份,每个参与者都需要一对密钥(私钥 + 公钥)。

为什么需要两对密钥?

打个比方:

  • 服务器就像一个「门」,它有自己的钥匙(私钥)和门牌号(公钥)
  • 客户端就像一个「人」,有自己的身份证(私钥)和证件号(公钥)

要进入这扇门:

  1. 人需要知道门的公钥(门牌号),才能找到正确的门
  2. 门需要知道人的公钥(证件号),才能验证是不是允许这个人进来

这就是为什么双方都需要互相交换公钥

密钥 保存在哪里 作用
服务器私钥 服务器配置 [Interface] 服务器身份验证,绝不外泄
服务器公钥 客户端配置 [Peer] 客户端用它找到服务器
客户端私钥 客户端配置 [Interface] 客户端身份验证,绝不外泄
客户端公钥 服务器配置 [Peer] 服务器用它验证客户端身份

密钥流向图

1
2
3
4
5
6
7
8
9
服务器配置文件                     客户端配置文件
┌─────────────────────┐ ┌─────────────────────┐
│ [Interface] │ │ [Interface] │
│ PrivateKey = 服务器私钥 │ │ PrivateKey = 客户端私钥 │
│ │ │ │
│ [Peer] │ │ [Peer] │
│ PublicKey = 客户端公钥 │◄──交换──►│ PublicKey = 服务器公钥 │
│ AllowedIPs = ... │ │ Endpoint = ... │
└─────────────────────┘ └─────────────────────┘

记住这个原则

  • 自己的私钥 → 写在自己的 [Interface]
  • 对方的公钥 → 写在自己的 [Peer]

生成密钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 创建配置目录
mkdir -p /etc/wireguard
cd /etc/wireguard

# 生成服务器密钥对
wg genkey | tee server_private.key | wg pubkey > server_public.key

# 生成客户端密钥对
wg genkey | tee client_private.key | wg pubkey > client_public.key

# 查看生成的密钥
cat server_private.key # 服务器私钥 → 写入服务器 [Interface]
cat server_public.key # 服务器公钥 → 写入客户端 [Peer]
cat client_private.key # 客户端私钥 → 写入客户端 [Interface]
cat client_public.key # 客户端公钥 → 写入服务器 [Peer]

重要:私钥绝对不能泄露!公钥可以公开分享给对方。

3. 创建服务器配置文件

1
2
3
4
5
6
7
8
9
10
cat > /etc/wireguard/wg0.conf << EOF
[Interface]
PrivateKey = <服务器私钥>
Address = 10.100.0.1/24
ListenPort = 443

[Peer]
PublicKey = <客户端公钥>
AllowedIPs = 10.100.0.2/32
EOF

配置说明:

  • ListenPort = 443:使用 HTTPS 端口,避免运营商封锁非标准端口(如 51820)
  • Address = 10.100.0.1/24:VPN 内网地址段,服务器使用 .1
  • AllowedIPs = 10.100.0.2/32:只允许该客户端使用 .2 地址

4. 启动 WireGuard 服务

1
2
3
4
5
6
# 启动服务
wg-quick up wg0

# 或使用 systemd 管理
systemctl start wg-quick@wg0
systemctl enable wg-quick@wg0 # 开机自启

验证服务状态:

1
wg show

输出示例:

1
2
3
4
5
6
7
interface: wg0
public key: <服务器公钥>
private key: (hidden)
listening port: 443

peer: <客户端公钥>
allowed ips: 10.100.0.2/32

防火墙与 NAT 配置

1. 开放防火墙端口

1
2
3
4
5
6
# firewalld
firewall-cmd --permanent --add-port=443/udp
firewall-cmd --reload

# iptables
iptables -A INPUT -p udp --dport 443 -j ACCEPT

2. 配置 NAT 规则(让客户端能上网)

WireGuard 客户端通过 VPN 连接后,需要 NAT 规则才能访问外网:

1
2
3
4
5
6
# 使用 firewalld 持久化 NAT 规则
firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -s 10.100.0.0/24 -o eth0 -j MASQUERADE
firewall-cmd --reload

# 或使用 iptables(重启后会丢失)
iptables -t nat -A POSTROUTING -s 10.100.0.0/24 -o eth0 -j MASQUERADE

3. 开启 IP 转发

1
2
3
4
5
6
7
8
9
# 检查是否已开启
cat /proc/sys/net/ipv4/ip_forward

# 如果为 0,需要开启
sysctl -w net.ipv4.ip_forward=1

# 持久化设置
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p

4. FORWARD 规则持久化

1
2
3
firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -i wg0 -o eth0 -j ACCEPT
firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -i eth0 -o wg0 -m state --state ESTABLISHED,RELATED -j ACCEPT
firewall-cmd --reload

5. 阿里云安全组配置

在阿里云控制台添加安全组规则:

方向 协议 端口 来源
入站 UDP 443 0.0.0.0/0
出站 UDP 全部 0.0.0.0/0

客户端配置

配置文件内容

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

[Peer]
PublicKey = <服务器公钥>
Endpoint = <服务器公网IP>:443
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

配置说明:

  • AllowedIPs = 0.0.0.0/0:所有流量都走 VPN
  • PersistentKeepalive = 25:保持连接活跃,避免断开

生成二维码(方便手机导入)

1
2
3
4
5
6
7
8
# 安装 qrencode
yum install qrencode

# 生成二维码
qrencode -t PNG -o wireguard-qr.png -r client.conf

# 或直接显示在终端
qrencode -t ANSIUTF8 -r client.conf

各平台客户端下载

平台 下载方式
iOS App Store 搜索 “WireGuard”
Android Google Play 或 F-Droid
Windows https://www.wireguard.com/install/
macOS App Store 或 brew install wireguard-tools

常见问题排查

1. 能连接但无法上网

检查 NAT 规则:

1
iptables -t nat -L POSTROUTING -n -v

如果没有 VPN 网段的 MASQUERADE 规则:

1
iptables -t nat -A POSTROUTING -s 10.100.0.0/24 -o eth0 -j MASQUERADE

2. 握手失败(无 latest handshake)

检查密钥是否匹配:

1
2
3
4
5
# 服务器端:客户端公钥应该和配置文件一致
wg show | grep peer

# 客户端配置里的服务器公钥应该和服务器实际公钥一致
cat /etc/wireguard/wg0.conf | grep PrivateKey | awk '{print $3}' | wg pubkey

3. 数据包发送但无响应

可能是端口被封锁。尝试:

  • 改用 UDP 443(HTTPS 端口)
  • 检查阿里云安全组是否开放 UDP 出站

4. 连接后流量不走 VPN

检查客户端 AllowedIPs:

1
2
AllowedIPs = 0.0.0.0/0  # 全局代理
AllowedIPs = 10.100.0.0/24 # 只代理 VPN 内网

5. 规则重启后丢失

确保使用 firewalld 持久化:

1
2
# 检查持久化规则
firewall-cmd --permanent --direct --get-all-rules

服务管理命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 启动
wg-quick up wg0
systemctl start wg-quick@wg0

# 停止
wg-quick down wg0
systemctl stop wg-quick@wg0

# 状态
wg show
systemctl status wg-quick@wg0

# 重启
systemctl restart wg-quick@wg0

添加更多客户端

每个客户端需要独立的密钥对和 IP 地址:

1
2
3
4
5
6
7
8
# 生成新客户端密钥
wg genkey | tee client2_private.key | wg pubkey > client2_public.key

# 在服务器端添加 peer
wg set wg0 peer <客户端2公钥> allowed-ips 10.100.0.3/32

# 更新服务器配置文件(持久化)
# 在 /etc/wireguard/wg0.conf 添加新的 [Peer] 块

小结

WireGuard 的部署比 OpenVPN 简单得多,核心要点:

  1. 密钥匹配:服务器和客户端的公钥/私钥必须正确配对
  2. 端口选择:用 UDP 443 避免运营商封锁
  3. NAT 规则:确保 VPN 网段有 MASQUERADE
  4. 规则持久化:用 firewalld direct 规则或 iptables-save

配置完成后,重启服务会自动恢复,无需手动干预。