sing-box 服务端安装

Youtube视频


相关链接

sing-box官方文档

sing-box官方仓库

文件目录规划

主程序:/usr/bin/sing-box

工作目录:/var/lib/sing-box

配置目录:/etc/sing-box

服务文件:/etc/systemd/system/sing-box.service

新手

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

📢脚本安装只会安装最新的release版本

安装

  • Debian/Ubuntu

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

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

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

卸载

  • Debian/Ubuntu

    # --purge会把配置也删除,保留配置请移除该参数
    sudo systemctl stop sing-box.service
    sudo systemctl disable sing-box.service
    sudo dpkg --purge sing-box
  • CentOS/Fedora

    # --allmatches会把配置也删除,保留配置请移除该参数
    sudo systemctl stop sing-box.service
    sudo systemctl disable sing-box.service
    sudo rpm -e --allmatches sing-box
  • Archlinux

    # -n参数会把配置也删除,保留配置请移除该参数
    sudo systemctl stop sing-box.service
    sudo systemctl disable sing-box.service
    sudo pacman -Rn sing-box

方式二:通过Docker安装

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

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

    sudo systemctl start docker
  3. 新建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
  2. 下载符合你服务器架构的压缩包

    使用deb/rpm/pkg.tar.zst软件包可直接安装带有服务文件管理的sing-box,与脚本安装一样
    • 配置临时环境变量

      export SING_BOX_VERSION=1.8.0-alpha.17
      export ARCH=$(case "$(uname -m)" in 'x86_64') echo 'amd64';; 'x86' | 'i686' | 'i386') echo '386';; 'aarch64' | 'arm64') echo 'arm64';; 'armv7l') echo 'armv7';; 's390x') echo 's390x';; *) echo '不支持的服务器架构';; esac)
      echo -e "\n我的服务器架构是:"$ARCH
    • 下载

      wget https://github.com/SagerNet/sing-box/releases/download/v$SING_BOX_VERSION/sing-box-$SING_BOX_VERSION-linux-$ARCH.tar.gz
  3. 解压

    tar -zxf sing-box-$SING_BOX_VERSION-linux-$ARCH.tar.gz
  4. 将可执行文件移动到/usr/bin目录下

    mv sing-box-$SING_BOX_VERSION-linux-$ARCH/sing-box /usr/bin
  5. 清理

    rm -rf ./sing-box-$SING_BOX_VERSION-linux-$ARCH
    rm -f ./sing-box-$SING_BOX_VERSION-linux-$ARCH.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 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

高手

通过源码编译安装

📢可以安装最新的开发版本,以及构建默认不自带的模块,如:grpcv2ray_api,同理可以移除不需要的模块
  1. 安装go,当前最新版本需要1.20.0以上版本

    • 下载解压

      • amd64

        wget -P /tmp https://go.dev/dl/go1.21.5.linux-amd64.tar.gz
        tar -zxf /tmp/go1.21.5.linux-amd64.tar.gz -C /usr/local
      • arm64

        wget -P /tmp https://go.dev/dl/go1.21.5.linux-arm64.tar.gz
        tar -zxf /tmp/go1.21.5.linux-arm64.tar.gz -C /usr/local
    • 环境变量配置

      echo 'export GOROOT=/usr/local/go' >> ~/.bashrc && echo 'export GOPATH=$HOME/go' >> ~/.bashrc && echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> ~/.bashrc && source ~/.bashrc
    • 验证

      go version
  2. 克隆源码

    git clone https://github.com/SagerNet/sing-box.git /opt/sing-box
  3. 构建

    cd /opt/sing-box
    go build -tags "with_quic with_dhcp with_wireguard with_ech with_utls with_reality_server with_acme with_clash_api with_gvisor with_grpc with_v2ray_api" ./cmd/sing-box
    mv ./sing-box /usr/bin
  4. 验证

    sing-box version
  5. 创建配置目录以及基本配置文件

    mkdir /etc/sing-box
    echo "{}" > /etc/sing-box/config.json
  6. 配置系统服务文件

    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
  7. 刷新服务配置

    systemctl daemon-reload
  8. 卸载

    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

    服务管理

  • 自启+启动

    sudo systemctl enable sing-box.service --now
  • 自启

    sudo systemctl enable sing-box.service
  • 启动

    sudo systemctl start sing-box.service
  • 停止

    sudo systemctl stop sing-box.service
  • 重启

    sudo systemctl restart sing-box.service
  • 查看日志

    # 最新日志
    sudo journalctl -u sing-box --output cat -e
    # 滚动日志
    sudo journalctl -u sing-box --output cat -f
如果觉得我的文章对你有用,请随意赞赏