Sing-Box 安装与配置详细教程
一、简介
Sing-Box 是一款高性能的代理平台,支持多种协议(如 VLESS、Trojan、Hysteria2、Shadowsocks 等),适用于科学上网、内网穿透或流量转发等场景。其配置灵活,资源占用低,在 Linux 系统中部署非常方便。
二、安装前准备
环境要求:
支持的 Linux 发行版:Ubuntu 20.04+/Debian 10+
已安装基础软件包(如
curlunzipvim)确保系统已更新至最新:
sudo apt update && sudo apt upgrade -y
三、安装 Sing-Box(推荐官方一键脚本)
方法 A:使用官方一键安装脚本
curl -fsSL https://sing-box.app/install.sh | sudo bash⚠️ 注意:如果网络受限,可以设置代理:
export https_proxy=http://<proxy_ip>:<port>
curl -fsSL https://sing-box.app/install.sh | sudo bash脚本会自动完成以下操作:
下载 Sing-Box 安装包
创建系统用户(如
singbox)配置 Systemd 服务并设置开机自启
创建配置文件路径
/etc/sing-box设置工作目录为
/var/lib/sing-box
四、常见错误排查与解决方案
1. 错误:status=217/USER
现象:
systemctl status sing-box
● sing-box.service loaded failed: No such file or directory
/lib/systemd/system/sing-box.service原因:
Systemd 服务文件缺失或未正确创建
解决步骤:
✅ 检查 Systemd 服务文件是否存在
ls /etc/systemd/system/sing-box.service如不存在,则手动创建该文件:
✅ 创建 systemd 服务文件
sudo vim /etc/systemd/system/sing-box.service将以下内容复制粘贴进来(根据你的实际路径调整):
[Unit]
Description=sing-box service
After=network.target
[Service]
User=singbox
Group=singbox
WorkingDirectory=/var/lib/sing-box
ExecStart=/usr/bin/sing-box -D /var/lib/sing-box -C /etc/sing-box run
Restart=on-failure
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target📌 如果你尚未创建
/var/lib/sing-box,请先执行:
sudo mkdir -p /var/lib/sing-box✅ 汇总配置文件的权限
sudo chown -R singbox:singbox /var/lib/sing-box✅ 重新加载 systemd
sudo systemctl daemon-reload✅ 启动服务并设置开机自启
sudo systemctl start sing-box
sudo systemctl enable sing-box2. 错误:status=203/EXEC(找不到可执行文件)
现象:Failed to start sing-box.service: Unit is not loaded properly.
解决:
1. 检查 Sing-Box 可执行文件路径是否正确:
ls -l /usr/bin/sing-box2. 若不存在,则重新下载安装:
wget https://github.com/SagerNet/sing-box/releases/download/v1.13.0/sing-box_1.13.0_linux_arm64.deb
sudo dpkg -i sing-box_1.13.0_linux_arm64.deb📌 如需安装其他架构,请前往 [GitHub releases 页面](https://github.com/SagerNet/sing-box/releases) 选择对应的版本。
3. 赋予执行权限:
chmod +x /usr/bin/sing-box3. 错误:status=200/CHDIR(工作目录不存在)
现象:Failed to start sing-box.service: Working directory not accessible
解决步骤:
1. 创建工作目录(如未创建):
sudo mkdir -p /var/lib/sing-box2. 设置目录权限:
sudo chown -R singbox:singbox /var/lib/sing-box4. 配置文件 JSON 格式错误
现象:Failed to parse config file.
解决步骤:
1. 使用 sing-box 自带工具检查:
sing-box check -c /etc/sing-box/config.json2. 或使用在线 JSON 校验工具(如 [JSONLint](https://jsonlint.com/))进行格式验证。
3. 修改配置文件后,重启服务:
sudo systemctl restart sing-box5. 端口冲突(bind: address already in use)
解决方法:
sudo ss -tulpn | grep <端口号>若发现有其他服务占用,可选择停止该服务或在 config.json 中修改端口设置。
五、配置文件示例(供参考)
在 /etc/sing-box/config.json 中添加如下结构内容:
{
"inbounds": [
{
"type": "vless",
"listen": "0.0.0.0:443",
"tag": "vless-inbounds",
"settings": {
"clients": [
{
"id": "your-uuid-here",
"flow": ""
}
]
},
"streamSettings": {
"network": "tcp"
}
}
],
"outbounds": [
{
"type": "direct"
}
]
}📌 实际配置请参考官方文档或根据实际需求修改。
六、常用管理命令速查
七、参考资源
官方文档:[https://sing-box.sagernet.org/](https://sing-box.sagernet.org/)
GitHub 项目仓库:[https://github.com/SagerNet/sing-box](https://github.com/SagerNet/sing-box)
配置示例:[https://sing-box.sagernet.org/configuration/](https://sing-box.sagernet.org/configuration/)
一键安装脚本项目(需自行甄别):GitHub 上搜索
sing-box-install
八、附录:操作记录与命令回顾
以下是一些典型的安装和调试命令,供参考:
sudo systemctl status sing-box # 查看服务状态
journalctl -u sing-box -n 30 # 显示最近30行日志
sudo systemctl daemon-reload # 重新加载 systemd 配置