Cloudflare WARP 教程:给 VPS 额外添加’原生’ IPv4/IPv6 双栈网络出口
本文适合只有单栈网络的 VPS(IPv6-only 或 IPv4-only)用户。你会学到:用 wgcf 注册 WARP 账户、生成 WireGuard 配置,并按需路由 IPv4/IPv6 流量,以及验证与故障排查方法。
概述
本教程介绍如何使用 Cloudflare WARP 为 VPS 服务器添加额外的 IPv4 或 IPv6 网络支持,实现双栈网络出口。
主要应用场景
1. IPv6-only VPS 获取 IPv4 访问能力
- 无需依赖 NAT64/DNS64
- 突破仅 IPv6 环境的限制
2. IPv4-only VPS 获取 IPv6 连接能力
- 访问 IPv6 网络资源
- 实现双栈网络支持
3. 访问具有原生 IP 检测的服务
- Netflix
- Google Scholar
- ChatGPT
- 其他地理位置敏感的服务
重要限制说明
NAT 访问限制
- 仅支持出站连接(outbound-only)
- 无法接收入站连接
- 基于 NAT 的访问方式
IP 共享问题
- 多个用户共享相同的 IP 地址
- IP 信誉度可能受到影响
- 部分服务可能已将 WARP IP 列入黑名单
性能考虑
- 高峰期可能出现性能下降
- 网络延迟可能增加
技术实现步骤
第一步:安装 WireGuard
Debian/Ubuntu 系统
# 更新软件包列表
apt update
# 安装 WireGuard
apt install -y wireguard
CentOS/RHEL 系统
# CentOS 8 / RHEL 8
dnf install -y wireguard-tools
# CentOS 7 / RHEL 7
yum install -y epel-release elrepo-release
yum install -y kmod-wireguard wireguard-tools
Alpine Linux
apk add wireguard-tools
Arch Linux
pacman -S wireguard-tools
第二步:下载并安装 wgcf 工具
下载 wgcf
# 下载最新版本(Linux amd64)
wget -O /usr/local/bin/wgcf https://github.com/ViRb3/wgcf/releases/latest/download/wgcf_2.2.20_linux_amd64
# 或者使用 curl
curl -fsSL https://github.com/ViRb3/wgcf/releases/latest/download/wgcf_2.2.20_linux_amd64 -o /usr/local/bin/wgcf
# 添加执行权限
chmod +x /usr/local/bin/wgcf
其他架构下载链接
- Linux ARM64:
wgcf_2.2.20_linux_arm64 - Linux ARM:
wgcf_2.2.20_linux_armv7 - Linux 386:
wgcf_2.2.20_linux_386
使用 wgcf 生成配置
# 注册 WARP 账户
wgcf register
# 生成配置文件
wgcf generate
生成的配置文件默认名称为 wgcf-profile.conf。
第三步:修改配置文件
复制配置文件到 WireGuard 目录
# 复制配置文件
cp wgcf-profile.conf /etc/wireguard/wgcf.conf
# 或者直接编辑
vim /etc/wireguard/wgcf.conf
IPv4-only 服务器配置(获取 IPv6 访问)
修改 /etc/wireguard/wgcf.conf:
[Interface]
PrivateKey = <your-private-key>
Address = 172.16.0.2/32, fd01:5ca1:ab1e:xxxx:xxxx:xxxx:xxxx:xxxx/128
DNS = 1.1.1.1, 1.0.0.1, 2606:4700:4700::1111, 2606:4700:4700::1001
MTU = 1280
[Peer]
PublicKey = bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=
AllowedIPs = ::/0
Endpoint = engage.cloudflareclient.com:2408
关键修改点:
AllowedIPs = ::/0- 仅路由 IPv6 流量
IPv6-only 服务器配置(获取 IPv4 访问)
修改 /etc/wireguard/wgcf.conf:
[Interface]
PrivateKey = <your-private-key>
Address = 172.16.0.2/32, fd01:5ca1:ab1e:xxxx:xxxx:xxxx:xxxx:xxxx/128
DNS = 1.1.1.1, 1.0.0.1, 2606:4700:4700::1111, 2606:4700:4700::1001
MTU = 1280
[Peer]
PublicKey = bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=
AllowedIPs = 0.0.0.0/0
Endpoint = [2606:4700:d0::a29f:c001]:2408
关键修改点:
AllowedIPs = 0.0.0.0/0- 仅路由 IPv4 流量Endpoint = [2606:4700:d0::a29f:c001]:2408- 使用 IPv6 端点地址
双栈服务器配置(同时路由 IPv4 和 IPv6)
[Peer]
PublicKey = bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = engage.cloudflareclient.com:2408
第四步:启用 WireGuard 接口
手动启动和停止
# 启动 WireGuard 接口
wg-quick up wgcf
# 停止 WireGuard 接口
wg-quick down wgcf
# 查看接口状态
wg show
设置开机自启
# 启用开机自启
systemctl enable wg-quick@wgcf
# 启动服务
systemctl start wg-quick@wgcf
# 查看服务状态
systemctl status wg-quick@wgcf
# 重启服务
systemctl restart wg-quick@wgcf
# 停止服务
systemctl stop wg-quick@wgcf
# 禁用开机自启
systemctl disable wg-quick@wgcf
第五步:DNS 优化(可选)
修改 DNS 配置
在 /etc/wireguard/wgcf.conf 的 [Interface] 部分修改 DNS:
# 使用 Cloudflare DNS
DNS = 1.1.1.1, 1.0.0.1
# 或使用 Google DNS
DNS = 8.8.8.8, 8.8.4.4
# IPv6 DNS
DNS = 2606:4700:4700::1111, 2606:4700:4700::1001
调整 IPv4/IPv6 优先级
编辑 /etc/gai.conf:
# 优先使用 IPv4
precedence ::ffff:0:0/96 100
# 优先使用 IPv6(默认)
# precedence ::/0 40
第六步:验证连接
测试 IPv4 连接
# 检查 IPv4 地址
curl -4 ip.p3terx.com
curl -4 ifconfig.me
# 测试 IPv4 连通性
ping -4 1.1.1.1
测试 IPv6 连接
# 检查 IPv6 地址
curl -6 ip.p3terx.com
curl -6 ifconfig.me
# 测试 IPv6 连通性
ping -6 2606:4700:4700::1111
查看路由信息
# 查看路由表
ip route show
ip -6 route show
# 查看 WireGuard 接口信息
ip addr show wgcf
故障排查
常见问题
1. 无法连接到 WARP
# 检查 WireGuard 接口状态
wg show
# 查看系统日志
journalctl -u wg-quick@wgcf -f
# 检查防火墙规则
iptables -L -n -v
ip6tables -L -n -v
2. DNS 解析问题
# 测试 DNS 解析
nslookup google.com 1.1.1.1
dig @1.1.1.1 google.com
# 检查 DNS 配置
cat /etc/resolv.conf
3. 重新生成配置
# 删除旧配置
rm -f wgcf-account.toml wgcf-profile.conf
# 重新注册和生成
wgcf register
wgcf generate
第七步:与代理工具集成(可选)
可以将 WARP 与其他代理工具(如 V2Ray、Xray、Clash)结合使用,实现更灵活的流量路由。
高级配置
分流配置(仅特定流量走 WARP)
仅特定 IP 段走 WARP
[Peer]
PublicKey = bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=
# 仅 Netflix IP 段
AllowedIPs = 23.246.0.0/18, 37.77.184.0/21, 45.57.0.0/17
Endpoint = engage.cloudflareclient.com:2408
使用路由表实现策略路由
# 创建自定义路由表
echo "200 warp" >> /etc/iproute2/rt_tables
# 添加路由规则
ip rule add from 172.16.0.2 table warp
ip route add default dev wgcf table warp
# 标记特定流量
iptables -t mangle -A OUTPUT -p tcp --dport 443 -j MARK --set-mark 200
ip rule add fwmark 200 table warp
MTU 优化
[Interface]
MTU = 1420 # 默认值
# 或
MTU = 1280 # 保守值,适用于复杂网络环境
测试最佳 MTU 值:
# 测试 MTU(从 1500 开始递减)
ping -M do -s 1472 1.1.1.1
# 如果失败,减小数值重试
ping -M do -s 1400 1.1.1.1
持久化 iptables 规则
# Debian/Ubuntu
apt install -y iptables-persistent
netfilter-persistent save
# CentOS/RHEL
yum install -y iptables-services
service iptables save
注意事项
- 合理使用: 请遵守 Cloudflare 的服务条款,避免滥用
- 账户限制: 不当使用可能导致账户被限制
- IP 黑名单: 由于滥用问题,越来越多的服务将 WARP IP 列入黑名单
- 性能影响: 使用 WARP 可能会影响网络性能
- 仅出站访问: 无法用于需要入站连接的服务
相关资源
官方文档
工具项目
自动化脚本
- P3TERX 的 WARP 一键脚本
- 其他社区维护的安装脚本
端点地址参考
IPv4 端点
engage.cloudflareclient.com:2408162.159.192.1:2408162.159.193.1:2408
IPv6 端点
[2606:4700:d0::a29f:c001]:2408[2606:4700:d0::a29f:c101]:2408
原文
本文档整理自 P3TERX 的原创教程,仅供学习和参考使用。