香港服务器搭建WireGuard VPN:企业组网 + 分布式团队内网连通完整方案
传统 VPN(OpenVPN/IPSec)配置复杂、性能低、协议老旧。WireGuard 是 Linux 内核原生支持的现代 VPN 协议:代码量仅 4000 行(OpenVPN 超过 10 万行)、基于 ChaCha20 加密(比 AES 更快)、握手时间约 100ms(OpenVPN 需要数秒)。在香港服务器上部署 WireGuard,让全球分布的团队成员、办公室和服务器组成统一的加密内网。
一、WireGuard 工作原理
<code">
WireGuard 拓扑(香港服务器作为中继节点):
服务器(香港 VPS)
IP: 10.8.0.1(WireGuard 内网 IP)
公网 IP: 1.2.3.4,端口: 51820
├── 北京办公室(Peer 1)
│ 内网 IP: 10.8.0.2
│ 访问路由: 192.168.1.0/24(办公室内网)
│
├── 远程员工 Alice(Peer 2)
│ 内网 IP: 10.8.0.3
│ 移动设备,动态 IP
│
└── 新加坡服务器(Peer 3)
内网 IP: 10.8.0.4
固定 IP,双向可达
二、安装 WireGuard(服务器端)
<code"># Ubuntu 22.04(内核已内置 WireGuard 模块) apt update && apt install -y wireguard wireguard-tools # 生成服务器密钥对 wg genkey | tee /etc/wireguard/server_private.key | wg pubkey > /etc/wireguard/server_public.key chmod 600 /etc/wireguard/server_private.key # 查看生成的密钥 SERVER_PRIVATE=$(cat /etc/wireguard/server_private.key) SERVER_PUBLIC=$(cat /etc/wireguard/server_public.key) echo "服务器公钥: $SERVER_PUBLIC"
<code"># /etc/wireguard/wg0.conf(服务器配置)
[Interface]
# 服务器 WireGuard 内网 IP
Address = 10.8.0.1/24
# 监听端口(在防火墙开放此 UDP 端口)
ListenPort = 51820
# 服务器私钥
PrivateKey = 服务器私钥内容
# 开启路由转发(让 Peer 之间可以互相访问)
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; \
iptables -A FORWARD -o wg0 -j ACCEPT; \
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; \
iptables -D FORWARD -o wg0 -j ACCEPT; \
iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
# ── 北京办公室(路由器/网关设备上安装 WireGuard)──
[Peer]
PublicKey = 北京办公室公钥
# 办公室内网:192.168.1.0/24 和 WireGuard IP 10.8.0.2
AllowedIPs = 10.8.0.2/32, 192.168.1.0/24
# ── 远程员工 Alice ──
[Peer]
PublicKey = Alice的公钥
AllowedIPs = 10.8.0.3/32
# 不设置 Endpoint(动态 IP,Alice 主动连接服务器)
# ── 新加坡服务器 ──
[Peer]
PublicKey = 新加坡服务器公钥
Endpoint = 5.6.7.8:51820 # 新加坡服务器的公网 IP 和端口
AllowedIPs = 10.8.0.4/32<code"># 开启 IP 转发(系统级) echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf sysctl -p # 开放 WireGuard UDP 端口 ufw allow 51820/udp # 启动 WireGuard systemctl enable --now wg-quick@wg0 wg show # 查看状态
三、客户端配置生成脚本
<code"># /usr/local/bin/wg-add-client.sh(批量生成客户端配置)
#!/bin/bash
CLIENT_NAME="$1"
CLIENT_IP="$2" # 如 10.8.0.3
SERVER_PUBLIC=$(cat /etc/wireguard/server_public.key)
SERVER_ENDPOINT="1.2.3.4:51820" # 香港服务器公网 IP 和端口
if [ -z "$CLIENT_NAME" ] || [ -z "$CLIENT_IP" ]; then
echo "用法: $0 客户端名称 IP地址(如: $0 alice 10.8.0.3)"
exit 1
fi
# 生成客户端密钥对
mkdir -p /etc/wireguard/clients
wg genkey | tee /etc/wireguard/clients/${CLIENT_NAME}_private.key | \
wg pubkey > /etc/wireguard/clients/${CLIENT_NAME}_public.key
CLIENT_PRIVATE=$(cat /etc/wireguard/clients/${CLIENT_NAME}_private.key)
CLIENT_PUBLIC=$(cat /etc/wireguard/clients/${CLIENT_NAME}_public.key)
# 生成客户端配置文件
cat > /etc/wireguard/clients/${CLIENT_NAME}.conf << EOF [Interface] PrivateKey = ${CLIENT_PRIVATE} Address = ${CLIENT_IP}/24 DNS = 1.1.1.1, 8.8.8.8 [Peer] PublicKey = ${SERVER_PUBLIC} Endpoint = ${SERVER_ENDPOINT} AllowedIPs = 10.8.0.0/24 # 只路由 WireGuard 内网(分流模式) # AllowedIPs = 0.0.0.0/0 # 全部流量通过 VPN(全隧道模式) PersistentKeepalive = 25 # 每25秒发送心跳(防 NAT 超时) EOF # 将客户端公钥添加到服务器配置 cat >> /etc/wireguard/wg0.conf << EOF
# ${CLIENT_NAME}
[Peer]
PublicKey = ${CLIENT_PUBLIC}
AllowedIPs = ${CLIENT_IP}/32
EOF
# 动态添加 Peer(无需重启 WireGuard)
wg set wg0 peer ${CLIENT_PUBLIC} allowed-ips ${CLIENT_IP}/32
echo "✅ 客户端配置已生成: /etc/wireguard/clients/${CLIENT_NAME}.conf"
echo "公钥: ${CLIENT_PUBLIC}"
echo ""
echo "将以下内容发给用户,或生成二维码扫描导入手机:"
cat /etc/wireguard/clients/${CLIENT_NAME}.conf<code"># 使用示例 bash /usr/local/bin/wg-add-client.sh alice 10.8.0.3 bash /usr/local/bin/wg-add-client.sh bob 10.8.0.4 bash /usr/local/bin/wg-add-client.sh beijing-office 10.8.0.10 # 生成二维码(手机 WireGuard App 扫码导入) apt install -y qrencode qrencode -t ansiutf8 < /etc/wireguard/clients/alice.conf
四、移动端配置(iOS/Android)
<code"> 手机端安装 WireGuard App(App Store / Google Play) 导入方式: 1. 扫描二维码(上面 qrencode 命令生成) 2. 从文件导入(将 .conf 文件发送到手机) 3. 手动输入 建议配置: - AllowedIPs: 10.8.0.0/24(分流模式,只有内网流量走 VPN) 优点:不影响其他网络访问速度 - AllowedIPs: 0.0.0.0/0(全隧道,所有流量走 VPN) 优点:安全性更高,适合连接公共 WiFi 时
五、WireGuard vs OpenVPN 性能对比
| 指标 | WireGuard | OpenVPN |
|---|---|---|
| 握手时间 | ~100ms | ~3000ms |
| 吞吐量(1Gbps 线路) | 约 900Mbps | 约 200Mbps |
| CPU 占用(1Gbps) | 低 | 高 |
| 延迟增加 | 约 1~2ms | 约 5~10ms |
| 代码行数 | ~4,000 | ~100,000+ |
| 协议 | UDP(更快) | UDP/TCP |
| NAT 穿透 | 好(PersistentKeepalive) | 中等 |
六、监控与管理
<code"># 查看 WireGuard 状态(连接的 Peer 和流量)
wg show
# 输出示例:
# peer: alice公钥xxxxx
# endpoint: 1.2.3.4:38492
# allowed ips: 10.8.0.3/32
# latest handshake: 2 minutes, 15 seconds ago
# transfer: 1.42 GiB received, 324 MiB sent
# 监控脚本(检测 Peer 是否在线)
for peer in $(wg show wg0 peers); do
last_handshake=$(wg show wg0 latest-handshakes | grep $peer | awk '{print $2}')
if [ "$last_handshake" -eq 0 ]; then
echo "$peer: 从未连接"
elif [ $(($(date +%s) - last_handshake)) -gt 300 ]; then
echo "$peer: 离线(最后握手超过5分钟)"
else
echo "$peer: 在线"
fi
done七、总结
WireGuard 在香港服务器上构建的企业 VPN,将全球分布的员工、办公室和服务器连成一个加密内网,性能开销极小(吞吐量接近原始带宽的90%)。相比 Headscale/Tailscale(基于 WireGuard 的上层封装),原生 WireGuard 的优势在于完全透明可控、无第三方依赖、协议最简单——对于网络工程师来说是理解和定制的最佳起点。