Linux 安装 Xray && 配置 Hysteria 2

准备

本文仅限于学习交流使用,严禁用于非法途径!

若读者因为浏览本文后做出任何非法行为,由该读者承担相应的后果,与作者无关,与平台无关!

本文操作均在 root 用户下操作,如果不是 root 用户,使用 sudo -i 命令切换。

安装 acme.sh

安装一些依赖。

1
2
apt install curl socat cron openssl
systemctl enable --now cron

下载 acme.sh 并签发证书。

1
2
3
curl -fsSL https://get.acme.sh -o /tmp/install-acme.sh
sh /tmp/install-acme.sh email=<YOUR_EMAIL>
/root/.acme.sh/acme.sh --issue --server letsencrypt --standalone -d "<YOUR_SERVER_IP>" --certificate-profile shortlived --keylength ec-256 --days 3

安装 Xray

下载 Xray。

1
curl -fL https://raw.githubusercontent.com/XTLS/Xray-install/main/install-release.sh -o /tmp/install-xray.sh

新建 xray 用户。

1
id xray >/dev/null 2>&1 || useradd --system --user-group --no-create-home --shell /usr/sbin/nologin xray

使用 xray 用户安装 Xray 服务。

因为后面还要修改配置,所以安装后要停止服务。

1
2
bash /tmp/install-xray.sh install -u xray
systemctl stop xray

基础配置

修改配置文件,并配置归属和访问权限。

1
2
3
vim /usr/local/etc/xray/config.json
chown root:xray /usr/local/etc/xray/config.json
chmod 640 /usr/local/etc/xray/config.json

config.json 配置文件样例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{
"log": {
"loglevel": "warning"
},
"inbounds": [
{
"listen": "0.0.0.0",
"port": 443,
"protocol": "hysteria",
"settings": {
"version": 2,
"clients": [
{
"auth": "<YOUR_UUID>"
}
]
},
"streamSettings": {
"network": "hysteria",
"security": "tls",
"tlsSettings": {
"alpn": [
"h3"
],
"certificates": [
{
"certificateFile": "/usr/cert/ip/fullchain.pem",
"keyFile": "/usr/cert/ip/privkey.pem"
}
]
},
"hysteriaSettings": {
"version": 2
},
"finalmask": {
"udp": [
{
"type": "salamander",
"settings": {
"password": "<YOUR_PASSWORD>"
}
}
]
}
}
}
],
"outbounds": [
{
"protocol": "freedom"
}
]
}

配置证书保存目录,并配置归属和访问权限。

1
2
install -d -m 755 /usr/cert
install -d -o root -g xray -m 750 /usr/cert/ip

创建 Xray 服务重启脚本,用于 acme.sh 的 reloadcmd,证书更新后自动重启 Xray 服务。

1
2
3
vim /usr/local/sbin/xray-cert-reload
chown root:root /usr/local/sbin/xray-cert-reload
chmod 750 /usr/local/sbin/xray-cert-reload

xray-cert-reload 文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh
set -eu
PATH=/usr/sbin:/usr/bin:/sbin:/bin
export PATH

chown root:xray /usr/cert/ip/privkey.pem /usr/cert/ip/fullchain.pem
chmod 640 /usr/cert/ip/privkey.pem /usr/cert/ip/fullchain.pem

runuser -u xray -- /usr/local/bin/xray run -test -config /usr/local/etc/xray/config.json

systemctl try-restart xray.service

部署证书。

1
/root/.acme.sh/acme.sh --install-cert -d "<YOUR_SERVER_IP>" --ecc --key-file /usr/cert/ip/privkey.pem --fullchain-file /usr/cert/ip/fullchain.pem --reloadcmd "/usr/local/sbin/xray-cert-reload"

检查证书:

1
2
ls -la /usr/cert/ip
openssl x509 -in /usr/cert/ip/fullchain.pem -noout -dates -ext subjectAltName

检查 Xray 配置:

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

配置无误可启动 Xray 服务,并设置开机自启动。

1
systemctl enable --now xray

检查 Xray 服务状态:

1
2
3
4
systemctl is-enabled xray
systemctl is-active xray
systemctl status xray --no-pager -l
ss -lunp

检查 acme.sh 的自动续期任务存在:

1
2
crontab -l
systemctl is-active cron

端口跳跃

需要用到 iptables,如果没有,需要先安装。同时,为实现持久化保存,还要安装 iptables-persistent。

1
apt install -y iptables iptables-persistent

使用下面的命令确认实际入站网卡:

1
2
3
WAN_IF=$(ip -4 route show default | awk 'NR == 1 {for (i = 1; i <= NF; i++) if ($i == "dev") {print $(i + 1); exit}}')
printf '使用网卡: %s\n' "$WAN_IF"
ip -br addr

确认网卡后,添加 UDP 指定端口范围(例如:20000-29999)转发到本机 UDP 443。

下面的命令会检查规则是否存在,避免重复添加相同规则。

1
2
3
4
5
6
(
set -eu
test -n "$WAN_IF"

iptables -t nat -C PREROUTING -i "$WAN_IF" -p udp --dport <YOUR_PORTS, e.g., 20000-29999> -j REDIRECT --to-ports 443 2>/dev/null || iptables -t nat -A PREROUTING -i "$WAN_IF" -p udp --dport <YOUR_PORTS, e.g., 20000-29999> -j REDIRECT --to-ports 443
)

如果使用云服务器,记得在防火墙放行相应的端口/端口范围。

保存规则并启用开机恢复:

1
2
netfilter-persistent save
systemctl enable --now netfilter-persistent

检查:

1
2
3
iptables -t nat -nvL PREROUTING --line-numbers
iptables -nvL INPUT --line-numbers
systemctl is-enabled netfilter-persistent

客户端

在客户端填写相应的信息,连接测试使用。

V2rayN 客户端的混淆配置可能需要填写 Finalmask,内容如下:

1
2
3
4
5
6
7
8
9
10
{
"udp": [
{
"type": "salamander",
"settings": {
"password": "<YOUR_PASSWORD>"
}
}
]
}

Linux 安装 Xray && 配置 Hysteria 2
https://www.lechnolocy.cn/2026/10/07/Linux 安装 Xray && 配置 Hysteria 2/
作者
Lechnolocy
发布于
2026年10月7日
许可协议