记录一下直接跑singbox内核,不用gui
起因
用flclash开机自启动在后台很慢,每次还想打开看一眼,寻思可以直接跑内核,很轻量,而且我是自建的,不用频繁的切换节点,于是就就有了这篇文章
过程
安装
https://sing-box.sagernet.org/installation/package-manager/
这是官网,我给arch和mac都配置了,最后管理自启动会有点不同
在这里下载安装singbox内核,恭喜你,拥有了singbox内核
配置
arch
默认配置文件在 /etc/sing-box/config.json
你只需要配置就可以了
我的配置如下,可以参考一下
{
"log": {
"level": "info",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "dns-remote",
"address": "8.8.8.8",
"detour": "select"
},
{
"tag": "dns-direct",
"address": "223.5.5.5",
"detour": "direct"
},
{
"tag": "dns-fakeip",
"address": "fakeip",
"strategy": "ipv4_only"
}
],
"rules": [
{
"domain_suffix": [".cn"],
"action": "route",
"server": "dns-direct"
},
{
"query_type": ["A"],
"action": "route",
"server": "dns-fakeip"
}
],
"final": "dns-remote",
"strategy": "ipv4_only",
"fakeip": {
"enabled": true,
"inet4_range": "198.18.0.0/15"
},
"independent_cache": true
},
"inbounds": [
{
"type": "mixed",
"tag": "mixed-in",
"listen": "0.0.0.0",
"listen_port": 7897,
"sniff": true,
"sniff_override_destination": true
},
{
"type": "tun",
"tag": "tun-in",
"interface_name": "singbox",
"address": ["172.19.0.1/30"],
"auto_route": true,
"strict_route": true,
"sniff": true,
"sniff_override_destination": true
}
],
"outbounds": [
{
"type": "selector",
"tag": "select",
"outbounds": ["node-1", "node-2", "auto", "direct"],
"default": "node-1"
},
{
"type": "urltest",
"tag": "auto",
"outbounds": ["node-1", "node-2"],
"url": "https://www.gstatic.com/generate_204",
"interval": "5m"
},
{
"type": "vless",
"tag": "node-1",
"server": "1.1.1.1",
"server_port": 12345,
"uuid": "00000000-0000-0000-0000-000000000000",
"flow": "xtls-rprx-vision",
"tls": {
"enabled": true,
"server_name": "www.google.com",
"utls": {
"enabled": true,
"fingerprint": "chrome"
},
"reality": {
"enabled": true,
"public_key": "YOUR_REALITY_PUBLIC_KEY_HERE",
"short_id": ""
}
}
},
{
"type": "vless",
"tag": "node-2",
"server": "2.2.2.2",
"server_port": 54321,
"uuid": "11111111-1111-1111-1111-111111111111",
"flow": "xtls-rprx-vision",
"tls": {
"enabled": true,
"server_name": "www.microsoft.com",
"utls": {
"enabled": true,
"fingerprint": "chrome"
},
"reality": {
"enabled": true,
"public_key": "YOUR_REALITY_PUBLIC_KEY_HERE", // 已脱敏
"short_id": ""
}
}
},
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
}
],
"route": {
"default_domain_resolver": "dns-direct",
"rules": [
{
"protocol": "dns",
"action": "hijack-dns"
},
{
"ip_is_private": true,
"action": "route",
"outbound": "direct"
},
{
"domain_suffix": [".cn", ".com.cn", ".net.cn", ".org.cn"],
"action": "route",
"outbound": "direct"
},
{
"domain_keyword": [
"baidu", "taobao", "alibaba", "tencent", "qq", "weixin",
"wechat", "bilibili", "163", "sina", "weibo", "jd",
"douyin", "bytedance", "zhihu"
],
"action": "route",
"outbound": "direct"
}
],
"final": "select",
"auto_detect_interface": true
},
"experimental": {
"cache_file": {
"enabled": true,
"path": "cache.db"
},
"clash_api": {
"external_controller": "127.0.0.1:9090",
"external_ui": "ui",
"secret": ""
}
}
}
配置完成之后,可以启动测试一下,下面是常用的命令
#启动
sudo systemctl start sing-box
# 停止
sudo systemctl stop sing-box
# 重启
sudo systemctl restart sing-box
# 查看状态
sudo systemctl status sing-box
# 查看实时日志
sudo journalctl -u sing-box -f
# 查看最近日志
sudo journalctl -u sing-box -n 50 --no-pager
# 开机自启动并立即运行
sudo systemctl enable --now sing-box
# 取消开机自启动
sudo systemctl disable sing-box
# 检查配置语法
sudo sing-box check -C /etc/sing-box/
mac
用 brew下载,launchctl来管理自启动,系统进程
准备配置文件
# 创建配置文件夹
sudo mkdir -p /opt/homebrew/etc/sing-box/
# 确保文件名为 config.json
sudo nano /opt/homebrew/etc/sing-box/config.json
默认配置文件在/opt/homebrew/etc/sing-box/config.json目录下
配置文件参考如下(此处为严格 JSON,不要加 // 注释)
{
"log": {
"level": "info",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "dns-remote",
"address": "8.8.8.8",
"detour": "select"
},
{
"tag": "dns-direct",
"address": "223.5.5.5",
"detour": "direct"
},
{
"tag": "dns-fakeip",
"address": "fakeip",
"strategy": "ipv4_only"
}
],
"rules": [
{
"domain_suffix": [".cn"],
"action": "route",
"server": "dns-direct"
},
{
"query_type": ["A"],
"action": "route",
"server": "dns-fakeip"
}
],
"final": "dns-remote",
"strategy": "ipv4_only",
"fakeip": {
"enabled": true,
"inet4_range": "198.18.0.0/15"
},
"independent_cache": true
},
"inbounds": [
{
"type": "mixed",
"tag": "mixed-in",
"listen": "0.0.0.0",
"listen_port": 7897,
"sniff": true,
"sniff_override_destination": true
},
{
"type": "tun",
"tag": "tun-in",
"interface_name": "singbox",
"address": ["172.19.0.1/30"],
"auto_route": true,
"strict_route": true,
"sniff": true,
"sniff_override_destination": true
}
],
"outbounds": [
{
"type": "selector",
"tag": "select",
"outbounds": ["dmit-node", "vps-backup", "auto", "direct"],
"default": "dmit-node"
},
{
"type": "urltest",
"tag": "auto",
"outbounds": ["dmit-node", "vps-backup"],
"url": "https://www.gstatic.com/generate_204",
"interval": "5m"
},
{
"type": "vless",
"tag": "dmit-node",
"server": "0.0.0.0",
"server_port": 12345,
"uuid": "00000000-0000-0000-0000-000000000000",
"flow": "xtls-rprx-vision",
"tls": {
"enabled": true,
"server_name": "www.cloudflare.com",
"utls": {
"enabled": true,
"fingerprint": "chrome"
},
"reality": {
"enabled": true,
"public_key": "YOUR_REALITY_PUBLIC_KEY",
"short_id": ""
}
}
},
{
"type": "vless",
"tag": "vps-backup",
"server": "0.0.0.0",
"server_port": 54321,
"uuid": "11111111-1111-1111-1111-111111111111",
"flow": "xtls-rprx-vision",
"tls": {
"enabled": true,
"server_name": "www.amazon.com",
"utls": {
"enabled": true,
"fingerprint": "chrome"
},
"reality": {
"enabled": true,
"public_key": "YOUR_REALITY_PUBLIC_KEY",
"short_id": ""
}
}
},
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
}
],
"route": {
"default_domain_resolver": "dns-direct",
"rules": [
{
"protocol": "dns",
"action": "hijack-dns"
},
{
"ip_is_private": true,
"action": "route",
"outbound": "direct"
},
{
"domain_suffix": [".cn", ".com.cn", ".net.cn", ".org.cn"],
"action": "route",
"outbound": "direct"
},
{
"domain_keyword": ["baidu", "taobao", "tencent", "bilibili"],
"action": "route",
"outbound": "direct"
}
],
"final": "select",
"auto_detect_interface": true
},
"experimental": {
"cache_file": {
"enabled": true,
"path": "cache.db"
},
"clash_api": {
"external_controller": "127.0.0.1:9090",
"external_ui": "ui",
"secret": ""
}
}
}
创建系统守护进程文件
sudo nano /Library/LaunchDaemons/sing-box.plist
内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.sing-box.service</string>
<key>ProgramArguments</key>
<array>
<string>/opt/homebrew/bin/sing-box</string>
<string>run</string>
<string>-c</string>
<string>/opt/homebrew/etc/sing-box/config.json</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/opt/homebrew/var/log/sing-box.log</string>
<key>StandardErrorPath</key>
<string>/opt/homebrew/var/log/sing-box.error.log</string>
</dict>
</plist>
确认路径正确
加载并启动服务
# 0. 准备日志目录
sudo mkdir -p /opt/homebrew/var/log
# 1. 设置文件权限
sudo chown root:wheel /Library/LaunchDaemons/sing-box.plist
sudo chmod 644 /Library/LaunchDaemons/sing-box.plist
# 2. 载入并启动
sudo launchctl bootstrap system /Library/LaunchDaemons/sing-box.plist
常用命令
# 启动
sudo launchctl bootstrap system /Library/LaunchDaemons/sing-box.plist
# 停止
sudo launchctl bootout system /Library/LaunchDaemons/sing-box.plist
# 重启
sudo launchctl bootout system /Library/LaunchDaemons/sing-box.plist && \
sudo launchctl bootstrap system /Library/LaunchDaemons/sing-box.plist
# 查看服务运行状态
sudo launchctl print system/com.sing-box.service
# 查看进程详情
ps aux | grep sing-box | grep -v grep
# 查看实时日志
sudo tail -f /opt/homebrew/var/log/sing-box.log
# 查看最近 50 行错误日志
sudo tail -n 50 /opt/homebrew/var/log/sing-box.error.log
# 检查配置语法
sing-box check -c /opt/homebrew/etc/sing-box/config.json
# 清理缓存数据
sudo rm -rf /opt/homebrew/var/lib/sing-box/*