Hysteria2 部署指南(sing-box)

Hysteria2 基于 QUIC,服务端听 UDP。证书用 ACME(推荐,要域名)或自签名。ACME 使用 certificate_providers,bbr_profile 和 gecko 也需要 sing-box 1.14.0 及以上。不要改回已废弃的 tls.acme,它会在 1.16.0 移除。入站是 UDP 443,TCP 443 空着,所以 HTTP-01(TCP 80)和 TLS-ALPN(TCP 443)都可以。域名为 IP 时,Let's Encrypt 会自动使用 shortlived profile,所以推荐域名。字段见 Hysteria2 inbound 与 outbound。

准备

  • 带 systemd 的 Linux VPS,有 root 或 sudo;官方安装脚本覆盖 deb、rpm、Arch、OpenWrt。
  • 一个解析到该 VPS 的域名,以及可收信的邮箱。
  • 安全组和本机防火墙放行 UDP 443。端口跳跃还要放行整个 UDP 范围,例如 20000–40000。
  • ACME 按挑战方式放行 TCP 80 或 TCP 443。
  • 一个较长的随机密码。

安装

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

版本必须 ≥ 1.14.0。

证书

ACME 按下一节配置。自签名不要把 insecure 当默认,也不要用 Ed25519:1.14 客户端默认鹦鹉 Chrome QUIC,Chrome 不声明 Ed25519,握手会失败。ACME 证书不受影响。

bash
sudo openssl ecparam -name prime256v1 -genkey -noout \
  -out /etc/sing-box/server.key

sudo openssl req -x509 -nodes \
  -key /etc/sing-box/server.key \
  -out /etc/sing-box/server.crt \
  -subj "/CN=bing.com" \
  -addext "subjectAltName=DNS:bing.com" \
  -days 825

sudo chmod 600 /etc/sing-box/server.key
sudo chmod 644 /etc/sing-box/server.crt

CN=bing.com 只是示例名。导入客户端并严格校验时,SAN 必须与 server_name 一致。自签名时删掉 certificate_providers,把 tls 换成:

json
"tls": {
  "enabled": true,
  "certificate_path": "/etc/sing-box/server.crt",
  "key_path": "/etc/sing-box/server.key"
}

服务端

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

json
{
  "log": {
    "level": "info",
    "timestamp": true
  },
  "certificate_providers": [
    {
      "type": "acme",
      "tag": "acme_cert",
      "domain": [
        "YOUR_DOMAIN"
      ],
      "email": "YOUR_EMAIL@example.com",
      "data_directory": "/var/lib/sing-box/certmagic"
    }
  ],
  "inbounds": [
    {
      "type": "hysteria2",
      "listen": "::",
      "listen_port": 443,
      "users": [
        {
          "password": "CHANGE_THIS_TO_A_STRONG_PASSWORD"
        }
      ],
      "tls": {
        "enabled": true,
        "server_name": "YOUR_DOMAIN",
        "certificate_provider": "acme_cert"
      }
    }
  ],
  "outbounds": [
    {
      "type": "direct"
    }
  ]
}

data_directory 必须在 /var/lib/sing-box 下。省略时数据跟着 sing-box 用户的 HOME 走,重启后可能写不进去。YOUR_DOMAIN 在 domain[0] 和 tls.server_name 两处必须一致。

配置项 说明
certificate_providers[0].domain[0] 解析到该 VPS 的域名
certificate_providers[0].email 邮箱
users[0].password 客户端密码
tls.server_name 与 domain[0] 一致
listen 禁用 IPv6 时改为 0.0.0.0

up_mbps / down_mbps 是服务端下发的 Brutal 上限,留空则不限速,且与 ignore_client_bandwidth 互斥。未设带宽时,ignore_client_bandwidth 让客户端改用 BBR;已设带宽时则禁止客户端用 BBR。bbr_profile 可选 conservative、standard、aggressive,默认 standard。官方程序的 userpass 在 sing-box 里要把 username:password 整段当作密码。

