Linux系统命令行配置Clash Meta与Systemd开机自启

更新时间:2026-11-18 适配平台:Linux 难度等级:高级进阶 阅读时长:9 分钟阅读
核心方案:在无桌面GUI的Linux服务器或工作站上,首选轻量高效的 Mihomo (原Clash.Meta) 二进制核心,搭配 Systemd 服务实现无人值守常驻后台与开机自启动。

一、下载并安装 Mihomo 二进制核心

根据您的系统架构(常见为 amd64 或 arm64)下载最新的 release 版本:

# 创建工作目录
sudo mkdir -p /etc/mihomo
cd /tmp
wget https://github.com/MetaCubeX/mihomo/releases/download/v1.18.8/mihomo-linux-amd64-v1.18.8.gz
gunzip mihomo-linux-amd64-v1.18.8.gz
sudo mv mihomo-linux-amd64-v1.18.8 /usr/local/bin/mihomo
sudo chmod +x /usr/local/bin/mihomo

二、拉取并配置订阅文件

使用 curl 下载机场提供的 Clash 配置文件保存为 /etc/mihomo/config.yaml:

sudo curl -L -o /etc/mihomo/config.yaml "https://your-airport-sub-url.com"

同时在 /etc/mihomo 目录下下载 Country.mmdb 全球 IP 地理数据库:

sudo wget -O /etc/mihomo/Country.mmdb https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/release/country.mmdb

三、配置 Systemd 服务实现开机自启

创建服务文件 /etc/systemd/system/mihomo.service:

[Unit]
Description=Mihomo Daemon Service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/mihomo -d /etc/mihomo
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

加载并启动服务:

sudo systemctl daemon-reload
sudo systemctl enable --now mihomo
sudo systemctl status mihomo

四、验证与环境变量配置

设置 HTTP 和 SOCKS5 代理环境变量:

export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
curl -I https://www.google.com

五、相关知识与进阶推荐