1. Mihomo 核心命令行(CLI)参数全解
# 在启动前先行校验配置语法合法性(避免由于 YAML 缩进错误导致服务崩溃)
mihomo -t -d /etc/mihomo -f /etc/mihomo/config.yaml
# 验证输出:
# configuration file /etc/mihomo/config.yaml test is successful 2. 生产级 systemd 单元服务文件编写(带安全权能)
[Unit]
Description=Mihomo (Clash Meta) Daemon Service
After=network.target network-online.target
Wants=network-online.target
[Service]
Type=simple
User=root
WorkingDirectory=/etc/mihomo
ExecStart=/usr/local/bin/mihomo -d /etc/mihomo -f /etc/mihomo/config.yaml
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5s
# 安全与网络特权配置(开启 TUN 模式或绑定 53 DNS 端口所必需)
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target 3. 运维实战操作命令集与生命周期管理
# 1. 重新加载 systemd 守护进程以识别新服务文件
sudo systemctl daemon-reload
# 2. 启用开机自启并立即启动 Mihomo 服务
sudo systemctl enable --now mihomo
# 3. 查看实时服务运行状态
sudo systemctl status mihomo
# 4. 实时查看内核滚动日志
sudo journalctl -u mihomo -f -o cat 4. 零中断热重载(Hot-Reload)实战
# 向本地控制器发送 PUT 请求重新加载配置
curl -X PUT -H "Authorization: Bearer 你的Secret密钥" -H "Content-Type: application/json" -d '{"path": "/etc/mihomo/config.yaml"}' http://127.0.0.1:9090/configs ❓ 常见疑问与排查步骤
执行 systemctl start 提示 Active: failed 是什么原因?
通常由于配置语法错误或端口冲突。立即执行 `sudo journalctl -u mihomo -n 30 --no-pager` 查看最后 30 行日志,精准定位错误根因。
服务重启后 Fake-IP 缓存会丢失吗?
不会。Mihomo 会在工作目录下自动维护 `cache.db`(基于 BoltDB 键值库),退出时自动持久化 Fake-IP 映射表,启动时自动恢复。
如何在非 root 用户下运行 systemd 服务?
将 service 文件中的 `User=root` 改为指定用户名,并确保给该二进制文件赋予 Linux 网络权能:`sudo setcap 'cap_net_admin,cap_net_bind_service=+ep' /usr/local/bin/mihomo`。