NanoPi R3S安装armbian设置mihomo旁路由

26次阅读
没有评论

共计 3510 个字符,预计需要花费 9 分钟才能阅读完成。

前言

R3S 是一台很适合折腾旁路由的小机器,双千兆网口、低功耗、体积小,用来跑 Armbian + mihomo 比较合适,本文记录从写入 Armbian、首次启动、固定 IP、安装 mihomo,到让局域网设备通过它作为旁路由上网的完整过程。
这里的 “旁路由” 不是替换主路由拨号,而是让主路由继续负责拨号、DHCP 和基础网络,NanoPi R3S 只负责代理网关能力。这样改动小,出问题时也容易回退。
实际上之前一直折腾 OpenWrt 没搞出来,可能我太傻逼了

一、准备工作

  1. NanoPi R3S * 1 废话
  2. TF 卡,建议 16GB 以上
  3. 读卡器
  4. USB-C 供电
  5. 网线
  6. Rufus https://rufus.ie
  7. Armbian 镜像 https://armbian.cn/boards/nanopi-r3s-lts
  8. SSH 工具

图中是 NanoPi R3S、本次用于写入系统的 U 盘/读卡器,以及准备安装 Armbian 的 TF 卡
NanoPi R3S安装armbian设置mihomo旁路由

二、系统安装与初始化

2.1 下载 Armbian 镜像

进入 Armbian 的 NanoPi R3S LTS 页面,选择适合自己的镜像
我这里建议优先选择:

  1. Debian 系:Trixie / Bookworm
  2. Ubuntu 系:Noble

如果只是做旁路由,我个人更推荐 Debian 系,包稳定,后续维护成本低。
NanoPi R3S安装armbian设置mihomo旁路由
注意区分 NanoPi R3S 和 NanoPi R3S LTS,两个页面不是同一个。R3S LTS 在 Armbian 页面里是单独列出的,不要下错镜像。

2.2 使用 Rufus 写入 TF 卡

NanoPi R3S安装armbian设置mihomo旁路由

2.3 首次启动 Armbian

写入完成后,把 TF 卡插入 NanoPi R3S,接上网线和电源,等待系统启动。
可以在主路由后台查看新设备 IP,也可以使用局域网扫描工具查找设备。
NanoPi R3S安装armbian设置mihomo旁路由
NanoPi R3S安装armbian设置mihomo旁路由

2.4 首次登录 Armbian 并完成初始化向导

NanoPi R3S 第一次启动 Armbian 后,可以通过主路由后台查看它获取到的局域网 IP。
NanoPi R3S安装armbian设置mihomo旁路由
NanoPi R3S安装armbian设置mihomo旁路由
使用 SSH 登录

ssh [email protected]

NanoPi R3S安装armbian设置mihomo旁路由
按照提示输入即可
NanoPi R3S安装armbian设置mihomo旁路由
NanoPi R3S安装armbian设置mihomo旁路由
NanoPi R3S安装armbian设置mihomo旁路由
NanoPi R3S安装armbian设置mihomo旁路由
等待脚本生成,至此安装完成!
NanoPi R3S安装armbian设置mihomo旁路由

2.5 安装基础软件包

export DEBIAN_FRONTEND=noninteractive
apt-get update && apt-get install -y bash-completion chrony curl dnsutils lrzsz net-tools tar unzip vim wget xz-utils ifupdown

2.6 固定 NanoPi R3S 的局域网 IP

旁路由建议固定 IP,否则每次重启后地址变化会很麻烦
先查看当前网卡和 IP
NanoPi R3S安装armbian设置mihomo旁路由
编辑网络配置文件

vim /etc/network/interfaces

写入如下配置

