WireGuard 配置实战
High Contrast
Dark Mode
Light Mode
Sepia
Forest
1 min read264 words

WireGuard 配置实战

WireGuard 的配置极其简洁——一个配置文件,几行命令,两台服务器就能建立加密隧道。本节覆盖两种最常见的拓扑:点对点(服务器互联)和星型组网(员工远程访问)。


安装

# Ubuntu 20.04+(内核 5.6+ 已内置 WireGuard 模块)
apt install wireguard wireguard-tools
# CentOS/RHEL 8+
dnf install epel-release
dnf install wireguard-tools
# 验证内核模块
modprobe wireguard
lsmod | grep wireguard

密钥生成

# 生成私钥
wg genkey | tee privatekey
# 从私钥生成公钥
wg pubkey < privatekey > publickey
# 一行生成(常用)
wg genkey | tee /etc/wireguard/private.key | wg pubkey > /etc/wireguard/public.key
chmod 600 /etc/wireguard/private.key
cat /etc/wireguard/private.key  # 保存好,不要泄露
cat /etc/wireguard/public.key   # 这个可以公开

场景 1:点对点隧道(两台服务器互联)

拓扑:
服务器 A(公网 IP:203.0.113.10)  ←→  服务器 B(公网 IP:198.51.100.20)
VPN IP:10.10.10.1/24                   VPN IP:10.10.10.2/24
内网:10.0.10.0/24                      内网:10.0.20.0/24
# === 服务器 A:/etc/wireguard/wg0.conf ===
[Interface]
PrivateKey = <服务器A的私钥>
Address = 10.10.10.1/24
ListenPort = 51820
# 可选:连接建立/断开时执行的脚本
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
# 服务器 B
PublicKey = <服务器B的公钥>
Endpoint = 198.51.100.20:51820       # 对端公网地址
AllowedIPs = 10.10.10.2/32, 10.0.20.0/24   # 通过此 Peer 路由的网段
PersistentKeepalive = 25              # 每25秒发心跳(穿透 NAT)
# === 服务器 B:/etc/wireguard/wg0.conf ===
[Interface]
PrivateKey = <服务器B的私钥>
Address = 10.10.10.2/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
# 服务器 A
PublicKey = <服务器A的公钥>
Endpoint = 203.0.113.10:51820
AllowedIPs = 10.10.10.1/32, 10.0.10.0/24
PersistentKeepalive = 25
# 启动 WireGuard
wg-quick up wg0
# 开机自启
systemctl enable wg-quick@wg0
# 查看状态
wg show
# 测试连通性
ping 10.10.10.2
ping 10.0.20.100    # 通过 VPN 访问服务器 B 的内网

场景 2:星型组网(Hub-Spoke,员工远程访问)

拓扑:
服务器(Hub)
公网 IP: 203.0.113.10
VPN IP: 10.20.0.1/24
╱         ╲
员工 A (Spoke)    员工 B (Spoke)
VPN IP: 10.20.0.2   VPN IP: 10.20.0.3
(动态公网 IP)       (动态公网 IP)
# === Hub 服务器:/etc/wireguard/wg0.conf ===
[Interface]
PrivateKey = <Hub私钥>
Address = 10.20.0.1/24
ListenPort = 51820
# 开启 IP 转发(让 Spoke 之间能互通)
PostUp = sysctl -w net.ipv4.ip_forward=1; iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
# 员工 A
[Peer]
PublicKey = <员工A公钥>
AllowedIPs = 10.20.0.2/32    # 只允许员工A的VPN IP
# 员工 B
[Peer]
PublicKey = <员工B公钥>
AllowedIPs = 10.20.0.3/32
# 可以无限添加更多员工...
# === 员工 A(客户端):wg0.conf ===
[Interface]
PrivateKey = <员工A私钥>
Address = 10.20.0.2/32
DNS = 10.20.0.1         # 使用 Hub 作为 DNS(可选)
[Peer]
PublicKey = <Hub公钥>
Endpoint = 203.0.113.10:51820
AllowedIPs = 0.0.0.0/0    # 所有流量走 VPN(全隧道模式)
# 或者只路由公司内网:
# AllowedIPs = 10.0.0.0/8, 192.168.0.0/16
PersistentKeepalive = 25

AllowedIPs 的双重作用

AllowedIPs 同时控制两件事:
1. 路由:到 AllowedIPs 网段的流量 → 通过此 Peer 发送
2. 过滤:只接受来自此 Peer 且源IP在 AllowedIPs 内的数据包
全隧道(Full Tunnel)vs 分割隧道(Split Tunnel):
AllowedIPs = 0.0.0.0/0           → 全部流量走 VPN(全隧道)
AllowedIPs = 10.0.0.0/8          → 只有到内网的流量走 VPN(分割隧道)
分割隧道的好处:
- 节省服务器带宽(员工上网不走 VPN)
- 降低延迟(视频/外网直接走本地)
- 服务器只处理内网访问流量

常用管理命令

# 查看 WireGuard 状态(包括传输量和最后握手时间)
wg show
# 输出示例:
# interface: wg0
#   public key: abcdef...
#   listening port: 51820
#
# peer: xyz123...
#   endpoint: 198.51.100.20:51820
#   allowed ips: 10.10.10.2/32
#   latest handshake: 1 minute, 23 seconds ago
#   transfer: 1.23 MiB received, 456 KiB sent
# 动态添加 Peer(不需要重启)
wg set wg0 peer <公钥> allowed-ips 10.20.0.10/32 endpoint 1.2.3.4:51820
# 动态删除 Peer
wg set wg0 peer <公钥> remove
# 保存当前配置到文件
wg showconf wg0 > /etc/wireguard/wg0.conf
# 重新加载配置(不中断现有连接)
wg syncconf wg0 <(wg-quick strip wg0)

防火墙放行 WireGuard

# 服务器端需要放行 UDP 51820
iptables -A INPUT -p udp --dport 51820 -j ACCEPT
# 如果用 nftables:
nft add rule inet filter input udp dport 51820 accept
# 云安全组:
# 协议:UDP
# 端口:51820
# 来源:0.0.0.0/0(或限制为员工IP段)

CCNA 对应考点

考纲位置:Domain 4.3 — Explain the function of SNMP in network operations

WireGuard 不在 CCNA 考纲内,但 VPN 隧道概念和 IPSec 基础是考纲内容。理解 WireGuard 的隧道模式(Tunnel Mode)和密钥对工作方式,对理解 IPSec 很有帮助。


下一节企业 SSL-VPN 方案——WireGuard 适合技术团队自建 VPN,企业还需要 Web 管理界面、LDAP 集成、MFA 等功能——了解 OpenVPN Access Server 和 Pritunl 等商业方案。