跳到主要内容

mihomo-网络代理

· 阅读需 3 分钟
Chius
通信工程本科生

mihomo 是一个轻量级的网络代理工具,支持多种协议和灵活的配置选项。 本文将介绍如何在 Raspberry Pi 上安装和配置 mihomo,以实现高效的网络代理服务。

下载并安装 mihomo

访问 mihomo 的 GitHub 发布页面

wget https://github.com/MetaCubeX/mihomo/releases/download/v1.19.16/mihomo-linux-arm64-v1.19.16.gz
gunzip mihomo-linux-arm64-v1.19.16.gz
mv mihomo-linux-arm64-v1.19.16 /usr/local/bin/mihomo
chmod +x /usr/local/bin/mihomo

创建配置文件

/etc/mihomo/ 目录下创建配置文件 config.yaml

mkdir -p /etc/mihomo
nano /etc/mihomo/config.yaml

自行添加配置文件。

运行 mihomo

使用以下命令启动 mihomo:

mihomo -d /etc/mihomo

设置开机自启

创建 systemd 服务文件:

nano /etc/systemd/system/mihomo.service

写入以下内容:

[Unit]
Description=mihomo daemon
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
# 请确认你的 mihomo 可执行文件路径是否正确
ExecStart=/usr/local/bin/mihomo -d /etc/mihomo
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

启动mihomo服务

# 重载systemd配置
systemctl daemon-reload
# 启用开机自启
systemctl enable mihomo
# 启动服务
systemctl start mihomo
# 查看运行状态
systemctl status mihomo
# 查看日志
journalctl -u mihomo -f

常见问题

如果运行mihomo -d /etc/mihomo时输入如下错误:

root@raspberrypi:/usr/local/bin# mihomo -d /etc/mihomo
INFO[2025-11-20T09:56:50.822634529+08:00] Start initial configuration in progress
INFO[2025-11-20T09:56:50.824679101+08:00] Geodata Loader mode: memconservative
INFO[2025-11-20T09:56:50.824722192+08:00] Geosite Matcher implementation: succinct
INFO[2025-11-20T09:56:50.833586577+08:00] Can't find MMDB, start download

这是因为缺少 GeoIP 数据库文件,可以等待其自动下载,或者访问gitee下载链接手动下载并改名为geoip.metadb放置到 /etc/mihomo/ 目录下。

为常用服务添加代理

配置命令行代理

编辑 ~/.bashrc 文件,添加以下内容:

export http_proxy="http://127.0.0.1:{config里配置的端口}"
export https_proxy="http://127.0.0.1:{config里配置的端口}"

保存后运行 source ~/.bashrc 使配置生效。

配置 git https 代理

运行以下命令:

git config --global http.proxy http://127.0.0.1:{config里配置的端口}
git config --global https.proxy http://127.0.0.1:{config里配置的端口}

或者在 ~/.gitconfig 文件中添加:

[http]
proxy = http://127.0.0.1:{config里配置的端口}
[https]
proxy = http://127.0.0.1:{config里配置的端口}

配置 docker 代理

创建或编辑 /etc/systemd/system/docker.service.d/http-proxy.conf 文件:

sudo nano /etc/systemd/system/docker.service.d/http-proxy.conf

添加以下内容:

[Service]
Environment="HTTP_PROXY=http://127.0.0.1:{config里配置的端口}"
Environment="HTTPS_PROXY=http://127.0.0.1:{config里配置的端口}"

保存后运行以下命令使配置生效:

sudo systemctl daemon-reload
sudo systemctl restart docker