source /etc/network/interfaces.d/*

auto lo
iface lo inet loopback

allow-hotplug enp1s0
auto enp1s0
iface enp1s0 inet static
    address 192.168.1.10/24
    gateway 192.168.1.1

iface enp1s0 inet6 auto

这里需要注意:

  • enp1s0 要换成你自己机器实际联网的网卡名
  • 192.168.1.10/24 要换成你准备给 NanoPi R3S 使用的固定 IP
  • 192.168.1.1 要换成你的主路由地址
  • 不要把固定 IP 设置成已经被其他设备占用的地址

如果使用 /etc/network/interfaces 固定 IP,建议切换到传统 networking.service 管理网络,并关闭 systemd-networkd,避免两个网络管理器同时接管同一块网卡。

systemctl enable networking
systemctl disable systemd-networkd
systemctl disable systemd-networkd.socket
systemctl mask systemd-networkd
systemctl mask systemd-networkd.socket
systemctl mask systemd-networkd-persistent-storage.service
reboot

这里先启用 networking,是为了保证下次启动时 /etc/network/interfaces 能正常生效;再 disable 和 mask systemd-networkd,是为了避免它继续接管网卡、覆盖静态 IP 或修改路由。最后重启,让系统从开机阶段就使用新的网络管理方式。
使用静态 IP 连接 R3S
重启后验证命令

systemctl is-enabled networking
systemctl is-enabled systemd-networkd systemd-networkd.socket systemd-networkd-persistent-storage.service

理想结果如下,如果不是则那你需要询问 OpenAI
NanoPi R3S安装armbian设置mihomo旁路由

2.7 固定 DNS

后续 NanoPi R3S 会作为旁路由使用,并且 mihomo 可能会接管 53 端口提供 DNS 服务
为了避免 systemd-resolved 占用 53 端口,或者把 /etc/resolv.conf 改成 127.0.0.53,这里直接关闭 systemd-resolved,然后手动写入 DNS。

systemctl disable --now systemd-resolved

rm -f /etc/resolv.conf

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

2.8 开启转发并调整内核网络参数

旁路由需要让 Linux 内核允许转发流量,否则局域网设备把网关指向 NanoPi R3S 后,流量无法继续转发出去。
同时关闭 rp_filter,避免后续 TUN、透明代理、策略路由场景下,系统因为回程路径检查不一致而丢包。

vim /etc/sysctl.d/99-sysctl.conf
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr

net.ipv4.ip_forward = 1
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.enp1s0.rp_filter = 0
net.ipv6.conf.all.forwarding = 1

2.9 关闭 Armbian zram swap(可选)

Armbian 默认可能会启用 zram swap。zram 是用内存压缩出来的 swap,对低内存设备有一定保护作用。
如果你希望旁路由运行状态更简单,避免 zram 带来的压缩开销,可以关闭它。不过如果设备内存较小,或者 mihomo 规则集较大,不建议盲目关闭。

swapoff /dev/zram0
systemctl disable --now armbian-zram-config
systemctl mask armbian-zram-config

验证

swapon --show
free -h
systemctl is-enabled armbian-zram-config

三、安装 mihomo

系统基础配置完成后,就可以安装 mihomo 了。
mihomo 本体是一个单文件二进制程序,下载后放到 /usr/local/bin/ 即可。配置文件统一放在 /etc/mihomo/ 目录下,后续通过 systemd 管理启动和重启。

3.1 下载 mihomo 二进制文件

MIHOMO_VERSION="$(curl -Ls https://v6.gh-proxy.org/https://api.github.com/repos/MetaCubeX/mihomo/releases |
  sed -n 's/.*"tag_name": *"v\([^"]*\)".*/\1/p' | sort -rV | head -n 1)"
curl -L "https://v6.gh-proxy.org/https://github.com/MetaCubeX/mihomo/releases/download/v$MIHOMO_VERSION/mihomo-linux-arm64-v$MIHOMO_VERSION.gz" | 
  gzip -d > /usr/local/bin/mihomo
chmod +x /usr/local/bin/mihomo
mihomo -v

3.2 写入 mihomo 自定义配置文件

放入如下路径

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

本次给一个配置示例,当然可以参阅文档进行自定义 https://wiki.metacubex.one

proxy-providers:
  CNIX:
    type: http
    url: "这里需要替换为你的订阅链接,请保留双引号和双引号前面的空格"
    path: ./cnix.yaml
    interval: 7200
mixed-port: 7890
allow-lan: false
mode: rule
log-level: debug
ipv6: false
external-controller: 0.0.0.0:9090
dns:
  enable: true
  listen: 0.0.0.0:53
  ipv6: false
  default-nameserver:
    - 223.5.5.5
    - 114.114.114.114
  nameserver:
    - 223.5.5.5
    - 114.114.114.114
    - 119.29.29.29
    - 180.76.76.76
  enhanced-mode: fake-ip
  fake-ip-range: 198.18.0.1/16
  fake-ip-filter:
    - "*.lan"
    - "*.localdomain"
    - "*.example"
    - "*.invalid"
    - "*.localhost"
    - "*.test"
    - "*.local"
    - "*.home.arpa"
    - router.asus.com
    - localhost.sec.qq.com
    - localhost.ptlogin2.qq.com
    - "+.msftconnecttest.com"