客户端自己的带宽字段是另一件事:留空则该客户端使用 BBR。

混淆时两端的类型和密码必须一致。gecko 还可设 min_packet_size(默认 512)和 max_packet_size(默认 1200):

json
"obfs": {
  "type": "salamander",
  "password": "OBFS_PASSWORD"
}

未配置 masquerade 时,认证失败返回 404。对象写法与字符串简写都还在:

json
"masquerade": {
  "type": "proxy",
  "url": "https://www.bing.com/",
  "rewrite_host": 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。

客户端

本机代理是 127.0.0.1:1080。ACME 证书不要加 insecure。自签名应导入证书,server_name 填证书里的伪装域名。

json
{
  "log": {
    "level": "info",
    "timestamp": true
  },
  "inbounds": [
    {
      "type": "mixed",
      "listen": "127.0.0.1",
      "listen_port": 1080
    }
  ],
  "outbounds": [
    {
      "type": "hysteria2",
      "tag": "hy2-out",
      "server": "YOUR_SERVER_IP",
      "server_port": 443,
      "password": "CHANGE_THIS_TO_A_STRONG_PASSWORD",
      "tls": {
        "enabled": true,
        "server_name": "YOUR_DOMAIN"
      }
    }
  ]
}

服务端开了 obfs 时,客户端要配同样的类型和密码。误用 Ed25519 证书会握手失败。可以临时设 "disable_chrome_parrot": true 做对比,生产环境应换 ECDSA 或 RSA 证书,而不是关掉鹦鹉。

端口跳跃

示例把 UDP 20000:40000 转到本机 9443,网卡是 eth0。安全组放行这个 UDP 范围。DNAT 之后公网不必再放行 443 或 9443。

把服务端 listen_port 改成 9443 并重启。然后:

bash
sudo apt update
sudo apt install -y iptables-persistent
sudo iptables -t nat -A PREROUTING -i eth0 -p udp --dport 20000:40000 -j DNAT --to-destination :9443
sudo ip6tables -t nat -A PREROUTING -i eth0 -p udp --dport 20000:40000 -j DNAT --to-destination :9443
sudo netfilter-persistent save

网卡不是 eth0 时,用 ip route get 1.1.1.1 查看后替换。客户端只写 server_ports,不要同时写 server_port。hop_interval 默认 30s,需要随机间隔时再加 hop_interval_max。

json
{
  "outbounds": [
    {
      "type": "hysteria2",
      "tag": "hy2-out",
      "server": "YOUR_SERVER_IP",
      "server_ports": [
        "20000:40000"
      ],
      "hop_interval": "30s",
      "password": "CHANGE_THIS_TO_A_STRONG_PASSWORD",
      "tls": {
        "enabled": true,
        "server_name": "YOUR_DOMAIN"
      }
    }
  ]
}

停用时删除对应 NAT 规则并 netfilter-persistent save。

排查

  • 版本是否 ≥ 1.14.0,证书路径和权限是否可读,data_directory 是否可由 sing-box 用户写入。
  • 不要同时设置带宽字段和 ignore_client_bandwidth。UDP 占用看 sudo ss -lunp | grep -E ':443|:9443'。禁用 IPv6 时不要听 ::。
  • 只放行 TCP 不够,Hysteria2 走 UDP。自签名要导入证书,不要长期开 insecure,也不要用 Ed25519。
  • 端口跳跃要求服务端听 9443,客户端只有 server_ports,DNAT 使用 UDP,IPv4 和 IPv6 都有规则,防火墙放行整个范围。
  • 不想限速就删掉 up_mbps / down_mbps。要客户端改用 BBR,只设 "ignore_client_bandwidth": true。

安全

密码用长随机值。config.json 和 server.key 权限 600。优先 ACME。自签用 P-256 或 RSA。升级后重新执行 sing-box check。