800 字
4 分钟
部署Sing-box搭配Trojan协议

1. 前置准备#

  • 一台有公网 IP 的 VPS(Ubuntu 22.04 / Debian 12 推荐)
  • 一个已解析到服务器 IP 的域名(例如 example.com)
  • 服务器开放端口:80(证书申请)和 443(Trojan 服务)
  • Root 权限

2. 安装 sing-box#

方法一:官方一键脚本(推荐)#

Terminal window
curl -fsSL https://sing-box.app/install.sh | sh

安装完成后会自动创建 systemd 服务。

方法二:APT 仓库安装#

Terminal window
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

验证安装:

Terminal window
sing-box version

3. 生成强密码#

Terminal window
# 生成 16 字节随机密码(推荐)
sing-box generate rand --base64 16
# 或使用 hex
sing-box generate rand --hex 16

将生成的密码保存好,服务器和客户端都需要使用它。

4. 申请 TLS 证书#

使用 sing-box 内置 ACME(certificate_provider)#

sing-box 1.14+ 已将内联 tls.acme 废弃,推荐使用 certificate_provider。

无需提前申请证书,sing-box 启动时会自动申请并续期(需要 with_acme 构建标签,官方包默认支持)。

5. 服务器配置文件#

配置文件路径:/etc/sing-box/config.json

使用内置 ACME 自动证书#

{
"log": {
"level": "info",
"timestamp": true
},
"inbounds": [
{
"type": "trojan",
"tag": "trojan-in",
"listen": "::",
"listen_port": 443,
"users": [
{
"name": "user1",
"password": "这里填写你生成的密码"
}
],
"tls": {
"enabled": true,
"server_name": "example.com",
"certificate_provider": {
"type": "acme",
"domain": ["example.com"],
"email": "your-email@example.com"
}
},
"multiplex": {
"enabled": false,
"padding": true
}
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
}
]
}

提示:如果 443 端口被占用,可将 listen_port 改为其他端口(如 8443),并在客户端对应修改。

6. 启动与管理服务#

Terminal window
# 检查配置是否正确
sudo sing-box check -c /etc/sing-box/config.json
# 启动并设置开机自启
sudo systemctl enable --now sing-box
# 查看状态
sudo systemctl status sing-box
# 实时日志
sudo journalctl -u sing-box -f --output cat
# 重启
sudo systemctl restart sing-box

7. 客户端配置示例#

以 sing-box 客户端为例:

{
"outbounds": [
{
"type": "trojan",
"tag": "proxy",
"server": "example.com",
"server_port": 443,
"password": "这里填写你生成的密码",
"tls": {
"enabled": true,
"server_name": "example.com"
},
"multiplex": {
"enabled": true
}
}
]
}

其他客户端(Clash Meta、v2rayN、SFI 等)也支持 Trojan,填入对应参数即可。

8. 进阶:Fallback 伪装(强烈推荐)#

开启 Fallback 后,非 Trojan 流量会被转发到本地真实网站,提高隐蔽性。

  1. 先在本地 8080 端口运行一个简单网站(可用 Caddy 或 Nginx)。
  2. 在 Trojan inbound 中添加:
"fallback": {
"server": "127.0.0.1",
"server_port": 8080
}

完整示例片段:

{
"type": "trojan",
"listen": "::",
"listen_port": 443,
"users": [...],
"tls": {...},
"fallback": {
"server": "127.0.0.1",
"server_port": 8080
}
}

9. 常见问题与排查#

问题解决方法
证书申请失败确认域名解析正确,80 端口未被占用
服务无法启动运行 sing-box check 查看具体错误
客户端连接失败检查密码、域名、端口、防火墙是否放行
日志报权限错误确保 sing-box 用户能读取证书文件
ACME 不工作确认使用的是官方构建包(包含 with_acme)

10. 安全建议#

  1. 使用强随机密码。
  2. 尽量开启 multiplex 并配合 Fallback。
  3. 定期更新 sing-box:sudo apt update && sudo apt upgrade sing-box 或重新运行安装脚本。
  4. 限制服务器 SSH 登录,开启防火墙仅放行必要端口。
  5. 生产环境建议使用手动证书 + 自动续期,稳定性更高。
部署Sing-box搭配Trojan协议
https://blog.karinuy.com/posts/singbox-trojan/
作者
Karinuy
发布于
2026-11-18
许可协议
CC BY-NC-SA 4.0