侧边栏壁纸
  • 累计撰写 65 篇文章
  • 累计创建 80 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

Debian 13 部署 Mihomo 透明旁路由与 MetaCubeXD 面板完整教程

Seger
2026-06-29 / 0 评论 / 0 点赞 / 0 阅读 / 3,231 字

tags:

  • Debian
  • Mihomo
  • 旁路由
  • MetaCubeXD
  • 透明代理
    categories:
  • 网络服务

Debian 13 部署 Mihomo 透明旁路由与 MetaCubeXD 面板完整教程

本文记录一套从 Debian 13 安装完成后开始,部署 Mihomo 代理核心、MetaCubeXD 面板,并实现局域网设备通过修改网关实现透明分流的完整流程。

最终实现效果:

  • Debian 代理机 IP:10.168.1.3
  • 主路由 IP:10.168.1.1
  • 手动代理端口:10.168.1.3:7890
  • MetaCubeXD 面板:http://10.168.1.3:9091
  • Mihomo API:http://10.168.1.3:9090
  • 支持手机、电视、电脑只修改网关为 10.168.1.3 后自动分流
  • 中国内地 IP 直连,Google / YouTube / ChatGPT 等按规则走对应代理组
  • 节点域名使用动态 IP 时,不写死 /etc/hosts,由 Mihomo 自动解析

一、网络环境

本文使用的网络环境如下:

主路由:10.168.1.1
Debian 代理机:10.168.1.3
Debian 网卡:ens18
局域网网段:10.168.1.0/24

如果你的网卡名称不是 ens18,先执行:

ip addr

找到实际网卡名称,然后把后文所有 ens18 替换成你的实际网卡名称。


二、设置 Debian 静态 IP

如果安装 Debian 时已经设置好静态 IP,可以跳过本节。

编辑网络配置:

nano /etc/network/interfaces

示例配置:

source /etc/network/interfaces.d/*

auto lo
iface lo inet loopback

auto ens18
iface ens18 inet static
    address 10.168.1.3/24
    gateway 10.168.1.1
    dns-nameservers 223.5.5.5 8.8.8.8

重启网络:

systemctl restart networking

确认 IP 和网关:

ip addr show ens18
ip route

应能看到:

10.168.1.3/24
default via 10.168.1.1 dev ens18

三、配置 DNS

写入 DNS:

chattr -i /etc/resolv.conf 2>/dev/null || true

cat > /etc/resolv.conf <<'EOF'
nameserver 223.5.5.5
nameserver 8.8.8.8
nameserver 1.1.1.1
EOF

chattr +i /etc/resolv.conf 2>/dev/null || true

测试解析:

ping -c 3 223.5.5.5
getent hosts baidu.com
getent hosts google.com

如果能返回 IP,说明 DNS 正常。


四、配置 Debian 软件源

这里使用清华源,适用于 Debian 13 trixie。

cat > /etc/apt/sources.list <<'EOF'
deb http://mirrors.tuna.tsinghua.edu.cn/debian trixie main contrib non-free non-free-firmware
deb http://mirrors.tuna.tsinghua.edu.cn/debian trixie-updates main contrib non-free non-free-firmware
deb http://security.debian.org/debian-security trixie-security main contrib non-free non-free-firmware
EOF

apt update

安装基础工具:

apt install -y curl wget nano ca-certificates gzip iproute2 iptables nftables tcpdump

五、安装 Docker 和 Compose

安装 Docker:

apt install -y docker.io docker-compose

启动 Docker:

systemctl enable --now docker

确认版本:

docker version
docker compose version
docker-compose version

六、安装 Mihomo 二进制核心

创建目录:

mkdir -p /opt/mihomo/config

下载 Mihomo:

curl -L \
  https://github.com/MetaCubeX/mihomo/releases/download/v1.19.27/mihomo-linux-amd64-v1-v1.19.27.gz \
  -o /tmp/mihomo.gz

gzip -dc /tmp/mihomo.gz > /usr/local/bin/mihomo
chmod +x /usr/local/bin/mihomo

确认版本:

mihomo -v

七、准备自定义规则文件

创建自定义规则文件:

nano /opt/mihomo/custom.yaml

这个文件用于保存自己的 proxy-groupsrules

示例结构如下:

proxy-groups:
  - name: 默认节点
    type: select
    proxies:
      - 香港节点
      - 美国节点
      - DIRECT

  - name: YouTube
    type: select
    proxies:
      - 美国节点
      - 默认节点

  - name: Google
    type: select
    proxies:
      - 美国节点
      - 默认节点

  - name: 全部节点
    type: select
    include-all: true

  - name: 香港节点
    type: select
    include-all: true
    filter: "(?i)港|hk|hong"

  - name: 美国节点
    type: select
    include-all: true
    filter: "(?i)美|us|america|洛杉矶"

rules:
  - GEOIP,private,DIRECT

  - GEOSITE,youtube,YouTube
  - GEOSITE,google,Google
  - GEOSITE,netflix,Netflix
  - GEOSITE,github,默认节点
  - GEOSITE,openai,默认节点

  - GEOSITE,cn,DIRECT
  - GEOIP,CN,DIRECT

  - MATCH,默认节点

实际使用时,可以替换成自己的完整分流规则。


八、创建订阅更新脚本

创建脚本:

nano /opt/mihomo/update-sub.sh

写入以下内容。

注意:需要把 SUB_URL='你的Clash/Mihomo订阅链接' 替换成自己的真实订阅链接。

如果你的节点域名不是 1688.al,需要把 '+.1688.al' 改成自己的节点域名后缀,例如 '+.example.com'

#!/usr/bin/env bash
set -euo pipefail

SUB_URL='你的Clash/Mihomo订阅链接'
SECRET='mihomo_2026_KJ'

BASE='/opt/mihomo/config'
RAW="$BASE/config.raw.yaml"
TMP="$BASE/config.raw.yaml.tmp"
PROXIES="$BASE/proxies.yaml"
CONF="$BASE/config.yaml"
CUSTOM="/opt/mihomo/custom.yaml"

mkdir -p "$BASE"

if [ ! -f "$CUSTOM" ]; then
  echo "ERROR: $CUSTOM 不存在。请先把你的 proxy-groups 和 rules 规则放到 $CUSTOM"
  exit 1
fi

echo "尝试更新订阅..."
if curl -L --retry 2 --connect-timeout 10 \
  -A "clash.meta" \
  "$SUB_URL" \
  -o "$TMP"; then
  mv "$TMP" "$RAW"
  echo "订阅更新成功"
else
  echo "订阅下载失败,使用本地旧订阅:$RAW"
  rm -f "$TMP"
  if [ ! -s "$RAW" ]; then
    echo "ERROR: 本地旧订阅不存在,无法生成配置"
    exit 1
  fi
fi

awk '
BEGIN {keep=0}
/^proxies:/ {keep=1}
(/^proxy-groups:/ || /^rules:/) {keep=0}
keep==1 {print}
' "$RAW" > "$PROXIES"

cat > "$CONF" <<EOF_CONF
mixed-port: 7890
allow-lan: true
bind-address: '*'
mode: rule
log-level: info
external-controller: 0.0.0.0:9090
secret: "$SECRET"

ipv6: false

dns:
  enable: true
  listen: 0.0.0.0:1053
  ipv6: false
  enhanced-mode: redir-host
  use-hosts: false

  default-nameserver:
    - 223.5.5.5
    - 8.8.8.8

  nameserver:
    - 223.5.5.5
    - 8.8.8.8

  proxy-server-nameserver:
    - 223.5.5.5
    - 8.8.8.8

  direct-nameserver:
    - 223.5.5.5
    - 8.8.8.8

  nameserver-policy:
    '+.169.al':
      - 223.5.5.5
      - 8.8.8.8

tun:
  enable: true
  stack: mixed
  device: mihomo
  auto-route: true
  auto-redirect: true
  auto-detect-interface: true
  strict-route: true
  dns-hijack:
    - any:53
    - tcp://any:53

unified-delay: true
tcp-concurrent: true

profile:
  store-selected: true
  store-fake-ip: false

geodata-mode: true
geo-auto-update: true
geo-update-interval: 24
EOF_CONF

cat "$PROXIES" >> "$CONF"
echo "" >> "$CONF"
cat "$CUSTOM" >> "$CONF"

echo "mihomo config updated: $CONF"

赋予执行权限并生成配置:

chmod +x /opt/mihomo/update-sub.sh
/opt/mihomo/update-sub.sh

检查配置:

grep -nE "mixed-port|enhanced-mode|use-hosts|proxy-server-nameserver|direct-nameserver|nameserver-policy|tun:|dns-hijack|store-fake-ip|rules:|MATCH" /opt/mihomo/config/config.yaml

应能看到类似内容:

mixed-port: 7890
enhanced-mode: redir-host
use-hosts: false
proxy-server-nameserver:
direct-nameserver:
nameserver-policy:
tun:
dns-hijack:
store-fake-ip: false
rules:
MATCH,默认节点

这里使用 redir-host,不使用 fake-ip
这样对 Android、Pixel、电视盒子等透明网关设备兼容性更好。


九、创建 Mihomo systemd 服务

创建服务文件:

cat > /etc/systemd/system/mihomo.service <<'EOF'
[Unit]
Description=Mihomo Proxy Service
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/usr/local/bin/mihomo -d /opt/mihomo/config
Restart=always
RestartSec=5
LimitNOFILE=1048576

[Install]
WantedBy=multi-user.target
EOF

启动 Mihomo:

systemctl daemon-reload
systemctl enable --now mihomo

查看状态:

systemctl status mihomo --no-pager

确认端口:

ss -lntp | grep -E "7890|9090|1053"

应看到:

*:7890
*:9090
*:1053

十、开启 TUN 和 IPv4 转发

加载 TUN:

modprobe tun

cat > /etc/modules-load.d/tun.conf <<'EOF'
tun
EOF

ls -l /dev/net/tun

开启 IPv4 转发:

cat > /etc/sysctl.d/99-mihomo-gateway.conf <<'EOF'
net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=0
net.ipv6.conf.default.forwarding=0
EOF

sysctl --system

确认:

sysctl net.ipv4.ip_forward

应显示:

net.ipv4.ip_forward = 1

十一、配置 nftables NAT

创建 nftables 配置:

cat > /etc/nftables.conf <<'EOF'
#!/usr/sbin/nft -f

flush ruleset

table inet mihomo_gateway {
  chain forward {
    type filter hook forward priority filter; policy accept;
  }
}

table ip nat {
  chain postrouting {
    type nat hook postrouting priority srcnat; policy accept;
    ip saddr 10.168.1.0/24 oifname "ens18" masquerade
  }
}
EOF

启用 nftables:

systemctl enable --now nftables
nft -f /etc/nftables.conf

注意:手动执行 nft -f /etc/nftables.conf 后,会清空 Mihomo 自动生成的透明代理规则,所以执行完 nftables 后,需要重启 Mihomo:

systemctl restart mihomo
sleep 3

检查规则:

nft list ruleset | grep -iE "masquerade|table inet mihomo|redirect" -A 10 -B 5

正常应能看到:

masquerade
table inet mihomo
redirect to :xxxxx

其中 redirect to :xxxxx 是 Mihomo 自动生成的内部透明代理端口,每次重启可能变化,属于正常现象。


十二、部署 MetaCubeXD 面板

因为 MetaCubeXD 镜像在 GitHub Container Registry,有时直连拉取慢,可以先给 Docker 配置代理。

创建 Docker 代理配置:

mkdir -p /etc/systemd/system/docker.service.d

cat > /etc/systemd/system/docker.service.d/http-proxy.conf <<'EOF'
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7890"
Environment="HTTPS_PROXY=http://127.0.0.1:7890"
Environment="NO_PROXY=localhost,127.0.0.1,10.168.0.0/16,192.168.0.0/16"
EOF

systemctl daemon-reload
systemctl restart docker

创建 Docker Compose 文件:

cat > /opt/mihomo/docker-compose.yml <<'EOF'
services:
  metacubexd:
    image: ghcr.io/metacubex/metacubexd:latest
    container_name: metacubexd
    restart: unless-stopped
    ports:
      - "9091:80"
EOF

启动面板:

cd /opt/mihomo
docker compose up -d

查看容器:

docker ps

浏览器访问:

http://10.168.1.3:9091

面板连接 Mihomo API:

API 地址:http://10.168.1.3:9090
Secret:mihomo_2026_KJ

十三、测试 Debian 本机代理

执行:

curl -x http://127.0.0.1:7890 -4 https://api.ipify.org
echo

如果输出代理出口 IP,说明 Mihomo 节点正常。

例如:

157.254.53.92

查看最近日志:

journalctl -u mihomo --since "5 minutes ago" --no-pager

如果没有大量:

dns resolve failed
connect error

说明节点域名解析正常。


十四、局域网设备透明网关配置

以 Pixel 手机为例。

手机 Wi-Fi 设置为静态:

IP 地址:10.168.1.21
网关:10.168.1.3
网络前缀长度:24
DNS 1:223.5.5.5
DNS 2:8.8.8.8
代理:无
私人 DNS:关闭

保存后关闭 Wi-Fi,再重新打开。

Debian 上实时看日志:

journalctl -u mihomo -f | grep --line-buffered "10.168.1.21"

手机打开:

https://api.ipify.org
https://www.google.com
https://www.youtube.com

正常日志示例:

10.168.1.21 --> youtube.com:443 match GeoSite(youtube) using YouTube
10.168.1.21 --> googlevideo.com:443 match GeoSite(youtube) using YouTube
10.168.1.21 --> googleusercontent.com:443 match GeoSite(google) using Google
10.168.1.21 --> 59.82.x.x:443 match GeoIP(cn) using DIRECT

这说明:

  • YouTube 流量进入 YouTube 分流组
  • Google 流量进入 Google 分流组
  • 中国内地 IP 命中 GeoIP CN,走 DIRECT 直连
  • 其他未命中规则的流量走默认节点

十五、手动代理使用方式

除了透明网关,也可以在其他设备或 Docker 服务中手动指定代理:

HTTP 代理:10.168.1.3:7890
SOCKS 代理:10.168.1.3:7890

例如在 Linux 终端临时使用:

export http_proxy=http://10.168.1.3:7890
export https_proxy=http://10.168.1.3:7890

测试:

curl -4 https://api.ipify.org
echo

十六、动态域名节点注意事项

如果节点域名是动态 IP,不要把节点域名写死到 /etc/hosts

确认没有写死:

grep -A30 "# BEGIN MIHOMO NODES" /etc/hosts

没有输出就是正常。

确认 Mihomo 配置:

grep -nE "enhanced-mode|use-hosts|proxy-server-nameserver|nameserver-policy|store-fake-ip" /opt/mihomo/config/config.yaml

建议保持:

enhanced-mode: redir-host
use-hosts: false
proxy-server-nameserver:
nameserver-policy:
store-fake-ip: false

这样节点域名 IP 变化时,Mihomo 后续新连接会重新通过 DNS 解析。
已经建立的旧连接不会自动迁移到新 IP,断开后重新建立连接即可使用新解析结果。


十七、最终状态检查

检查 Mihomo:

systemctl status mihomo --no-pager

检查端口:

ss -lntp | grep -E "7890|9090|1053"

检查 TUN:

ip addr show mihomo

检查 nftables:

nft list ruleset | grep -iE "masquerade|table inet mihomo|redirect" -A 10 -B 5

正常状态应包括:

mihomo.service active running
*:7890
*:9090
*:1053
mihomo TUN 网卡存在
masquerade 存在
table inet mihomo 存在
redirect to :xxxxx 存在

十八、开机自启

确认服务开机自启:

systemctl enable mihomo
systemctl enable nftables
systemctl enable docker

确认 MetaCubeXD 容器自启:

docker inspect metacubexd | grep -i RestartPolicy -A 5

十九、日常维护命令

更新订阅并重启 Mihomo:

/opt/mihomo/update-sub.sh
systemctl restart mihomo

查看 Mihomo 日志:

journalctl -u mihomo -f

查看某个设备是否走代理:

journalctl -u mihomo -f | grep --line-buffered "10.168.1.21"

抓包排查设备流量:

tcpdump -ni ens18 host 10.168.1.21

重启 MetaCubeXD 面板:

cd /opt/mihomo
docker compose restart

查看面板容器:

docker ps

二十、总结

本方案最终实现了 Debian 13 作为局域网透明旁路由:

局域网设备
    ↓
网关:10.168.1.3
    ↓
Debian 13
    ↓
Mihomo TUN + auto-redirect
    ↓
按规则分流
    ├── 中国内地 IP:DIRECT 直连
    ├── YouTube:YouTube 分流组
    ├── Google:Google 分流组
    ├── ChatGPT:ChatGPT 分流组
    └── 其他流量:默认节点

同时保留手动代理能力:

HTTP / SOCKS:10.168.1.3:7890

MetaCubeXD 面板地址:

http://10.168.1.3:9091

Mihomo API 地址:

http://10.168.1.3:9090

Secret:

mihomo_2026_KJ

这套方案适合软路由、PVE 虚拟机、家用 Debian 旁路由、电视盒子、手机、平板等设备统一透明分流使用。

0

评论区