Xray安装教程

Xray VLESS + XHTTP + Reality 安装教程

适用系统:OpenCloudOS / RHEL / CentOS / Rocky / AlmaLinux 等 systemd 系统。 示例配置:8443 端口 + XHTTP + Reality,伪装目标 www.microsoft.com。 客户端要求:VLESS + Reality 不支持旧版 Clash for Windows,请使用 Clash Verge Rev / Mihomo Party / Sparkle / FlClash 等基于 Mihomo(Clash Meta)内核的客户端。

1. 安装依赖

dnf makecache
dnf install -y git curl unzip openssl

2. 安装 Xray-core

使用官方安装脚本,自动创建 systemd 服务:

bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install

检查版本:

xray version

3. 生成独立凭据

每台服务器必须独立生成,不要复制其他节点的 UUID、私钥和 ShortId。

# 生成 UUID
xray uuid

# 生成 Reality 密钥对
xray x25519

# 生成 ShortId(8~16 位十六进制)
openssl rand -hex 8

保存好以下信息:

  • 新 UUID
  • 新 Private key(服务端用)
  • 新 Public key(客户端用)
  • 新 ShortId

4. 编写服务端配置

编辑配置文件:

nano /usr/local/etc/xray/config.json

完整示例(请替换 <UUID>、<PRIVATE_KEY>、<SHORT_ID>):

