新服务器 Mihomo 代理配置教程
📅 基于 2026-06-14 的配置导出
适用于 Ubuntu/Debian 系统,root 用户操作
一、整体架构
┌──────────────────────────────────────────────────────┐
│ 新服务器 │
│ │
│ /etc/systemd/system/clash.service │
│ │ │
│ ▼ │
│ /etc/clash/mihomo (Mihomo Meta v1.19.21) │
│ /etc/clash/config.yaml │
│ /etc/clash/geoip.metadb │
│ /etc/clash/cache.db │
│ │ │
│ ▼ 监听 0.0.0.0:7897 (HTTP + SOCKS5 混合端口) │
│ │
│ 三处环境变量配置让所有工具默认走代理: │
│ ┌─ /etc/profile → export HTTP_PROXY/HTTPS_PROXY │
│ ├─ ~/.bashrc → proxy_on / proxy_off 别名 │
│ ├─ ~/.zshrc → 同上 │
│ └─ npm config → proxy = http://127.0.0.1:7897/ │
└──────────────────────────────────────────────────────┘
二、步骤 1:下载 Mihomo 二进制文件
# 创建目录
mkdir -p /etc/clash
cd /etc/clash
# 下载 Mihomo Meta (Clash Meta 继任者)
# 根据 CPU 架构选择: amd64 / arm64 / armv7 等
# 最新版本见: https://github.com/MetaCubeX/mihomo/releases
ARCH=$(uname -m)
case $ARCH in
x86_64) ARCH="amd64" ;;
aarch64) ARCH="arm64" ;;
armv7l) ARCH="armv7" ;;
esac
# 这里以 v1.19.21 为例,你可以去 GitHub 获取最新版本
VERSION="v1.19.21"
wget "https://github.com/MetaCubeX/mihomo/releases/download/${VERSION}/mihomo-linux-${ARCH}-${VERSION}.gz"
gunzip "mihomo-linux-${ARCH}-${VERSION}.gz"
mv "mihomo-linux-${ARCH}-${VERSION}" /etc/clash/mihomo
chmod +x /etc/clash/mihomo
三、步骤 2:创建 config.yaml 配置文件
cat > /etc/clash/config.yaml << 'EOF'
mixed-port: 7897
allow-lan: true
mode: rule
log-level: info
# ── 代理节点(按需修改) ──
proxies:
- name: "Hysteria2-easytake"
type: hysteria2
server: your-server.com # ← 改成你的服务器地址
port: 443
password: your-password # ← 改成你的密码
alpn:
- h3
sni: your-server.com # ← 改成你的 SNI
skip-cert-verify: false
udp: true
# ── 代理组 ──
proxy-groups:
- name: "Proxy"
type: select
proxies:
- "Hysteria2-easytake"
- "DIRECT"
- name: "Mode"
type: select
proxies:
- "Rule 模式"
- "Global 模式"
- "Direct 直连"
- name: "Rule 模式"
type: select
proxies:
- "Proxy"
- "DIRECT"
- name: "Global 模式"
type: select
proxies:
- "Proxy"
- name: "Direct 直连"
type: select
proxies:
- "DIRECT"
# ── 分流规则 ──
rules:
- GEOIP,CN,DIRECT
- DOMAIN-SUFFIX,baidu.com,DIRECT
- DOMAIN-SUFFIX,bilibili.com,DIRECT
- DOMAIN-SUFFIX,qq.com,DIRECT
- DOMAIN-SUFFIX,weibo.com,DIRECT
- DOMAIN-SUFFIX,alipay.com,DIRECT
- MATCH,Proxy
EOF
配置说明:
mixed-port: 7897— 同时提供 HTTP 和 SOCKS5 代理allow-lan: true— 允许局域网内其他设备使用(不需要就改成 false)mode: rule— 按 rules 分流,中国 IP + 国内网站直连,其余走代理- 代理协议使用的是 Hysteria2,基于 QUIC/UDP,速度快、延迟低
四、步骤 3:下载 GeoIP 数据库
cd /etc/clash
# 从 MetaCubeX 下载 GeoIP 数据库(用于 GEOIP,CN 规则)
wget https://github.com/MetaCubeX/meta-rules-dat/releases/latest/download/geoip.metadb
五、步骤 4:创建 systemd 服务
cat > /etc/systemd/system/clash.service << 'EOF'
[Unit]
Description=Clash mihomo Proxy Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/etc/clash
ExecStart=/etc/clash/mihomo -d /etc/clash
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
# 重载 systemd 配置
systemctl daemon-reload
# 测试配置是否正确
/etc/clash/mihomo -d /etc/clash -t
# 启动服务
systemctl start clash
# 设置开机自启
systemctl enable clash
# 查看运行状态
systemctl status clash
# 验证端口是否监听
ss -tlnp | grep 7897
六、步骤 5:配置系统环境变量(让所有工具走代理)
6.1 系统级默认代理 — /etc/profile
在 /etc/profile 文件末尾添加以下两行:
# 在 /etc/profile 末尾添加
export HTTP_PROXY=http://127.0.0.1:7897
export HTTPS_PROXY=http://127.0.0.1:7897
# 执行命令添加
cat >> /etc/profile << 'EOF'
export HTTP_PROXY=http://127.0.0.1:7897
export HTTPS_PROXY=http://127.0.0.1:7897
EOF
⚠️ 注意:
HTTP_PROXY是大写,对大部分 Linux 工具生效;http_proxy(小写)则只在 shell alias 中按需设置。
6.2 Bash 用户 — 快捷开关别名
在 ~/.bashrc 末尾添加:
cat >> ~/.bashrc << 'EOF'
# Clash 代理快捷开关(mihomo mixed-port: 7897)
alias proxy_on='export http_proxy=http://127.0.0.1:7897; export https_proxy=http://127.0.0.1:7897; export all_proxy=http://127.0.0.1:7897; echo "代理已开启(127.0.0.1:7897)"'
alias proxy_off='unset http_proxy https_proxy all_proxy; echo "代理已关闭"'
alias proxy_status='echo "http_proxy : ${http_proxy:-未设置}"; echo "https_proxy: ${https_proxy:-未设置}"; echo "all_proxy : ${all_proxy:-未设置}"'
EOF
6.3 Zsh 用户 — 快捷开关别名
在 ~/.zshrc 末尾添加:
cat >> ~/.zshrc << 'EOF'
# >>> 代理快捷开关(Clash/Mihomo 默认端口 7897)<<<
alias proxy_on='export http_proxy=http://127.0.0.1:7897 https_proxy=http://127.0.0.1:7897 all_proxy=http://127.0.0.1:7897; echo "✅ 代理已开启(127.0.0.1:7897)"'
alias proxy_off='unset http_proxy https_proxy all_proxy; echo "❌ 代理已关闭"'
alias proxy_status='echo "http_proxy : ${http_proxy:-未设置}"; echo "https_proxy: ${https_proxy:-未设置}"; echo "all_proxy : ${all_proxy:-未设置}"'
EOF
6.4 npm 代理
npm config set proxy http://127.0.0.1:7897/
npm config set https-proxy http://127.0.0.1:7897/
6.5 Git 代理(可选)
# 全局代理
git config --global http.proxy http://127.0.0.1:7897
git config --global https.proxy http://127.0.0.1:7897
七、步骤 6:使配置生效
# 重新登录,或:
source /etc/profile
source ~/.bashrc # bash 用户
source ~/.zshrc # zsh 用户
# 验证代理已生效
echo $HTTP_PROXY # 应输出 http://127.0.0.1:7897
proxy_status # 查看当前 shell 代理状态
# 测试代理是否工作
curl -I https://www.google.com
八、日常使用
proxy_on # 开启代理(当前 shell)
proxy_off # 关闭代理(当前 shell)
proxy_status # 查看代理状态
# 服务管理
systemctl start clash # 启动代理服务
systemctl stop clash # 停止代理服务
systemctl restart clash # 重启代理服务
systemctl status clash # 查看服务状态
# 查看实时日志
journalctl -u clash -f
九、快捷复制版(全新服务器一键配置)
以下为简化版,适合在新服务器上逐段执行(请先修改代理节点信息):
# ──── 1. 安装 Mihomo ────
mkdir -p /etc/clash && cd /etc/clash
# 从这里找到最新版下载链接: https://github.com/MetaCubeX/mihomo/releases
wget https://github.com/MetaCubeX/mihomo/releases/download/v1.19.21/mihomo-linux-amd64-v1.19.21.gz
gunzip mihomo-linux-amd64-v1.19.21.gz
mv mihomo-linux-amd64-v1.19.21 mihomo
chmod +x mihomo
# ──── 2. 下载 GeoIP ────
wget https://github.com/MetaCubeX/meta-rules-dat/releases/latest/download/geoip.metadb
# ──── 3. 创建配置文件 ────
cat > /etc/clash/config.yaml << 'CLASHEOF'
mixed-port: 7897
allow-lan: true
mode: rule
log-level: info
proxies:
- name: "Hysteria2-node"
type: hysteria2
server: easytake.work # ← 修改这里
port: 443
password: Se7RAuFZ8Lzg # ← 修改这里
alpn:
- h3
sni: easytake.work # ← 修改这里
skip-cert-verify: false
udp: true
proxy-groups:
- name: "Proxy"
type: select
proxies:
- "Hysteria2-node"
- "DIRECT"
- name: "Mode"
type: select
proxies:
- "Rule 模式"
- "Global 模式"
- "Direct 直连"
- name: "Rule 模式"
type: select
proxies:
- "Proxy"
- "DIRECT"
rules:
- GEOIP,CN,DIRECT
- DOMAIN-SUFFIX,baidu.com,DIRECT
- DOMAIN-SUFFIX,bilibili.com,DIRECT
- DOMAIN-SUFFIX,qq.com,DIRECT
- DOMAIN-SUFFIX,weibo.com,DIRECT
- DOMAIN-SUFFIX,alipay.com,DIRECT
- MATCH,Proxy
CLASHEOF
# ──── 4. 创建 systemd 服务 ────
cat > /etc/systemd/system/clash.service << 'SERVEOF'
[Unit]
Description=Clash mihomo Proxy Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/etc/clash
ExecStart=/etc/clash/mihomo -d /etc/clash
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
SERVEOF
systemctl daemon-reload
/etc/clash/mihomo -d /etc/clash -t && systemctl start clash && systemctl enable clash
# ──── 5. 配置环境变量 ────
cat >> /etc/profile << 'EOF'
export HTTP_PROXY=http://127.0.0.1:7897
export HTTPS_PROXY=http://127.0.0.1:7897
EOF
# bash 别名
cat >> ~/.bashrc << 'EOF'
# Mihomo 代理快捷开关
alias proxy_on='export http_proxy=http://127.0.0.1:7897; export https_proxy=http://127.0.0.1:7897; export all_proxy=http://127.0.0.1:7897; echo "代理已开启(127.0.0.1:7897)"'
alias proxy_off='unset http_proxy https_proxy all_proxy; echo "代理已关闭"'
alias proxy_status='echo "http_proxy : ${http_proxy:-未设置}"; echo "https_proxy: ${https_proxy:-未设置}"; echo "all_proxy : ${all_proxy:-未设置}"'
EOF
# zsh 别名
cat >> ~/.zshrc << 'EOF'
# >>> 代理快捷开关(Clash/Mihomo 默认端口 7897)<<<
alias proxy_on='export http_proxy=http://127.0.0.1:7897 https_proxy=http://127.0.0.1:7897 all_proxy=http://127.0.0.1:7897; echo "✅ 代理已开启(127.0.0.1:7897)"'
alias proxy_off='unset http_proxy https_proxy all_proxy; echo "❌ 代理已关闭"'
alias proxy_status='echo "http_proxy : ${http_proxy:-未设置}"; echo "https_proxy: ${https_proxy:-未设置}"; echo "all_proxy : ${all_proxy:-未设置}"'
EOF
# npm 代理
npm config set proxy http://127.0.0.1:7897/
npm config set https-proxy http://127.0.0.1:7897/
# ──── 6. 生效 ────
source /etc/profile
source ~/.bashrc 2>/dev/null; source ~/.zshrc 2>/dev/null
echo "✅ 配置完成!运行 proxy_status 查看状态"
十、故障排查
| 问题 | 检查命令 |
|---|---|
| 服务是否运行 | systemctl status clash |
| 端口是否监听 | ss -tlnp | grep 7897 |
| 配置是否有语法错误 | /etc/clash/mihomo -d /etc/clash -t |
| 代理是否生效 | curl -x http://127.0.0.1:7897 -I https://www.google.com |
| 环境变量是否设置 | echo $HTTP_PROXY |
| 查看实时日志 | journalctl -u clash -f |
十一、参考资料
- Mihomo (Clash Meta) 仓库:https://github.com/MetaCubeX/mihomo
- GeoIP 数据库:https://github.com/MetaCubeX/meta-rules-dat
- Hysteria2 协议:https://hysteria.network/
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 kipleyarch@gmail.com