文章目录[隐藏]
- 一、方案说明
- 二、确认 IPv6 正常
- 三、安装依赖
- 四、创建 YouTube IPv6 ipset
- 五、配置 dnsmasq 自动识别 YouTube
- 六、启动 dnsmasq
- 七、测试 ipset 是否正常
- 八、创建 IPv6 路由表
- 九、添加 IPv6 路由表
- 十、配置 nftables
- 十一、启动 nftables
- 十二、添加策略路由
- 十三、重要:不要重复执行 ip rule
- 十四、创建 nftables 与 ipset 同步脚本
- 十五、给予权限
- 十六、执行同步
- 十七、检查 nft set
- 十八、添加自动同步(重要)
- 十九、不会影响什么?
- 二十、最终效果
- 二十一、如何确认已经成功?
- 二十二、适合长期使用吗?
已经避开了你遇到的所有坑:
- nftables 误写成 firewall
- 重复 ip rule
- nft set 与 ipset 不同步
- 默认路由误修改
- SSH/端口被锁
- Xboard-node 坑
- Soga Pro routing 坑
一、方案说明
本方案适用于:Debian 11/12, Xboard, Soga, Xray, sing-box, Reality, Hysteria2, TUIC
目标:
| 流量 | 出口 |
|---|---|
| YouTube | IPv6 |
| 其它所有网站 | IPv4 |
特点:
- 不需要 Soga Pro
- 不需要 Xboard-node
- 不需要 sing-box routing
- 不影响面板 / 节点 / SSH / Reality / Hysteria2 / TUIC
- 系统层分流,长期稳定
原理:
客户端 → Soga/Xray → Linux 系统 → nftables 识别 YouTube → IPv6 出口
二、确认 IPv6 正常
curl -6 ip.sb
如果返回 IPv6,说明 IPv6 正常。查看 IPv6 路由:
ip -6 route
找到 default via xxxx dev xxxx,例如:default via 2001:470:18:299::1 dev he-ipv6。
记住:IPv6 网关 和 网卡名,后面会用到。
三、安装依赖
apt update
apt install -y dnsmasq nftables ipset
四、创建 YouTube IPv6 ipset
ipset create youtube6 hash:ip family inet6
ipset list youtube6
正常会显示:Number of entries: 0
五、配置 dnsmasq 自动识别 YouTube
创建 /etc/dnsmasq.d/youtube.conf:
ipset=/youtube.com/youtube6
ipset=/googlevideo.com/youtube6
ipset=/ytimg.com/youtube6
ipset=/youtu.be/youtube6
ipset=/youtubei.googleapis.com/youtube6
ipset=/ggpht.com/youtube6
六、启动 dnsmasq
systemctl restart dnsmasq
systemctl enable dnsmasq
七、测试 ipset 是否正常
nslookup youtube.com 127.0.0.1
ipset list youtube6
正常应看到:
Members:
2404:xxxx:xxxx
八、创建 IPv6 路由表
编辑 /etc/iproute2/rt_tables,末尾添加:
100 youtubev6
九、添加 IPv6 路由表
ip -6 route add default via 你的 IPv6 网关 dev 你的网卡 table youtubev6
示例:
ip -6 route add default via 2001:470:18:299::1 dev he-ipv6 table youtubev6
查看:
ip -6 route show table youtubev6
十、配置 nftables
编辑 /etc/nftables.conf:
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority filter; policy accept;
}
chain forward {
type filter hook forward priority filter; policy accept;
}
chain output {
type filter hook output priority filter; policy accept;
}
}
table inet mangle {
set youtube6 {
type ipv6_addr
flags dynamic
}
chain output {
type route hook output priority mangle;
ip6 daddr @youtube6 meta mark set 6
}
}
十一、启动 nftables
systemctl enable nftables
systemctl restart nftables
nft list ruleset
十二、添加策略路由
ip -6 rule add fwmark 6 table youtubev6
ip -6 rule
正常应只有一条:fwmark 0x6 lookup youtubev6
十三、重要:不要重复执行 ip rule
如果重复执行会出现多条重复规则。
ip -6 rule del fwmark 6 table youtubev6
删到只剩一条即可。
十四、创建 nftables 与 ipset 同步脚本
原因:dnsmasq 使用 ipset,nftables 使用 nft set,两者不会自动同步。
创建 /usr/local/bin/youtube-sync.sh:
#!/bin/bash
nft flush set inet mangle youtube6
ipset list youtube6 | grep -E '^[0-9a-f:]' | while read ip; do
nft add element inet mangle youtube6 { $ip }
done
十五、给予权限
chmod +x /usr/local/bin/youtube-sync.sh
十六、执行同步
/usr/local/bin/youtube-sync.sh
十七、检查 nft set
nft list set inet mangle youtube6
正常应看到 IPv6 地址,说明分流已生效。
十八、添加自动同步(重要)
crontab -e
添加:
*/5 * * * * /usr/local/bin/youtube-sync.sh >/dev/null 2>&1
每 5 分钟自动同步 YouTube IPv6 地址。
十九、不会影响什么?
本方案不会影响:SSH、Xboard、Soga、Xray、Reality、Hysteria2、TUIC、节点端口、TCP/UDP 监听。
原因:只修改 OUTPUT 出站 routing,不修改 INPUT/FORWARD。
二十、最终效果
| 网站 | 出口 |
|---|---|
| youtube.com | IPv6 |
| googlevideo.com | IPv6 |
| ytimg.com | IPv6 |
| OpenAI | IPv4 |
| Netflix | IPv4 |
| TikTok | IPv4 |
| 其它全部 | IPv4 |
二十一、如何确认已经成功?
nft list set inet mangle youtube6
ip -6 rule
ss -tunp | grep googlevideo
tcpdump -i any ip6 and port 443 -n
二十二、适合长期使用吗?
适合。不依赖面板 / Soga Pro / Xboard-node / sing-box routing / API / 核心版本。
属于 Linux 系统层策略路由,稳定性非常高。