{
  "log": {
    "loglevel": "warning"
  },
  "inbounds": [
    {
      "port": 8443,
      "protocol": "vless",
      "settings": {
        "clients": [
          {
            "id": "<UUID>",
            "flow": ""
          }
        ],
        "decryption": "none"
      },
      "streamSettings": {
        "network": "xhttp",
        "security": "reality",
        "realitySettings": {
          "show": false,
          "dest": "www.microsoft.com:443",
          "xver": 0,
          "serverNames": [
            "www.microsoft.com"
          ],
          "privateKey": "<PRIVATE_KEY>",
          "shortIds": [
            "<SHORT_ID>"
          ]
        },
        "xhttpSettings": {
          "mode": "stream-one",
          "path": "/"
        }
      },
      "sniffing": {
        "enabled": true,
        "destOverride": [
          "http",
          "tls",
          "quic"
        ]
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "freedom",
      "tag": "direct"
    },
    {
      "protocol": "blackhole",
      "tag": "block"
    }
  ]
}

关键点:

  • port 使用 8443。
  • XHTTP 模式下 flow 必须为空字符串。
  • network 为 xhttp。
  • xhttpSettings.mode 为 stream-one。
  • dest 和 serverNames 保持一致,如 www.microsoft.com。
  • 只保留一个 inbound。如果配置里还有 443 inbound 且 443 被占用,删掉 443 那一段。
  • shortIds 建议只保留非空值,不要留空字符串。

5. 测试配置

xray -test -config /usr/local/etc/xray/config.json

看到 Configuration OK 再继续。

6. 启动并检查监听

systemctl enable --now xray
systemctl status xray
ss -tlnp | grep ':8443'

预期:

  • systemctl status xray 显示 active (running)
  • ss 显示 xray 在 0.0.0.0:8443 或 *:8443 监听

如果启动失败,看日志:

journalctl -u xray -n 80 --no-pager

常见错误 bind: address already in use 表示端口被占用。检查:

ss -tlnp | grep ':443'
ss -tlnp | grep ':8443'

如果配置里同时有 443 和 8443 两个 inbound,而 443 被占用,删掉 443 那段,只保留 8443。

7. 放行 firewalld 和云安全组

OpenCloudOS 本机防火墙:

firewall-cmd --permanent --add-port=8443/tcp
firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload
firewall-cmd --list-ports

云服务商安全组入站放行:

  • TCP:22(SSH)
  • TCP:8443(Xray)
  • 其他端口拒绝。

8. 本地验证端口

在你自己的 Windows 电脑上执行:

Test-NetConnection 服务器IP -Port 8443

必须看到:

TcpTestSucceeded : True

如果是 False,按顺序检查:

  1. 云安全组是否放行 TCP 8443
  2. firewalld 是否放行 8443
  3. Xray 是否在监听 8443

9. 提取客户端信息

从配置文件取:

cat /usr/local/etc/xray/config.json

需要确认:

  • port:8443
  • id:UUID
  • serverNames / dest:www.microsoft.com
  • shortIds:取一个非空值
  • privateKey:私钥,不要外发

用私钥推导客户端公钥:

xray x25519 -i "你的privateKey"

输出中的 Public key 就是客户端要填的 public-key。

10. 客户端配置:Clash Verge Rev

在 proxies 里添加节点:

proxies:
  - name: "KR-XHTTP-Reality"
    type: vless
    server: 服务器IP
    port: 8443
    uuid: "服务端UUID"
    network: xhttp
    tls: true
    udp: true
    servername: www.microsoft.com
    reality-opts:
      public-key: "服务端Public key"
      short-id: "服务端ShortId"
    xhttp-opts:
      path: "/"
      mode: "stream-one"
    client-fingerprint: firefox

注意:

  • XHTTP 模式不要写 flow 字段。
  • mode、path 必须与服务端一致。
  • uuid、public-key、short-id 必须是该服务器自己的值,不能沿用其他节点。

策略组示例:

proxy-groups:
  - name: "PROXY"
    type: select
    proxies:
      - "KR-XHTTP-Reality"
      - DIRECT

rules:
  - MATCH,PROXY

导入 Clash Verge Rev 后:

  1. 订阅页面导入或新建本地配置。
  2. 单击配置卡片激活。
  3. 代理页面选择节点并测延迟。
  4. 首页开启系统代理或 TUN 模式。

11. 常见问题

11.1 bind: address already in use

端口被占用。检查:

ss -tlnp | grep ':443'
ss -tlnp | grep ':8443'

如果配置里同时有 443 和 8443 两个 inbound,而 443 被占用,删掉 443 那段,只保留 8443。

11.2 Configuration OK 但服务启动失败

xray -test 只检查语法,不检查端口占用和运行时权限。看日志:

journalctl -u xray -n 80 --no-pager

或前台运行:

/usr/local/bin/xray run -config /usr/local/etc/xray/config.json

11.3 Reality 警告

REALITY: Choosing "www.microsoft.com" as the target will increase the likelihood...
REALITY: Listening on non-443 ports will increase the likelihood...

这是 Xray 新版风险提示,不是错误。8443 + 热门目标可用,但被封风险高于 443 + 冷门目标。若后续被封,优先换 dest 域名或换回 443。

11.4 只改私钥不够

复制其他服务器配置时,至少同时改:

  • UUID
  • privateKey
  • shortIds

客户端也要换对应的 public-key 和 short-id。只改私钥会导致客户端公钥不匹配,节点连不上。

11.5 shortIds 里有空字符串

"shortIds": ["", "d69589", "ccb8c5417061"]

空字符串表示允许不填 shortId。建议每台服务器独立生成 shortId,并删除空字符串,减少共用入口。

11.6 Clash for Windows 连不上

Clash for Windows 旧内核不支持 VLESS + Reality。换用 Clash Verge Rev、Mihomo Party、Sparkle 或 FlClash。

12. 安全清单

  • 每台服务器独立 UUID、私钥、ShortId。
  • 云安全组只放行 SSH 和代理端口。
  • 关闭 Xray 访问日志。
  • 阻止 BitTorrent,避免 VPS 被商家封禁。
  • 不公开服务器 IP、UUID、私钥、ShortId。
  • 定期更新 Xray-core。
  • 新机器先验证 SSH 可重连,再调整 SSH 端口或防火墙。

评论

公开显示已审核通过的评论

还没有评论

提交后将进入审核

文章导航