800 字
4 分钟
部署Sing-box搭配Trojan协议
1. 前置准备
- 一台有公网 IP 的 VPS(Ubuntu 22.04 / Debian 12 推荐)
- 一个已解析到服务器 IP 的域名(例如
example.com) - 服务器开放端口:
80(证书申请)和443(Trojan 服务) - Root 权限
2. 安装 sing-box
方法一:官方一键脚本(推荐)
curl -fsSL https://sing-box.app/install.sh | sh安装完成后会自动创建 systemd 服务。
方法二: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: debURIs: https://deb.sagernet.org/Suites: *Components: *Enabled: yesSigned-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验证安装:
sing-box version3. 生成强密码
# 生成 16 字节随机密码(推荐)sing-box generate rand --base64 16
# 或使用 hexsing-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. 启动与管理服务
# 检查配置是否正确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-box7. 客户端配置示例
以 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 流量会被转发到本地真实网站,提高隐蔽性。
- 先在本地 8080 端口运行一个简单网站(可用 Caddy 或 Nginx)。
- 在 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. 安全建议
- 使用强随机密码。
- 尽量开启
multiplex并配合 Fallback。 - 定期更新 sing-box:
sudo apt update && sudo apt upgrade sing-box或重新运行安装脚本。 - 限制服务器 SSH 登录,开启防火墙仅放行必要端口。
- 生产环境建议使用手动证书 + 自动续期,稳定性更高。
部署Sing-box搭配Trojan协议
https://blog.karinuy.com/posts/singbox-trojan/