WireGuard 自建 VPN 完整教程 2026:从零搭建高性能全设备翻墙
当提到自建科学上网方案时,大多数人首先想到的是 V2Ray、Shadowsocks 或 3X-UI 面板。但如果你需要的是一种 全设备无感翻墙 ——不仅仅是浏览器和特定 App 走代理,而是整个系统的所有流量(包括系统服务、DNS 查询、局域网通信)都通过加密隧道传输——那么 WireGuard 才是最佳选择。
WireGuard 是一种现代化的 VPN 协议,代码量仅约 4,000 行(相比之下 OpenVPN 超过 10 万行、IPSec 超过 40 万行),已于 2020 年正式合并进 Linux 内核。它以极高的性能、极简的配置和极强的安全性著称,被公认为下一代 VPN 协议的标准。
本文将详细介绍如何在 VPS 上从零搭建 WireGuard VPN,并涵盖多用户管理、性能优化、全平台客户端配置和常见故障排查。
WireGuard 简介
什么是 WireGuard
WireGuard 是一种 Layer 3 VPN 协议,由 Jason A. Donenfeld 开发。它的设计理念是:
🎯 极简:整个协议只有 4,000 行代码,易于审计
🚀 极速:内核级运行,性能远超用户态协议
🔒 极安全:使用现代加密算法(ChaCha20、Poly1305、Curve25519)
📱 全平台:Linux/Windows/macOS/iOS/Android 全支持
🔄 无状态:无连接概念,UDP 无状态设计,切换网络自动恢复工作原理
WireGuard 的核心设计与传统 VPN 协议有本质区别:
| 特性 | WireGuard | OpenVPN | IPSec |
|---|---|---|---|
| 架构 | 无状态 UDP | 有状态 TLS | 有状态 IKE |
| 代码量 | ~4,000 行 | ~100,000 行 | ~400,000 行 |
| 加密 | ChaCha20+Poly1305 | AES-256-CBC | AES-256-GCM |
| 密钥交换 | Curve25519 | RSA/DH | DH/RSA |
| 运行层级 | 内核态(Linux) | 用户态 | 内核态 |
| 握手 | 1-RTT | 多轮握手 | 多轮握手 |
| 漫游 | 自动 IP 漫游 | 需要重连 | 需要重连 |
WireGuard 的核心概念是 Peer(对等节点)。每个 Peer 拥有一对公私钥,双方通过交换公钥建立加密通道。WireGuard 没有传统 VPN 的「客户端-服务端」概念——每个节点都是对等的,只是配置时通常将 VPS 作为中转节点。
与 V2Ray/Shadowsocks 的本质区别
| 维度 | WireGuard | V2Ray/Shadowsocks |
|---|---|---|
| 工作层级 | 网络层(Layer 3) | 应用层代理(SOCKS5/HTTP) |
| 流量类型 | 全部流量(TCP+UDP+ICMP) | 仅代理指定流量 |
| 运行方式 | 虚拟网卡(tun 设备) | 应用层监听端口 |
| DNS | 强制走 VPN 隧道 | 可配置 DNS 但非强制 |
| 设备覆盖 | 系统级全流量 | 需要逐应用配置代理 |
| 隐蔽性 | 较低(特征明显的 UDP 流量) | 较高(可伪装为 HTTPS) |
| 适用场景 | 全设备无感翻墙 | 精细化代理控制 |
简单理解:V2Ray/Shadowsocks 是「选择性代理」——你指定哪些 App 走代理;WireGuard 是「全部代理」——连上之后所有流量都走 VPN 隧道。
适用场景
WireGuard 特别适合以下场景:
- 全设备翻墙:需要路由器/Apple TV 等设备全局翻墙,而非逐设备配置
- 远程办公:连接公司内网或访问受地理限制的服务
- 家庭网络:在 OpenWrt 路由器上部署,全家设备自动翻墙
- 高带宽需求:WireGuard 性能远超用户态协议,适合 4K 流媒体
- 多设备互联:组建跨地域局域网(如 Tailscale/Netmaker 方案)
不适用场景
- 需要高隐蔽性:WireGuard 的 UDP 流量特征明显,容易被 DPI 识别和 QoS
- 需要精细分流:WireGuard 默认全流量走隧道,分流需要额外配置
- 需要 CDN 中转:WireGuard 是 UDP 协议,无法通过 Cloudflare CDN 中转
WireGuard vs Shadowsocks vs V2Ray 对比
| 对比维度 | WireGuard | Shadowsocks | V2Ray (VMess/VLESS) |
|---|---|---|---|
| 协议类型 | VPN (Layer 3) | 代理 (SOCKS5) | 代理 (SOCKS5/HTTP) |
| 加密方式 | ChaCha20-Poly1305 | AEAD (AES-GCM) | 可选多种加密 |
| 性能 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐ |
| 隐蔽性 | ⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| 配置难度 | ⭐⭐(简单) | ⭐⭐(简单) | ⭐⭐⭐⭐(复杂) |
| 全设备支持 | ✅ 原生 | ❌ 需额外配置 | ❌ 需额外配置 |
| UDP 支持 | ✅ 原生 | ✅ | ✅ |
| CDN 中转 | ❌ | ✅ | ✅ |
| 流量伪装 | ❌ | ❌ | ✅ (WS+TLS/Reality) |
| 防封锁能力 | 低 | 中 | 高 |
| 内核加速 | ✅ Linux 内核 | ❌ 用户态 | ❌ 用户态 |
| 代码量 | ~4,000 行 | ~2,000 行 | ~100,000+ 行 |
选择建议
| 你的需求 | 推荐方案 |
|---|---|
| 需要高隐蔽性、防封锁 | V2Ray + Reality(参考 3X-UI 教程) |
| 需要精细分流、按 App 代理 | V2Ray + Clash(参考 Clash Nyanpasu 教程) |
| 需要全设备无感翻墙 | WireGuard(本文) |
| 需要路由器级翻墙 | WireGuard(OpenWrt 部署) |
| 需要简单快速上手 | Shadowsocks 或 机场服务 |
| 需要两者兼顾 | WireGuard + V2Ray 双部署 |
VPS 准备
推荐配置
WireGuard 对 VPS 配置要求较低,因为它运行在内核态,开销极小:
| 配置项 | 最低要求 | 推荐配置 | 说明 |
|---|---|---|---|
| CPU | 1 核 | 1-2 核 | WireGuard 几乎不占 CPU |
| 内存 | 256MB | 512MB-1GB | 内核模块几乎不占内存 |
| 带宽 | 100Mbps | 500Mbps+ | WireGuard 可跑满带宽 |
| 流量 | 500GB/月 | 1TB+/月 | 全流量 VPN 消耗流量较大 |
| 系统 | Linux 5.6+ | Ubuntu 22.04/24.04 | 内核 5.6+ 原生支持 WireGuard |
| 架构 | x86_64 / ARM64 | 均可 | 树莓派/Oracle ARM 也支持 |
注意:Oracle Cloud 免费 ARM 实例(4 核 24GB)是搭建 WireGuard 的绝佳选择,性能过剩且完全免费。参考 Oracle Cloud 注册教程。
VPS 供应商推荐
| 供应商 | 最低价 | 推荐度 | 说明 |
|---|---|---|---|
| Oracle Cloud | 免费 | ⭐⭐⭐⭐⭐ | ARM 免费实例,4C24G |
| RackNerd | $10/年 | ⭐⭐⭐⭐ | 性价比高 |
| Vultr | $3.5/月 | ⭐⭐⭐⭐ | 全球机房可选 |
| 搬瓦工 | $49.99/年 | ⭐⭐⭐⭐ | CN2 GIA 线路佳 |
详见 2026 VPS 供应商横向对比。
系统初始化
以 Ubuntu 22.04/24.04 为例:
# 更新系统
apt update && apt upgrade -y
# 安装必要工具
apt install -y curl wget git qrencode iptables
# 检查内核版本(需 5.6+)
uname -r
# 输出示例: 6.5.0-1014-oracle (已内置 WireGuard)
# 如果内核低于 5.6,需要安装 wireguard-dkms
# Ubuntu 20.04:
# apt install -y wireguard wireguard-tools wireguard-dkms检查 WireGuard 模块
# 检查内核模块是否已加载
modprobe wireguard && lsmod | grep wireguard
# 输出: wireguard 114688 0
# 检查 wg 命令是否可用
wg --version
# 输出: wireguard-tools v1.0.20210914服务端配置
一键安装脚本(推荐新手)
使用社区维护的 wireguard-install 脚本,一键完成所有配置:
# 下载并运行一键脚本
curl -O https://raw.githubusercontent.com/angristan/wireguard-install/master/wireguard-install.sh
chmod +x wireguard-install.sh
./wireguard-install.sh脚本会交互式询问以下信息:
Welcome to the WireGuard installer!
The repository for the system is ready.
IPv4 or IPv6 public address: <自动检测>
Public interface: eth0
WireGuard port: 51820 (默认)
First DNS resolver: 1.1.1.1 (可选 8.8.8.8)
Second DNS resolver: 1.0.0.1 (可选 8.8.4.4)
Client name: client1
Client's IPv4: 10.66.66.2 (自动分配)
Client's IPv6: fd42:42:42::2 (自动分配)脚本完成后会生成客户端配置文件和二维码。
手动配置(推荐进阶用户)
手动配置能让你更好地理解 WireGuard 的工作原理。
步骤一:生成密钥对
# 创建配置目录
mkdir -p /etc/wireguard
cd /etc/wireguard
# 生成服务端私钥和公钥
wg genkey | tee server_private.key | wg pubkey > server_public.key
# 设置权限(私钥必须仅 root 可读)
chmod 600 server_private.key
# 查看密钥
echo "Server Private Key: $(cat server_private.key)"
echo "Server Public Key: $(cat server_public.key)"
# 为第一个客户端生成密钥
wg genkey | tee client1_private.key | wg pubkey > client1_public.key
chmod 600 client1_private.key步骤二:创建服务端配置文件
cat > /etc/wireguard/wg0.conf << EOF
[Interface]
# 服务端私钥
PrivateKey = $(cat server_private.key)
# 监听端口
Address = 10.66.66.1/24
ListenPort = 51820
# DNS(可选,使用 Pi-hole 或 AdGuard 可设置)
# DNS = 10.66.66.1
# IP 转发 NAT 规则(将 VPN 流量转发到公网)
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -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; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
# --- 客户端 Peer 配置 ---
# Client 1
[Peer]
PublicKey = $(cat client1_public.key)
AllowedIPs = 10.66.66.2/32
EOF注意:
eth0是 VPS 的公网网卡名。如果不确定,运行ip addr查看。Oracle Cloud 通常是ens3或enp0s3。
步骤三:开启 IP 转发
# 临时开启
sysctl -w net.ipv4.ip_forward=1
sysctl -w net.ipv6.conf.all.forwarding=1
# 永久开启
cat >> /etc/sysctl.conf << EOF
net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1
EOF
# 应用
sysctl -p步骤四:防火墙配置
# UFW 放行 WireGuard 端口
ufw allow 51820/udp
# 确保 VPN 内网流量可转发
ufw route allow in on wg0 out on eth0
# 重载防火墙
ufw reload
# 如果使用 iptables(无 UFW)
iptables -A INPUT -p udp --dport 51820 -j ACCEPT
iptables -A FORWARD -i wg0 -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
# 保存规则
apt install -y iptables-persistent
netfilter-persistent saveOracle Cloud 注意:Oracle Cloud 有额外的安全列表(Security List)和网络安全组(NSG),需要在 Oracle Cloud 控制台的 VCN 设置中放行 UDP 51820 端口。
步骤五:启动 WireGuard
# 启动 WireGuard 接口
wg-quick up wg0
# 设置开机自启
systemctl enable wg-quick@wg0
# 查看状态
wg showwg show 输出示例:
interface: wg0
public key: <server_public_key>
private key: (hidden)
listening port: 51820
peer: <client1_public_key>
endpoint: <client_ip>:<port>
allowed ips: 10.66.66.2/32
latest handshake: 1 minute ago
transfer: 1.23 GiB received, 456.78 MiB sent客户端配置
通用客户端配置文件
为客户端生成配置文件:
cat > /etc/wireguard/client1.conf << EOF
[Interface]
PrivateKey = $(cat client1_private.key)
Address = 10.66.66.2/24
DNS = 1.1.1.1, 1.0.0.1
[Peer]
PublicKey = $(cat server_public.key)
Endpoint = <SERVER_IP>:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
EOF关键字段说明:
| 字段 | 说明 |
|---|---|
Address | 客户端在 VPN 内网的 IP |
DNS | 客户端使用的 DNS 服务器 |
PublicKey | 服务端的公钥 |
Endpoint | 服务端公网 IP 和端口 |
AllowedIPs = 0.0.0.0/0 | 全流量走 VPN(关键设置) |
PersistentKeepalive = 25 | 每 25 秒发送心跳保持连接 |
分流模式:如果只想特定 IP 段走 VPN,将
AllowedIPs改为具体网段,如AllowedIPs = 10.66.66.0/24, 192.168.1.0/24。
Windows 客户端
- 下载:从 wireguard.com/install 下载 Windows 客户端
- 导入配置:点击「Import tunnel(s) from file」→ 选择
client1.conf文件 - 连接:点击「Activate」激活隧道
- 验证:打开浏览器访问 ip.sb,确认 IP 已变更为 VPS IP
macOS 客户端
方式一:App Store 客户端
- 在 Mac App Store 搜索 "WireGuard" 并安装
- 打开 App → 「Import Tunnel(s) from File」→ 选择
client1.conf - 点击开关激活
方式二:Homebrew
brew install wireguard-tools
# 将配置文件复制到 /etc/wireguard/
sudo cp client1.conf /etc/wireguard/wg0.conf
# 启动
sudo wg-quick up wg0
# 停止
sudo wg-quick down wg0iOS 客户端
- 在 App Store 搜索 "WireGuard" 安装
- 将
client1.conf文件通过 AirDrop 发送到 iPhone - 在 WireGuard App 中「Import tunnel(s) from file」
- 或扫描二维码:在 VPS 上运行
qrencode -t ansiutf8 /etc/wireguard/client1.conf
Android 客户端
- 在 Google Play 或 F-Droid 搜索 "WireGuard" 安装
- 点击「+」→ 「Import from file or archive」→ 选择
client1.conf - 或扫描二维码:在 VPS 上运行
qrencode -t ansiutf8 /etc/wireguard/client1.conf
Linux 客户端
# 安装
apt install -y wireguard-tools
# 复制配置
sudo cp client1.conf /etc/wireguard/wg0.conf
# 启动
sudo wg-quick up wg0
# 开机自启
sudo systemctl enable wg-quick@wg0
# 查看状态
wg showOpenWrt 路由器(全家庭翻墙)
在 OpenWrt 路由器上部署 WireGuard,实现全家设备自动翻墙:
# 安装 WireGuard 包
opkg update
opkg install wireguard-tools kmod-wireguard luci-proto-wireguard
# 配置接口
uci set network.wg0=interface
uci set network.wg0.proto='wireguard'
uci set network.wg0.private_key='<client_private_key>'
uci set network.wg0.addresses='10.66.66.2/24'
# 配置 Peer
uci add network wireguard_wg0
uci set network.@wireguard_wg0[0].public_key='<server_public_key>'
uci set network.@wireguard_wg0[0].endpoint_host='<SERVER_IP>'
uci set network.@wireguard_wg0[0].endpoint_port='51820'
uci set network.@wireguard_wg0[0].allowed_ips='0.0.0.0/0'
uci set network.@wireguard_wg0[0].persistent_keepalive='25'
# 应用配置
uci commit network
/etc/init.d/network reload多用户管理
添加新用户
cd /etc/wireguard
# 生成新用户密钥
wg genkey | tee client2_private.key | wg pubkey > client2_public.key
chmod 600 client2_private.key
# 生成客户端配置文件
cat > client2.conf << EOF
[Interface]
PrivateKey = $(cat client2_private.key)
Address = 10.66.66.3/24
DNS = 1.1.1.1, 1.0.0.1
[Peer]
PublicKey = $(cat server_public.key)
Endpoint = <SERVER_IP>:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
EOF
# 将新 Peer 添加到服务端配置
cat >> /etc/wireguard/wg0.conf << EOF
# Client 2
[Peer]
PublicKey = $(cat client2_public.key)
AllowedIPs = 10.66.66.3/32
EOF
# 重启 WireGuard 应用新配置
wg-quick down wg0 && wg-quick up wg0
# 生成二维码
qrencode -t ansiutf8 client2.confIP 分配规则
| 客户端 | VPN IP | 说明 |
|---|---|---|
| 服务端 | 10.66.66.1 | 网关地址 |
| Client 1 | 10.66.66.2 | 第一个用户 |
| Client 2 | 10.66.66.3 | 第二个用户 |
| Client N | 10.66.66.N+1 | 依次递增 |
流量监控
# 查看所有 Peer 的流量统计
wg show
# 只看某个 Peer
wg show peer <public_key>
# 实时流量监控(每 2 秒刷新)
watch -n 2 wg show
# 按 JSON 格式输出(便于脚本处理)
wg show wg0 dump输出中的 transfer 字段显示每个 Peer 的接收和发送流量:
peer: <client1_public_key>
endpoint: <ip>:<port>
allowed ips: 10.66.66.2/32
latest handshake: 2 minutes ago
transfer: 1.23 GiB received, 456.78 MiB sent批量用户管理脚本
#!/bin/bash
# /etc/wireguard/add-user.sh
# 用法: ./add-user.sh <username>
USERNAME=$1
IP_BASE=10.66.66
# 查找下一个可用 IP
LAST_IP=$(grep "AllowedIPs" /etc/wireguard/wg0.conf | awk -F'[./]' '{print $5}' | sort -n | tail -1)
NEXT_IP=$((LAST_IP + 1))
cd /etc/wireguard
# 生成密钥
wg genkey | tee ${USERNAME}_private.key | wg pubkey > ${USERNAME}_public.key
chmod 600 ${USERNAME}_private.key
# 生成客户端配置
cat > ${USERNAME}.conf << EOF
[Interface]
PrivateKey = $(cat ${USERNAME}_private.key)
Address = ${IP_BASE}.${NEXT_IP}/24
DNS = 1.1.1.1, 1.0.0.1
[Peer]
PublicKey = $(cat server_public.key)
Endpoint = $(curl -s ifconfig.me):51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
EOF
# 添加到服务端配置
cat >> /etc/wireguard/wg0.conf << EOF
# ${USERNAME}
[Peer]
PublicKey = $(cat ${USERNAME}_public.key)
AllowedIPs = ${IP_BASE}.${NEXT_IP}/32
EOF
# 重启
wg-quick down wg0 && wg-quick up wg0
echo "用户 ${USERNAME} 创建成功!"
echo "配置文件: /etc/wireguard/${USERNAME}.conf"
echo "VPN IP: ${IP_BASE}.${NEXT_IP}"
echo ""
echo "二维码:"
qrencode -t ansiutf8 ${USERNAME}.conf删除用户
# 从服务端配置中删除对应的 [Peer] 段
# 手动编辑 /etc/wireguard/wg0.conf 删除该用户的段落
# 或使用 wg 命令动态删除(不需要重启)
wg set wg0 peer <client_public_key> remove
# 保存到配置文件
wg-quick save wg0
# 清理密钥文件
rm -f /etc/wireguard/client1_private.key /etc/wireguard/client1_public.key /etc/wireguard/client1.conf性能优化
MTU 调整
MTU(最大传输单元)是 WireGuard 性能优化的关键参数。默认值 1420 在部分网络环境下会导致分包,降低性能。
# 测试最佳 MTU
# 在 VPS 上执行(假设 VPS 公网 MTU 为 1500)
ping -M do -s 1392 <client_ip>
# 如果能通,逐步增大直到不通,最佳值 = 最大通值 + 28
# 常见 MTU 推荐:
# - 以太网: 1420(默认)
# - PPPoE: 1412
# - Cloudflare WARP: 1280
# - 移动网络: 1280在配置文件中设置 MTU:
[Interface]
PrivateKey = ...
Address = 10.66.66.1/24
ListenPort = 51820
MTU = 1420 # 添加此行内核参数优化
cat >> /etc/sysctl.conf << EOF
# WireGuard 性能优化
# 增大网络缓冲区
net.core.rmem_max = 7500000
net.core.wmem_max = 7500000
net.core.rmem_default = 7500000
net.core.wmem_default = 7500000
# 启用 BBR 拥塞控制
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
# 增大 UDP 缓冲区
net.ipv4.udp_rmem_min = 8192
net.ipv4.udp_wmem_min = 8192
EOF
sysctl -pUDP 加速
WireGuard 使用 UDP 传输,部分 ISP 会对 UDP 进行 QoS 限速。以下是缓解方案:
方案一:更换端口
# 尝试使用 53、80、443 等常见端口
# 修改 wg0.conf
ListenPort = 53
# 注意:需要 root 权限绑定 1024 以下端口方案二:UDP over TCP
如果 ISP 完全封锁 UDP,可以使用 udp2raw 将 WireGuard 的 UDP 流量伪装为 TCP:
# 安装 udp2raw
wget https://github.com/wangyu-/udp2raw-tunnel/releases/latest/download/udp2raw_binaries.tar.gz
tar xzf udp2raw_binaries.tar.gz
# 服务端(VPS)
./udp2raw -s -l 0.0.0.0:4096 -r 127.0.0.1:51820 --raw-mode faketcp -a -k "your_password"
# 客户端
./udp2raw -c -l 127.0.0.1:51820 -r <SERVER_IP>:4096 --raw-mode faketcp -a -k "your_password"
# 然后客户端 WireGuard 连接到 127.0.0.1:51820方案三:Phantun
Phantun 是 udp2raw 的轻量替代:
# 服务端
phantun_server --local 51820 --listen 4567
# 客户端
phantun_client --local 51820 --remote <SERVER_IP>:4567Cloudflare WARP 中转
什么是 Cloudflare WARP
Cloudflare WARP 是 Cloudflare 提供的免费 VPN 服务,基于 WireGuard 协议。可以将 WARP 作为 WireGuard 链式代理的出口,实现:
- 隐藏 VPS 真实 IP:流量先到你的 VPS,再通过 WARP 出去
- 解锁 Netflix 等流媒体:WARP 出口 IP 是 Cloudflare 的住宅 IP
- 防止 VPS IP 被封:即使 WARP IP 被封也不影响 VPS
在 VPS 上配置 WARP
# 安装 Cloudflare WARP 客户端
curl -fsSL https://pkg.cloudflareclient.com/pubkey.gpg | gpg --yes --dearmor --output /usr/share/keyrings/cloudflare-warp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/cloudflare-warp-archive-keyring.gpg] https://pkg.cloudflareclient.com/ jammy main" | tee /etc/apt/sources.list.d/cloudflare-client.list
apt update && apt install -y cloudflare-warp
# 注册
warp-cli registration new
# 设置代理模式(仅代理特定流量)
warp-cli mode proxy
# 连接
warp-cli connect
# 验证
curl --proxy socks5h://127.0.0.1:40000 https://cloudflare.com/cdn-cgi/trace/
# warp=on 表示 WARP 已生效WireGuard + WARP 链式配置
修改 WireGuard 服务端配置,将流量通过 WARP 代理出去:
# 修改 wg0.conf 的 PostUp/PostDown
# 将原本的 MASQUERADE 改为通过 WARP SOCKS5 代理
# 安装 redsocks 实现透明代理
apt install -y redsocks
# 配置 redsocks
cat > /etc/redsocks.conf << EOF
base {
log_debug = off;
log_info = on;
log = "syslog:daemon";
daemon = on;
user = redsocks;
group = redsocks;
redirector = iptables;
}
redsocks {
local_ip = 127.0.0.1;
local_port = 12345;
ip = 127.0.0.1;
port = 40000;
type = socks5;
}
EOF
# 启动 redsocks
systemctl restart redsocks
systemctl enable redsocks简化方案:WARP WireGuard 配置
更简单的方式是直接获取 WARP 的 WireGuard 配置,作为额外 Peer 添加:
# 使用 wgcf 工具获取 WARP 配置
wget https://github.com/ViRb3/wgcf/releases/latest/download/wgcf_2.2.0_linux_amd64 -O /usr/local/bin/wgcf
chmod +x /usr/local/bin/wgcf
# 注册 WARP 账户
wgcf register --accept-tos
# 生成 WireGuard 配置
wgcf generate
# 会生成 wgcf-profile.conf,其中包含 WARP 的 Peer 信息
# 将其作为额外 Peer 添加到你的 wg0.conf 中全设备翻墙与分流
全局代理模式
默认配置 AllowedIPs = 0.0.0.0/0, ::/0 表示所有流量走 VPN。这是最简单的全设备翻墙模式。
分流模式
如果只想让特定流量走 VPN,其余走本地网络:
方式一:AllowedIPs 精确控制
[Peer]
PublicKey = <server_public_key>
Endpoint = <SERVER_IP>:51820
# 仅代理这些 IP 段
AllowedIPs = 10.66.66.0/24, 8.8.8.8/32, 1.1.1.1/32
PersistentKeepalive = 25方式二:使用 Policy-Based Routing
在服务端配置基于源 IP 的路由策略:
# 为特定客户端创建单独的路由表
echo "100 wireguard" >> /etc/iproute2/rt_tables
# 在 wg0.conf 中添加
PostUp = ip rule add from 10.66.66.2 table wireguard
PostUp = ip route add default via <WARP_GATEWAY> table wireguard
PostDown = ip rule del from 10.66.66.2 table wireguard
PostDown = ip route del default via <WARP_GATEWAY> table wireguard方式三:客户端使用 Split Tunneling
在 Windows/macOS 客户端中设置:
- WireGuard App → Edit Tunnel → 修改
AllowedIPs - 仅填写需要走 VPN 的目标 IP 段
- 其余流量自动走本地网络
DNS 配置
[Interface]
PrivateKey = ...
Address = 10.66.66.2/24
# 使用自定义 DNS
DNS = 10.66.66.1 # 如果服务端运行了 Pi-hole/AdGuard
# 或使用公共 DNS
DNS = 1.1.1.1, 8.8.8.8安全加固
密钥管理
# 定期轮换密钥(建议每 6 个月)
# 生成新密钥
wg genkey | tee new_server_private.key | wg pubkey > new_server_public.key
# 更新配置文件
sed -i "s/PrivateKey = .*/PrivateKey = $(cat new_server_private.key)/" /etc/wireguard/wg0.conf
# 重启
wg-quick down wg0 && wg-quick up wg0
# 所有客户端也需要更新 PublicKey端口随机化
# 使用随机高端口而非默认 51820
# 修改 wg0.conf
ListenPort = 47821 # 随机端口
# 防火墙同步更新
ufw allow 47821/udp限制 Peer 数量
# 在 wg0.conf 中限制最大 Peer 数
# 通过 iptables 限制并发连接
iptables -A INPUT -p udp --dport 51820 -m connlimit --connlimit-above 10 -j DROP日志与监控
# 启用 WireGuard 日志(需要 debug 模块)
modprobe wireguard debug
# 查看内核日志
dmesg | grep wireguard
# 使用 Prometheus 监控(可选)
# 安装 wireguard_exporter
wget https://github.com/MindFlavor/prometheus_wireguard_exporter/releases/latest/download/prometheus_wireguard_exporter
./prometheus_wireguard_exporter
# 访问 http://localhost:9586/metrics常见问题
Q1: 连接后无法上网,但握手成功
这是最常见的问题,通常是 IP 转发或 NAT 规则未正确配置:
# 1. 检查 IP 转发是否开启
sysctl net.ipv4.ip_forward
# 应输出: net.ipv4.ip_forward = 1
# 2. 检查 iptables NAT 规则
iptables -t nat -L POSTROUTING
# 应包含 MASQUERADE 规则
# 3. 检查网卡的名称是否正确
ip addr
# 确认 PostUp/PostDown 中的 eth0 与实际网卡名一致
# 4. 检查防火墙是否放行
ufw status
# 确认 51820/udp 已放行Q2: 握手失败,无法连接
# 1. 检查服务端是否在运行
wg show
# 应显示 listening port: 51820
# 2. 检查端口是否放行
# VPS 控制台安全组 / UFW / iptables
ufw status
# Oracle Cloud 需要检查 VCN Security List
# 3. 检查公钥是否正确
# 客户端的 [Peer] PublicKey 应与服务端 [Interface] PrivateKey 对应
# 服务端的 [Peer] PublicKey 应与客户端 [Interface] PrivateKey 对应
# 4. 检查 Endpoint 地址
# 客户端配置中的 Endpoint 应为 VPS 的公网 IP
# 5. 测试端口连通性
nc -vu <SERVER_IP> 51820Q3: 速度很慢,无法跑满带宽
# 1. 调整 MTU
# 编辑 wg0.conf 添加 MTU = 1280(保守值)或 1420(默认)
# 2. 开启 BBR 拥塞控制
echo "net.core.default_qdisc = fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control = bbr" >> /etc/sysctl.conf
sysctl -p
# 3. 增大网络缓冲区
echo "net.core.rmem_max = 7500000" >> /etc/sysctl.conf
echo "net.core.wmem_max = 7500000" >> /etc/sysctl.conf
sysctl -p
# 4. 检查是否被 ISP QoS 限速
# 尝试更换端口(53/80/443)
# 或使用 udp2raw 伪装为 TCP
# 5. 检查 VPS 带宽限制
# 部分廉价 VPS 限速严重Q4: UDP 被 ISP 限速/封锁怎么办?
部分中国 ISP 会对 UDP 流量进行 QoS 限速,尤其是在晚高峰时段。解决方案:
- 更换端口:尝试使用 53(DNS)、123(NTP)、443(HTTPS)等常见端口
- udp2raw 伪装:将 UDP 流量伪装为 TCP,绕过 UDP QoS(见上文 UDP 加速章节)
- Phantun:轻量级 UDP-to-TCP 伪装工具
- 双协议部署:同时部署 WireGuard(日常使用)和 V2Ray+Reality(备用),参考 3X-UI 教程
Q5: 切换 WiFi/4G 后断连
WireGuard 设计为无状态协议,理论上切换网络后应自动恢复。如果断连:
- 确认 PersistentKeepalive:客户端配置中
PersistentKeepalive = 25是关键 - 重启客户端:手动关闭再打开 WireGuard 连接
- 检查 MTU:移动网络下 MTU 可能需要降低到 1280
- iOS 后台限制:iOS 可能在后台杀掉 WireGuard 进程,需在设置中允许后台运行
Q6: WireGuard 会泄露真实 IP 吗?
WireGuard 本身设计安全,但需要注意:
- DNS 泄露:如果 DNS 查询走本地而非 VPN 隧道,会泄露访问的域名。在配置中设置
DNS = 1.1.1.1确保 DNS 走隧道 - IPv6 泄露:如果客户端有 IPv6 但 VPN 仅隧道 IPv4,IPv6 流量可能直连。设置
AllowedIPs = 0.0.0.0/0, ::/0同时覆盖 IPv6 - Kill Switch:WireGuard 断连后流量可能直连。在客户端设置防火墙规则阻断非 VPN 流量
- WebRTC 泄露:浏览器 WebRTC 可能泄露本地 IP。使用浏览器插件禁用 WebRTC
Q7: 如何查看谁在用我的 VPN?
# 查看所有已连接的 Peer
wg show
# 关注 latest handshake 字段
# 如果显示 "X seconds/minutes ago",说明该 Peer 最近有活跃连接
# 如果显示 "(none)",说明该 Peer 未连接
# 实时监控
watch -n 5 wg show
# 导出流量统计
wg show wg0 dump | awk '{print $1, $5}'可以配合日志脚本,在 Peer 连接/断开时发送通知:
# 简单监控脚本
while true; do
wg show wg0 latest-handshakes | while read pubkey ts; do
if [ $ts -gt 0 ]; then
echo "$(date): Peer $pubkey active" >> /var/log/wireguard-monitor.log
fi
done
sleep 60
doneQ8: WireGuard 和 Tailscale 有什么区别?
Tailscale 是基于 WireGuard 的商业 VPN 服务,做了大量封装:
| 对比 | WireGuard | Tailscale |
|---|---|---|
| 类型 | 开源协议 | 商业产品(基于 WireGuard) |
| 配置 | 手动管理密钥和配置 | 一键安装,自动密钥管理 |
| NAT 穿透 | 需要公网 IP 或端口转发 | 自动 NAT 穿透(DERP 服务器) |
| ACL | 基于 IP 的防火墙规则 | 基于用户/设备的 ACL 策略 |
| 费用 | 免费 | 免费(个人 100 设备)/ 付费(团队) |
| 控制权 | 完全自主 | 依赖 Tailscale 协调服务器 |
| 适用场景 | 需要完全控制的自建用户 | 快速组建团队 VPN |
如果你不想自己管理 VPS 和密钥,Tailscale 是更好的选择。如果需要完全控制流量走向,自建 WireGuard 更灵活。
总结与推荐
WireGuard 自建 VPN Checklist
✅ VPS 已购买并完成安全加固
✅ 内核版本 ≥ 5.6(WireGuard 已内置)
✅ 服务端 wg0.conf 配置完成
✅ IP 转发已开启
✅ 防火墙已放行 UDP 端口
✅ 客户端配置文件已生成
✅ 全平台客户端已连接并验证
✅ MTU 已优化
✅ BBR 已开启
✅ 多用户管理脚本已就绪推荐部署方案
| 方案 | 适合人群 | 优点 | 缺点 |
|---|---|---|---|
| 纯 WireGuard | 技术爱好者,全设备翻墙需求 | 性能最佳,全设备覆盖 | 隐蔽性低,可能被 QoS |
| WireGuard + udp2raw | ISP 限制 UDP 的用户 | 绕过 UDP QoS | 配置复杂 |
| WireGuard + WARP | 需要解锁流媒体 | 隐藏 VPS IP,住宅出口 | 速度略有损耗 |
| WireGuard + V2Ray 双部署 | 需要兼顾隐蔽性和全设备 | 双保险,互为备份 | 需要维护两套系统 |
与其他自建方案的关系
WireGuard 和 V2Ray/3X-UI 不是互斥的——它们解决不同问题:
- V2Ray/3X-UI:精细化代理控制,高隐蔽性,适合日常科学上网 → 参考 3X-UI 教程
- WireGuard:全设备无感翻墙,高性能,适合路由器/Apple TV 等设备 → 本文
- 机场服务:零配置,即买即用 → 参考 机场推荐
最佳实践是 双部署:VPS 上同时运行 3X-UI(日常代理)和 WireGuard(全设备翻墙),互为补充。
延伸阅读
- 2026 年科学上网协议深度对比
- 3X-UI 面板自建代理完整教程 2026
- 全平台科学上网客户端配置终极指南
- Clash Nyanpasu 使用教程
- 优质机场推荐与选购指南
- 2026 VPS 供应商横向对比与选购指南
- Oracle Cloud 免费实例注册教程
延伸阅读
免责声明
本文仅供技术交流和学习参考。涉及第三方服务的链接可能包含 sponsored 标记,请自行核实服务条款、价格和可用性,并遵守当地法律法规。