

Linux 无图形界面服务器代理配置(Clash Verge 方案)
面向纯 SSH 服务器:Clash Verge 无 GUI 不可用时,使用 mihomo 内核完成订阅、选节点、systemd 常驻与终端代理。
你这个场景是“Linux 服务器只有 SSH,没有图形桌面”。
先说结论:无 GUI 环境不能直接运行 Clash Verge(它是基于 WebView 的图形客户端)。
在服务器上应改为使用它的内核方案:mihomo 命令行。
这篇给你一套纯命令行流程,目标是:
- 不依赖图形界面
- 可开机自启
- 默认不走全局代理,需要时再临时
export - 终端里的
curl/apt/git可按命令局部走代理
0. 前置条件#
- 服务器系统:Linux(Ubuntu / Debian / CentOS)
- 你有一个可用的 Clash 订阅链接
- 你有
sudo权限
1. 安装 mihomo(替代 Clash Verge GUI)#
不要先手写通配符解压。先把文件下载到当前目录,再解压。
下面脚本会自动识别架构并从官方最新 release 获取下载链接:
https://github.com/MetaCubeX/mihomo/releases/latest
mkdir -p ~/downloads/mihomo
cd ~/downloads/mihomo
ARCH="$(uname -m)"
if [ "$ARCH" = "x86_64" ]; then
PATTERN='mihomo-linux-amd64[^"]*\.gz'
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
PATTERN='mihomo-linux-arm64[^"]*\.gz'
else
echo "Unsupported arch: $ARCH"
exit 1
fi
URL="$(curl -fsSL https://api.github.com/repos/MetaCubeX/mihomo/releases/latest \
| grep -oE "https://[^\"]+${PATTERN}" \
| head -n 1)"
if [ -z "$URL" ]; then
echo "Failed to resolve download url"
exit 1
fi
curl -L "$URL" -o mihomo.gz
gunzip -f mihomo.gz
chmod +x mihomo
sudo install -m 755 mihomo /usr/local/bin/mihomobash验证安装:
mihomo -vbash你遇到的这类错误:
gzip: mihomo-linux-amd64-*.gz: No such file or directorytext本质是当前目录里没有匹配文件,常见原因有两个:
- 还没下载
mihomo压缩包 - 文件名和通配符不一致
先 ls -lh 看目录里实际文件名,再执行解压。
2. 准备配置目录与 config.yaml#
创建工作目录:
sudo mkdir -p /etc/mihomo/providersbash新建 /etc/mihomo/config.yaml(最小可用模板):
mixed-port: 7890
allow-lan: false
bind-address: 127.0.0.1
mode: rule
log-level: info
external-controller: 127.0.0.1:9090
secret: "your-strong-secret"
proxy-providers:
mysub:
type: http
url: "https://your-subscription-url"
path: ./providers/mysub.yaml
interval: 3600
health-check:
enable: true
interval: 300
url: https://www.gstatic.com/generate_204
proxy-groups:
- name: Proxy
type: select
use:
- mysub
rules:
- MATCH,Proxyyaml如果你“只有一个订阅链接”,只需要改这两处:
secret:改成你自己的随机字符串(例如 16~32 位)url:改成你的订阅链接原文
示例(只演示要改的行):
secret: "8e1d3f4a5b6c7d8e"
url: "https://your-subscription-url"yaml
url必须带引号,避免&、?等字符导致 YAML 解析错误。
写入文件:
sudo tee /etc/mihomo/config.yaml >/dev/null <<'YAML'
mixed-port: 7890
allow-lan: false
bind-address: 127.0.0.1
mode: rule
log-level: info
external-controller: 127.0.0.1:9090
secret: "your-strong-secret"
proxy-providers:
mysub:
type: http
url: "https://your-subscription-url"
path: ./providers/mysub.yaml
interval: 3600
health-check:
enable: true
interval: 300
url: https://www.gstatic.com/generate_204
proxy-groups:
- name: Proxy
type: select
use:
- mysub
rules:
- MATCH,Proxy
YAMLbash如果你的机场给的是“完整配置文件”而不是订阅 URL,可以直接把完整 YAML 覆盖到
/etc/mihomo/config.yaml。
2.1 只有订阅链接时,怎么判断用哪种方式#
先看订阅返回的前几行:
curl -L "你的订阅链接" | head -n 20bash- 如果能看到
proxies:、proxy-groups:、rules:,说明这是完整配置。直接保存为/etc/mihomo/config.yaml即可。 - 如果主要是节点列表,没有完整规则组,就用本文上面的
proxy-providers模板,把链接填到url。
3. 先前台启动测试#
sudo mihomo -d /etc/mihomo -t
sudo mihomo -d /etc/mihomobash关注是否有报错;确认正常后用 Ctrl+C 停止,继续做 systemd 常驻。
4. 配置 systemd 开机自启#
创建服务文件 /etc/systemd/system/mihomo.service:
[Unit]
Description=mihomo Daemon, Another Clash Kernel.
After=network.target NetworkManager.service systemd-networkd.service iwd.service
[Service]
Type=simple
LimitNPROC=500
LimitNOFILE=1000000
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH CAP_DAC_OVERRIDE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH CAP_DAC_OVERRIDE
Restart=always
ExecStartPre=/usr/bin/sleep 1s
ExecStart=/usr/local/bin/mihomo -d /etc/mihomo
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.targetini执行:
sudo systemctl daemon-reload
sudo systemctl enable mihomo
sudo systemctl start mihomo
sudo systemctl status mihomobash4.1 不开机自启,只手动启动#
如果你不希望开机自动拉起,只想“需要时再开”:
sudo systemctl daemon-reload
sudo systemctl disable mihomobash之后按需手动控制:
sudo systemctl start mihomo
sudo systemctl stop mihomo
sudo systemctl restart mihomo
sudo systemctl status mihomobash开机自启和手动启动并不冲突:
enable/disable决定“开机是否自动启动”start/stop决定“当前这一刻是否运行”
查看日志:
journalctl -u mihomo -o cat -fbash5. 无 GUI 下切换节点(API)#
先在当前 shell 定义变量(推荐):
export MIHOMO_API="http://127.0.0.1:9090"
export MIHOMO_SECRET="your-strong-secret"bash
config.yaml里的secret仍然是固定字符串;这里用环境变量只是为了命令行调用更方便。
可选:写入 ~/.bashrc,每次登录自动可用:
echo 'export MIHOMO_API="http://127.0.0.1:9090"' >> ~/.bashrc
echo 'export MIHOMO_SECRET="your-strong-secret"' >> ~/.bashrc
source ~/.bashrcbash如果是多人共用账号,不建议把
MIHOMO_SECRET长期明文写在共享用户的~/.bashrc。
先注意请求头写法,必须是:
-H "Authorization: Bearer ${MIHOMO_SECRET}"bash错误写法(会返回 {"message":"Unauthorized"}):
-H "your-strong-secret"bash查看策略组节点:
curl -s -H "Authorization: Bearer ${MIHOMO_SECRET}" \
"${MIHOMO_API}/proxies" | headbash把 Proxy 组切到指定节点(例如 HK-01):
curl -s -X PUT \
-H "Authorization: Bearer ${MIHOMO_SECRET}" \
-H "Content-Type: application/json" \
-d '{"name":"HK-01"}' \
"${MIHOMO_API}/proxies/Proxy"bash用完后清理变量(可选):
unset MIHOMO_API MIHOMO_SECRETbash6. 让终端命令走代理#
当前 shell 临时生效:
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
export all_proxy=socks5://127.0.0.1:7890bash验证:
curl https://ipinfo.io
curl -I https://www.google.combash取消:
unset http_proxy https_proxy all_proxybash服务器场景不建议写进 ~/.bashrc 全局生效,推荐做成手动开关函数:
proxy_on() {
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
export all_proxy=socks5://127.0.0.1:7890
}
proxy_off() {
unset http_proxy https_proxy all_proxy
}
proxy_status() {
env | grep -i proxy || echo "proxy is off"
}bash把上面函数放进 ~/.bashrc 或 ~/.zshrc 后,按需执行 proxy_on / proxy_off。
7. 常见工具代理#
APT(单次命令局部代理)#
sudo env \
http_proxy=http://127.0.0.1:7890 \
https_proxy=http://127.0.0.1:7890 \
apt updatebash安装包也是同理:
sudo env \
http_proxy=http://127.0.0.1:7890 \
https_proxy=http://127.0.0.1:7890 \
apt install -y <package-name>bashGit(单次命令局部代理)#
git -c http.proxy=http://127.0.0.1:7890 \
-c https.proxy=http://127.0.0.1:7890 \
clone <repo-url>bashpull / push 也可以同样加 -c:
git -c http.proxy=http://127.0.0.1:7890 \
-c https.proxy=http://127.0.0.1:7890 \
pullbash8. 排障速查#
1) curl 仍然直连#
env | grep -i proxy
ss -lntp | grep 7890bash确认代理变量已设置,且 mihomo 确实监听在 127.0.0.1:7890。
2) 服务起不来#
systemctl status mihomo
journalctl -u mihomo -o cat -ebash大多数情况是 config.yaml 语法错误或订阅 URL 无效。
3) API 连接被拒绝#
检查 external-controller 是否是 127.0.0.1:9090,以及 secret 是否和请求头一致。
如果返回:
{"message":"Unauthorized"}json通常不是端口错了,而是认证头格式错误。应使用:
curl -s -H "Authorization: Bearer ${MIHOMO_SECRET}" \
"${MIHOMO_API}/proxies" | headbash4) mihomo.service not found / Unit file does not exist#
这表示 systemd 服务文件还没创建成功,或文件名不对。
先确认二进制路径存在:
which mihomo
ls -l /usr/local/bin/mihomobash再重新创建服务文件:
sudo tee /etc/systemd/system/mihomo.service >/dev/null <<'UNIT'
[Unit]
Description=mihomo Daemon, Another Clash Kernel.
After=network.target NetworkManager.service systemd-networkd.service iwd.service
[Service]
Type=simple
Restart=always
ExecStart=/usr/local/bin/mihomo -d /etc/mihomo
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
UNITbash然后加载并启动:
sudo systemctl daemon-reload
sudo systemctl enable mihomo
sudo systemctl start mihomo
sudo systemctl status mihomobash如果你只想手动启动,不开机自启,把 enable 换成:
sudo systemctl disable mihomo
sudo systemctl start mihomobash9. 最小闭环(无 GUI)#
# 1) 启动服务
sudo systemctl start mihomo
# 2) 配置终端代理
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
export all_proxy=socks5://127.0.0.1:7890
# 3) 验证
curl https://ipinfo.iobash到这里,你在纯 SSH 服务器上的代理就可用了。