sing-box服务端的安装方式

文件目录规划

主程序:/usr/bin/sing-box
工作目录:/var/lib/sing-box
配置目录:/etc/sing-box
服务文件:/etc/systemd/system/sing-box.service

方式一:通过脚本使用软件包安装

Debian/Ubuntu


bash <(curl -fsSL https://sing-box.app/deb-install.sh)

方式二:通过Docker安装

配置目录映射至宿主机: /etc/sing-box 目录下
1.安装Docker

sudo curl -fsSL https://get.docker.com | bash -s docker

2.启动Docker

sudo systemctl start docker
  1. 新建 docker-compose.yaml 配置文件
mkdir ~/sing-box
cat <<EOF> ~/sing-box/docker-compose.yaml
version: "3.8"
services:
  sing-box:
    image: ghcr.io/sagernet/sing-box
    container_name: sing-box
    restart: always
    volumes:
      - /etc/sing-box:/etc/sing-box/
    command: -D /var/lib/sing-box -C /etc/sing-box/ run
EOF

4.运行

cd ~/sing-box
docker compose up -d

5.卸载

docker compose down
rm -rf /etc/sing-box

方式三:通过下载二进制文件安装

  1. 打开官方仓库releases页面
https://github.com/SagerNet/sing-box/releases
  1. 下载服务器压缩包,以下以amd64为例
    使用deb/rpm/pkg.tar.zst软件包可直接安装带有服务文件管理的sing-box,与脚本安装一样
wget https://github.com/SagerNet/sing-box/releases/download/v1.8.0/sing-box-1.8.0-linux-amd64.tar.gz

3.解压

tar -zxf sing-box-1.8.0-linux-amd64.tar.gz
  1. 将可执行文件移动到/usr/bin 目录下
mv sing-box-1.8.0-linux-amd64/sing-box /usr/bin

5.清理

rm -rf ./sing-box-1.8.0-linux-amd64
rm -f ./sing-box-1.8.0-linux-amd64/.tar.gz

6.创建配置目录以及基本配置文件

mkdir /etc/sing-box
echo "{}" > /etc/sing-box/config.json

7.配置系统服务文件

cat <<EOF> /etc/systemd/system/sing-box.service
[Unit]
Description=sing-box service
Documentation=https://sing-box.sagernet.org
After=network.target nss-lookup.target

[Service]
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
ExecStart=/usr/bin/sing-box -D /var/lib/sing-box -C /etc/sing-box run
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=10s
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target
EOF

8.刷新服务配置

systemctl daemon-reload

9.启动服务

systemctl start sing-box.service

10.查看服务状态

systemctl status sing-box.service

11.查看版本号

sing-box version

12.卸载

systemctl stop sing-box.service
systemctl disable sing-box.service
systemctl daemon-reload
rm -rf /etc/sing-box
rm -rf /var/lib/sing-box
rm -f /usr/bin/sing-box
rm -f /etc/systemd/system/sing-box.service