树莓派5旁路由部署
1. 前提条件
Section titled “1. 前提条件”- 本文以 树莓派 5 运行官方系统为例。
- 宿主机已成功部署并运行 Docker 或 Podman 容器引擎。
2. 镜像下载与导入
Section titled “2. 镜像下载与导入”cd /tmp# 下载官方 AArch64/ARMv8 架构的 rootfs 镜像wget https://archive.openwrt.org/releases/24.10.4/targets/armsr/armv8/openwrt-24.10.3-armsr-armv8-generic-ext4-rootfs.img.gz# 解压镜像文件gzip -d openwrt-24.10.3-armsr-armv8-generic-ext4-rootfs.img.gz# 将镜像导入为 Docker 镜像 (此处若使用的是官方 rootfs 归档包,可直接 docker import)docker import openwrt-24.10.3-armsr-armv8-generic-ext4-rootfs.img openwrt3. 网络准备与容器运行
Section titled “3. 网络准备与容器运行”- 开启物理网卡混杂模式并创建 Macvlan 网络
# 临时开启 eth0 网卡的混杂模式ip link set eth0 promisc on
# 创建 Docker Macvlan 外部网络(请根据实际局域网网段修改 subnet 与 gateway)docker network create -d macvlan --subnet=192.168.8.0/24 --gateway=192.168.8.1 -o parent=eth0 openwrt- 创建 docker-compose.yml 配置文件
services: openwrt: image: openwrt container_name: openwrt-route restart: always privileged: true command: /sbin/init networks: - openwrt
networks: openwrt: external: true # 声明使用上面手动创建的 macvlan 网络- 启动并进入 OpenWrt 容器
docker compose up -ddocker exec -it openwrt-route /bin/sh4. OpenWrt 初始化设置
Section titled “4. OpenWrt 初始化设置”在容器内执行以下初始化操作:
- 修改 Root 密码
passwd- 配置静态 IP 路由信息
编辑 /etc/config/network 配置文件,根据实际局域网规划静态 IP(例如配置为 192.168.8.202):
config interface 'loopback' option device 'lo' option proto 'static' option ipaddr '127.0.0.1' option netmask '255.0.0.0'
config globals 'globals' option ula_prefix 'fd61:570b:366f::/48'
config device option name 'br-lan' option type 'bridge' list ports 'eth0'
config interface 'lan' option device 'br-lan' option proto 'static' option ipaddr '192.168.8.202' option netmask '255.255.255.0' option gateway '192.168.8.1' option dns '223.5.5.5' option ip6assign '60'- 重启网络服务
service network restart- 调整基础服务组件
# 更新并切换到完整版 dnsmasq 以支持进阶过滤规则opkg updateopkg remove dnsmasqopkg install dnsmasq-fullservice dnsmasq status
# 停止容器内默认的 IPv6 DHCP (odhcpd) 服务,防止与主路由冲突/etc/init.d/odhcpd stop/etc/init.d/odhcpd disable
# 修正时区设置uci set system.@system[0].zonename='Asia/Shanghai'uci set system.@system[0].timezone='CST-8'uci commit system/etc/init.d/system restart5. 常用软件与插件安装
Section titled “5. 常用软件与插件安装”5.1. LuCI Web 中文面板
Section titled “5.1. LuCI Web 中文面板”cd /tmpopkg install luci-compatopkg install luci-lib-ipkgopkg install luci-i18n-base-zh-cnopkg install curl wget-ssl unzip
# 安装优秀的 Argon 质感主题与配置工具wget --no-check-certificate https://github.com/jerrykuku/luci-theme-argon/releases/download/v2.3.2/luci-theme-argon_2.3.2-r20250207_all.ipkopkg install luci-theme-argon*.ipk
wget --no-check-certificate -O luci-app-argon-config_0.9_all.ipk https://github.com/jerrykuku/luci-app-argon-config/releases/download/v0.9/luci-app-argon-config_0.9_all.ipkopkg install luci-app-argon-config*.ipk5.2. iStore 软件中心
Section titled “5.2. iStore 软件中心”wget https://github.com/linkease/openwrt-app-actions/raw/main/applications/luci-app-systools/root/usr/share/systools/istore-reinstall.runchmod 755 istore-reinstall.run./istore-reinstall.run5.3. PassWall2 网络代理插件
Section titled “5.3. PassWall2 网络代理插件”# 安装系统依赖opkg install kmod-nft-socketopkg install kmod-nft-tproxy
# 导入 GPG 公钥并添加软件源wget -O passwall.pub https://master.dl.sourceforge.net/project/openwrt-passwall-build/passwall.pubopkg-key add passwall.pub
read release arch << EOF$(. /etc/openwrt_release ; echo ${DISTRIB_RELEASE%.*} $DISTRIB_ARCH)EOF
for feed in passwall_luci passwall_packages passwall2; do echo "src/gz $feed https://master.dl.sourceforge.net/project/openwrt-passwall-build/releases/packages-$release/$arch/$feed" >> /etc/opkg/customfeeds.confdone
opkg updateopkg install luci-app-passwall2opkg install luci-i18n-passwall2-zh-cnservice passwall2 enable6. 宿主机与容器互通优化
Section titled “6. 宿主机与容器互通优化”在 Docker Macvlan 模式下,出于安全隔离,宿主机与容器默认是无法直接通信的。若想让宿主机自身也能使用旁路由,需通过添加虚拟网桥进行通信中转。
- 配置宿主机
/etc/rc.local脚本
#!/bin/bash# 1. 开启物理网卡混杂模式ip link set eth0 promisc on
# 2. 创建宿主机专属的 macvlan 虚拟网卡并绑定到物理网卡ip link add macvlan-bridge link eth0 type macvlan mode bridgeip addr add 192.168.8.203/24 dev macvlan-bridgeip link set macvlan-bridge up
# 3. 指定宿主机访问容器 IP (192.168.8.202) 的路由规则走虚拟网卡ip route add 192.168.8.202 dev macvlan-bridge
# 4. 优先级路由:将系统默认网关指向旁路由,设置较低 Metric (10)# 这样即便 192.168.8.1 的默认网关存在,系统也会优先通过旁路由出网ip route add default via 192.168.8.202 dev macvlan-bridge metric 10
exit 0- 应用并激活 rc.local 服务
chmod +x /etc/rc.localsystemctl restart rc-local- 适配 DNS 服务走旁路由代理
修改宿主机 /etc/systemd/resolved.conf:
[Resolve]DNS=192.168.8.202FallbackDNS=192.168.8.1 114.114.114.114重启系统解析服务并清理缓存:
systemctl daemon-reloadsystemctl restart systemd-resolvedresolvectl flush-cachesresolvectl status