tun:
  enable: true
  stack: system
  auto-route: true
  auto-detect-interface: true
  dns-hijack:
    - 114.114.114.114:53
    - 180.76.76.76:53
    - 119.29.29.29:53
    - 223.5.5.5:53
    - 8.8.8.8:53
    - 8.8.4.4:53
    - 1.1.1.1:53
    - 1.0.0.1:53

proxy-groups:
  - name: PROXY
    type: select
    proxies:
      - CNIX
      - DIRECT
  - name: CNIX
    type: select
    use:
      - CNIX

rule-providers:
  reject:
    type: http
    behavior: domain
    url: "https://testingcf.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/reject.txt"
    path: ./ruleset/reject.yaml
    interval: 36000

  icloud:
    type: http
    behavior: domain
    url: "https://testingcf.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/icloud.txt"
    path: ./ruleset/icloud.yaml
    interval: 36000

  apple:
    type: http
    behavior: domain
    url: "https://testingcf.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/apple.txt"
    path: ./ruleset/apple.yaml
    interval: 36000

  google:
    type: http
    behavior: domain
    url: "https://testingcf.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/google.txt"
    path: ./ruleset/google.yaml
    interval: 36000

  proxy:
    type: http
    behavior: domain
    url: "https://testingcf.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/proxy.txt"
    path: ./ruleset/proxy.yaml
    interval: 36000

  direct:
    type: http
    behavior: domain
    url: "https://testingcf.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/direct.txt"
    path: ./ruleset/direct.yaml
    interval: 36000

  private:
    type: http
    behavior: domain
    url: "https://testingcf.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/private.txt"
    path: ./ruleset/private.yaml
    interval: 36000

  telegramcidr:
    type: http
    behavior: ipcidr
    url: "https://testingcf.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/telegramcidr.txt"
    path: ./ruleset/telegramcidr.yaml
    interval: 36000

  cncidr:
    type: http
    behavior: ipcidr
    url: "https://testingcf.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/cncidr.txt"
    path: ./ruleset/cncidr.yaml
    interval: 36000

  lancidr:
    type: http
    behavior: ipcidr
    url: "https://testingcf.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/lancidr.txt"
    path: ./ruleset/lancidr.yaml
    interval: 36000

  applications:
    type: http
    behavior: classical
    url: "https://testingcf.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/applications.txt"
    path: ./ruleset/applications.yaml
    interval: 36000

rules:
  - RULE-SET,applications,DIRECT
  - DOMAIN,clash.razord.top,DIRECT
  - DOMAIN,yacd.haishan.me,DIRECT
  - RULE-SET,private,DIRECT
  - RULE-SET,reject,REJECT
  - DOMAIN,www.icloud.com,PROXY
  - DOMAIN-SUFFIX,icloud-content.com,PROXY
  - RULE-SET,icloud,DIRECT
  - RULE-SET,apple,PROXY
  - RULE-SET,google,PROXY
  - RULE-SET,proxy,PROXY
  - RULE-SET,direct,DIRECT
  - RULE-SET,lancidr,DIRECT
  - RULE-SET,cncidr,DIRECT
  - RULE-SET,telegramcidr,PROXY
  - MATCH,PROXY

3.3 创建 mihomo systemd 服务

vim /etc/systemd/system/mihomo.service
[Unit]
Description=mihomo Daemon, Another Clash Kernel.
Documentation=https://wiki.metacubex.one
After=network.target nss-lookup.target network-online.target
Wants=network-online.target

[Service]
Type=simple
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH CAP_DAC_OVERRIDE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH CAP_DAC_OVERRIDE
ExecStart=/usr/local/bin/mihomo -d /etc/mihomo
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=10
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

3.4 启动 mihomo

启动前确认配置文件已经放入 /etc/mihomo/config.yaml
启动服务

systemctl daemon-reload
systemctl enable --now mihomo.service

查看状态

systemctl status mihomo.service --no-pager

如果看到 active (running),说明启动成功
NanoPi R3S安装armbian设置mihomo旁路由

四、接入旁路由

在需要使用旁路由的设备上,把网络参数改成

网关: 192.168.1.10
DNS: 192.168.1.10

至此 NanoPi R3S 安装 Armbian 并配置 mihomo 旁路由完成
NanoPi R3S安装armbian设置mihomo旁路由
后续修改配置文件 /etc/mihomo/config.yaml
修改后重启

systemctl restart mihomo.service
正文完
 0
honeok
版权声明:本站原创文章,由 honeok 于2026-05-27发表,共计3510字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码