Skip to Content
教程Linux 电脑Sing-Box 使用教程 (Linux)

Sing-Box 使用教程 (Linux)

Sing-Box 是一款功能强大的跨平台代理工具。它支持多种协议(如 Shadowsocks、VMess、Trojan、Hysteria2 等)。


仓库安装

使用仓库安装完之后会自动启动systemd服务

“Debian / APT”

sudo mkdir -p /etc/apt/keyrings && sudo curl -fsSL https://sing-box.app/gpg.key -o /etc/apt/keyrings/sagernet.asc && sudo chmod a+r /etc/apt/keyrings/sagernet.asc && echo ' Types: deb URIs: https://deb.sagernet.org/ Suites: * Components: * Enabled: yes Signed-By: /etc/apt/keyrings/sagernet.asc ' | sudo tee /etc/apt/sources.list.d/sagernet.sources && sudo apt-get update && sudo apt-get install sing-box # or sing-box-beta

“Redhat / DNF 5”

sudo dnf config-manager addrepo --from-repofile=https://sing-box.app/sing-box.repo && sudo dnf install sing-box # or sing-box-beta

“Redhat / DNF 4”

sudo dnf config-manager --add-repo https://sing-box.app/sing-box.repo && sudo dnf -y install dnf-plugins-core && sudo dnf install sing-box # or sing-box-beta

托管安装

类型平台命令链接
AURArch Linux? -S sing-boxAUR package
nixpkgsNixOSnix-env -iA nixos.sing-boxnixpkgs unstable package
HomebrewmacOS / Linuxbrew install sing-boxHomebrew package
APKAlpineapk add sing-boxAlpine Linux Edge package
DEBAOSCapt install sing-boxAOSC package

服务管理

对于带有 ystemd 的 Linux 系统,通常安装已经包含 sing-box 服务, 您可以使用以下命令管理服务:

行动命令
启用sudo systemctl enable sing-box
禁用sudo systemctl disable sing-box
启动sudo systemctl start sing-box
停止sudo systemctl stop sing-box
强行停止sudo systemctl kill sing-box
重新启动sudo systemctl restart sing-box
查看日志sudo journalctl -u sing-box --output cat -e
实时日志sudo journalctl -u sing-box --output cat -f

下载配置文件到systemd里指定的配置路径

默认情况下,sing-box 会从 /etc/sing-box/ 目录读取配置文件。 注意: 一般需要加url参数flag到订阅链接后面,否则下载的配置文件格式不正确,参数值为sing-box

sudo curl -o /etc/sing-box/config.json '<订阅链接>?flag=sing-box'

重新启动sing-box服务

sudo systemctl restart sing-box

查看日志

sudo journalctl -u sing-box -f

旁路由透明代理配置

什么是透明代理

透明代理简单地说就是不让被代理的设备感觉到自己被代理了。简单地说就是,被代理的设备上不需要运行任何代理软件(比如 Xray、V2RayNG 等),当你连接上网络时,你的设备已经被代理了。

这也意味着,代理的软件运行在别的地方,比如运行在路由器中,通过路由器上网的设备就自动被代理了。

为什么使用透明代理

你也想会遇到终端设置只有连接wifi的功能而不能运行代理软件的窘境。比如手机、平板等设备,它们通常只支持WiFi连接设置,无法直接安装和配置代理客户端。

透明代理的主要优势包括:

  • 无需在每个设备上安装代理客户端:只需在网关设备(如路由器)上配置一次,所有连接到该网络的设备都会自动受益
  • 统一管理:可以对整个网络的流量进行统一的代理策略管理
  • 更好的用户体验:用户无需手动配置每个设备的代理设置,使用更加便捷
  • 安全性:可以集中控制和监控网络流量,提高安全性

如果你想让 sing-box 代理局域网内的所有设备(即 Linux 机器作为旁路由),需要进行以下配置。

参考项目:sing-box-bypass-router-transparent-proxy-configuration 

下面假设主路由的 IP 为 192.168.31.1,旁路由的 IP 为 192.168.31.6,请根据实际情况替换。

1. 配置旁路由 IP

修改旁路由的 IP 获取方式为手动:

  • 地址:192.168.31.6
  • 子网掩码:255.255.255.0
  • 网关:192.168.31.1
  • DNS:192.168.31.1

2. 修改主路由 DHCP

在主路由的 DHCP 设置中:

  • 网关:192.168.31.6
  • DNS:192.168.31.6

3. 开启 IPv4 转发

检查 IPv4 转发是否已启用:

sysctl net.ipv4.ip_forward

如果输出为 net.ipv4.ip_forward = 0,需要开启它:

echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf sudo sysctl -p

4. 配置 NAT 转发规则

修改 /etc/nftables.conf,添加以下规则:

# 定义表 table ip nat { chain postrouting { type nat hook postrouting priority 100; policy accept; # 对192.168.31.0/24网段(除了192.168.31.1)的流量进行NAT ip saddr 192.168.31.0/24 ip saddr != 192.168.31.1 oif "tun0" masquerade } }

让规则生效:

sudo nft -f /etc/nftables.conf

5. 配置 DNS 监听

修改 /etc/systemd/resolved.conf,添加以下内容:

DNSStubListenerExtra=0.0.0.0 DNSStubListenerExtra=::

让配置生效:

sudo systemctl restart systemd-resolved

6. 设置开机自启

新建 /etc/systemd/system/nftables-post-tun0.service 文件:

[Unit] Description=Apply nftables rules after tun0 is up After=network-online.target Wants=network-online.target [Service] Type=oneshot ExecStart=/bin/bash -c 'until ip link show tun0 &>/dev/null; do sleep 1; done; nft -f /etc/nftables.conf' RemainAfterExit=true [Install] WantedBy=multi-user.target

启用服务:

sudo systemctl daemon-reload sudo systemctl enable nftables-post-tun0.service

完成以上配置后,局域网内的设备都可以通过旁路由进行代理了。

Last updated on