Shadowsocks 部署指南(sing-box)

Shadowsocks 2022(2022-blake3-*)自带加密,没有 TLS,也不需要证书。旧版方法有重放风险,只用 2022 系列。需要 sing-box 1.14.0 及以上。字段见 Shadowsocks inbound。

准备

  • 带 systemd 的 Linux VPS,有 root 或 sudo;官方安装脚本覆盖 deb、rpm、Arch、OpenWrt。
  • 安全组和本机防火墙放行 TCP 443。需要 UDP 时一并放行。端口未被占用。
  • 一块长度符合加密方法的密钥。

安装

bash
curl -fsSL https://sing-box.app/install.sh | sh
sing-box version

版本必须 ≥ 1.14.0。

密钥

加密方法 密钥长度 说明
2022-blake3-aes-128-gcm 16 字节 有 AES 加速时优先
2022-blake3-aes-256-gcm 32 字节 余量更大
2022-blake3-chacha20-poly1305 32 字节 无 AES 加速时更快;不支持多用户
bash
sing-box generate rand --base64 16
sing-box generate rand --base64 32

密钥必须与客户端完全一致,长度也必须匹配,否则服务起不来或连不上。

服务端

写入 /etc/sing-box/config.json:

json
{
  "log": {
    "level": "info",
    "timestamp": true
  },
  "inbounds": [
    {
      "type": "shadowsocks",
      "listen": "::",
      "listen_port": 443,
      "method": "2022-blake3-aes-128-gcm",
      "password": "CHANGE_THIS_TO_GENERATED_KEY"
    }
  ],
  "outbounds": [
    {
      "type": "direct"
    }
  ]
}
配置项 说明
password 生成的密钥,长度必须匹配加密方法
method 与客户端一致
listen_port 默认 443
listen 默认 ::。禁用 IPv6 时改为 0.0.0.0

network 省略时同时监听 TCP 和 UDP。只要其一就写 "tcp" 或 "udp",不要写 "tcp,udp",也不要写成数组。

多路复用时,入站和出站使用同一段。开启 padding 后,服务端会拒绝未填充的连接:

json
"multiplex": {
  "enabled": true,
  "padding": true
}
bash
sudo chmod 600 /etc/sing-box/config.json
sudo sing-box check -c /etc/sing-box/config.json
sudo systemctl enable sing-box
sudo systemctl restart sing-box

日志:sudo journalctl -u sing-box -e。

客户端

出站必须放在 outbounds 里。本机代理是 127.0.0.1:1080。

json
{
  "log": {
    "level": "info",
    "timestamp": true
  },
  "inbounds": [
    {
      "type": "mixed",
      "listen": "127.0.0.1",
      "listen_port": 1080
    }
  ],
  "outbounds": [
    {
      "type": "shadowsocks",
      "tag": "ss-out",
      "server": "YOUR_SERVER_IP",
      "server_port": 443,
      "method": "2022-blake3-aes-128-gcm",
      "password": "CHANGE_THIS_TO_GENERATED_KEY"
    }
  ]
}

没有 TLS 段。method 和 password 必须与服务端一致。服务端开了 multiplex 时,客户端也要配同样的段。

多用户

顶层 password 仍是服务端密钥,不能删。2022-blake3-chacha20-poly1305 不能多用户。

json
{
  "inbounds": [
    {
      "type": "shadowsocks",
      "listen": "::",
      "listen_port": 443,
      "method": "2022-blake3-aes-128-gcm",
      "password": "SERVER_KEY",
      "users": [
        {
          "name": "user1",
          "password": "USER1_KEY"
        },
        {
          "name": "user2",
          "password": "USER2_KEY"
        }
      ]
    }
  ]
}

客户端 password 是 SERVER_KEY:USER_KEY,服务端密钥在前。

排查

  • 密钥长度:aes-128-gcm 是 16 字节 Base64,其余是 32 字节,且必须是合法 Base64。
  • 多用户时顶层 password 仍必填。
  • network 不要写成 "tcp,udp" 或数组。端口占用看 sudo ss -lntup | grep ':443'。禁用 IPv6 时不要听 ::。
  • 客户端方法和密钥必须同一套。多用户时冒号顺序不能错。multiplex 两侧一致。
  • 少量认证失败多半是扫描。持续失败就核对 Base64,包括结尾 =;泄露后立刻换密钥。

安全

只用 2022 系列。config.json 权限 600。443 上没有 TLS,流量不像 HTTPS。升级后重新执行 sing-box check。