Appearance
完整教程 (TYPE-C) Sing-box 全平台配置指南:从 JSON 语法模型、出入站设计到高性能 TUN 模式全景实操
Sing-box 是由 SagerNet 开源社区打造的下一代通用通用网络代理核心。它以惊人的超低内存开销(通常仅为传统代理核心的 1/3)、现代化的纯模块化设计以及对前沿协议(VLESS Reality、Hysteria 2、TUIC v5、ShadowTLS)的一流原生支持,成为资深开发者与网络极客的终极首选。
然而,由于 Sing-box 彻底抛弃了过时的 YAML 语法,全面拥抱严谨的 JSON/JSON5 规范与二进制 Rule-set 规则集体系,许多初学者在面对其高度抽象的配置文件结构时往往感到无从下手。
本文将为您从零解构 Sing-box 的配置模型,提供一份开箱即用、包含完整智能 DNS 分流与全流量 TUN 虚拟网卡接管的生产级配置模板,并深入剖析其高级调优技巧。
一、 学习目标与准备工作
1. 学习目标
- 理解 Sing-box 的四大核心设计模块:
log、dns、inbounds、outbounds、route。 - 掌握通过 JSON 语法编写支持 VLESS Reality / Hysteria 2 的出站节点。
- 掌握 TUN 虚拟网卡入站配置,实现内核态全流量透明接管。
- 理解 Remote Rule-set (SRS 二进制规则集) 的工作原理,实现极速规则匹配。
- 掌握常见启动报错(如权限不足、端口冲突、语法错误)的排查与修复。
2. 准备工作清单
- 客户端下载:前往 Sing-box 官方 GitHub Releases 页面,下载对应平台的最新二进制核心或 GUI 客户端(Windows / macOS / Linux / Android)。
- 权限要求:Windows 环境下需具备管理员权限(用于初始化
singbox-tun虚拟网卡驱动)。
二、 Sing-box 核心配置模型深度拆解
mermaid
graph TD
subgraph 1. 入站层 (Inbounds)
In1[TUN 虚拟网卡: 172.19.0.1/30] --> Core[Sing-box 路由核心引擎 (Route Engine)]
In2[Mixed 本地混合端口: 127.0.0.1:2080] --> Core
end
subgraph 2. 路由与 DNS 决策层 (Route & DNS)
Core --> DNS{智能 DNS 分流}
DNS -->|国内域名 *.cn / QQ / 微信| DirectDNS[国内 DoH: 阿里 / 腾讯 DNS]
DNS -->|境外受限域名 Google / OpenAI| ProxyDNS[境外 DoH: 1.1.1.1 / 8.8.8.8]
Core --> RuleMatch{Rule-set 规则集匹配}
end
subgraph 3. 出站层 (Outbounds)
RuleMatch -->|直连规则 geosite-cn / geoip-cn| OutDirect[Direct 出站 (直接访问)]
RuleMatch -->|广告拦截规则 geosite-category-ads| OutBlock[Block 出站 (丢弃流量)]
RuleMatch -->|默认代理规则| OutSelector[Selector 节点选择组 (Reality / Hysteria 2)]
end三、 生产级全功能配置文件模板 (config.json)
以下配置文件经过生产环境严格验证,集成了 TUN 虚拟网卡全接管、Fake-IP 智能解析、中国大陆白名单直连与境外自动分流:
json
{
"log": {
"disabled": false,
"level": "info",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "dns_direct",
"address": "https://223.5.5.5/dns-query",
"detour": "direct"
},
{
"tag": "dns_proxy",
"address": "https://1.1.1.1/dns-query",
"detour": "select-node"
},
{
"tag": "dns_fakeip",
"address": "fakeip"
}
],
"rules": [
{
"outbound": "any",
"server": "dns_direct"
},
{
"rule_set": "geosite-cn",
"server": "dns_direct"
},
{
"query_type": ["A", "AAAA"],
"server": "dns_fakeip"
}
],
"fakeip": {
"enabled": true,
"inet4_range": "198.18.0.0/15",
"inet6_range": "fc00::/18"
},
"strategy": "ipv4_only"
},
"inbounds": [
{
"type": "tun",
"tag": "tun-in",
"interface_name": "singbox-tun",
"inet4_address": "172.19.0.1/30",
"auto_route": true,
"strict_route": true,
"stack": "system",
"sniff": true,
"sniff_override_destination": true
},
{
"type": "mixed",
"tag": "mixed-in",
"listen": "127.0.0.1",
"listen_port": 2080
}
],
"outbounds": [
{
"type": "selector",
"tag": "select-node",
"outbounds": [
"vless-reality-node",
"hysteria2-node",
"direct"
]
},
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
},
{
"type": "dns",
"tag": "dns-out"
},
{
"type": "vless",
"tag": "vless-reality-node",
"server": "your-server.com",
"server_port": 443,
"uuid": "your-uuid-here",
"flow": "xtls-rprx-vision",
"tls": {
"enabled": true,
"server_name": "gateway.icloud.com",
"utls": {
"enabled": true,
"fingerprint": "chrome"
},
"reality": {
"enabled": true,
"public_key": "your-public-key-here",
"short_id": "your-short-id-here"
}
}
},
{
"type": "hysteria2",
"tag": "hysteria2-node",
"server": "your-server.com",
"server_port": 443,
"up_mbps": 50,
"down_mbps": 300,
"password": "your-password-here",
"tls": {
"enabled": true,
"server_name": "your-server.com",
"alpn": ["h3"]
}
}
],
"route": {
"rule_set": [
{
"tag": "geosite-cn",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-cn.srs",
"download_detour": "select-node"
},
{
"tag": "geoip-cn",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geoip/rule-set/geoip-cn.srs",
"download_detour": "select-node"
}
],
"rules": [
{
"protocol": "dns",
"outbound": "dns-out"
},
{
"rule_set": "geosite-cn",
"outbound": "direct"
},
{
"rule_set": "geoip-cn",
"outbound": "direct"
},
{
"ip_is_private": true,
"outbound": "direct"
}
],
"auto_detect_interface": true
}
}四、 常见问题深度解答 (FAQ 10 问 10 答)
1. 为什么运行 sing-box run 提示“configure tun interface: Access is denied”?
创建 TUN 虚拟网卡需要操作系统的网络底层驱动级权限。在 Windows 下必须在“管理员:PowerShell”中运行;在 Linux 下必须使用 sudo sing-box run -c config.json。 2. 什么是 .srs 格式规则集?为什么它比传统的文本规则更快?
.srs 是 Sing-box 专用的二进制预编译规则集(Rule-Set)。相比于每次启动解析数万行文本域名,二进制规则集采用内存映射与树状索引结构,匹配速度提升上百倍,启动耗时低于 10ms。 3. Sing-box 和 Clash Verge Rev 相比,内存占用到底差多少?
在同等加载数万条规则集和 50+ 节点的环境下,Clash Verge Rev(含图形界面)常驻内存约 50-80MB,而纯核心版 Sing-box 仅常驻 15-25MB,非常适合在廉价云主机或软路由中作为系统服务静默运行。4. 为什么开启 TUN 后无法访问局域网打印机或 NAS?
请确保在 route 规则中加入了{"ip_is_private": true, "outbound": "direct"} 规则,强制所有私有网段(192.168.x.x / 10.x.x.x)直连绕过代理。 五、 关联教程与技术百科
- 图形客户端教程:Clash Verge Rev 完整实战教程
- 弱网协议深度解析:Hysteria 2 协议深度解析
- 协议对比:SS vs VMess vs VLESS 演进史
- 优质专线推荐:2026 科学上网机场推荐指南