BusyBox 中文文档 下载 App

OpenWrt 路由器固件中的 BusyBox 深度集成

OpenWrt 为什么选择 BusyBox

OpenWrt 是最流行的开源路由器固件(派生自 Linksys 的 GPL 源码,被 DD-WRT、LEDE 等继承和继续开发)。它的选择逻辑和 BusyBox 本身的设计哲学高度契合:

OpenWrt 在 BusyBox 基础上构建了自己的整套系统——配置系统(UCI)、包管理器(opkg)、Web 管理界面(LuCI),全部围绕 BusyBox 的运行时环境设计。

OpenWrt 对 BusyBox 的扩展

OpenWrt 直接修改 BusyBox 源码,增加了几个路由器特有的 Applet 和功能:

扩展说明
UCI 支持统一配置接口,通过 uci set/get/commit 操作系统配置
fw4 / nftables 包装OpenWrt 的防火墙层
uci 配置系统(luci 前端)与 BusyBox ash shell 深度集成
热插拔事件BusyBox init + /etc/hotplug.d/
opkg 包管理器运行时安装软件(BusyBox 里没这个)
dnsmasq、odhcpd网络服务

在 OpenWrt 中自定义 BusyBox

OpenWrt 的构建系统有专门的 BusyBox 配置入口:

# 获取 OpenWrt 源码
git clone https://git.openwrt.org/openwrt/openwrt.git
cd openwrt
./scripts/feeds update -a
./scripts/feeds install -a

# 选择平台(比如 x86 路由器或 ARM 路由器)
make menuconfig
# Target System -> 选 x86 或 ARM
# Target Profile -> 选具体设备

# 进入 BusyBox 配置
# 在 OpenWrt menuconfig 里:
# Packages -> Utilities -> BusyBox
# BusyBox configuration -> Customize busybox options -> Y
# 然后:
make package/system/busybox/compile V=s
# 这会打开 BusyBox 的 menuconfig

# 修改后重新构建
make -j$(nproc)

OpenWrt 的 BusyBox 默认配置(OpenWrt defconfig)已经针对路由器优化过:开启了所有网络工具、init、日志、压缩工具,关闭了编译/开发相关的 Applet。

路由器上的 BusyBox 实战

登录一台 OpenWrt 路由器后,你面对的就是 BusyBox ash 环境:

# 查看 BusyBox 版本
busybox
# BusyBox v1.36.1 (2025-01-15 10:30:00) multi-call binary.

# 查看所有可用命令(OpenWrt 扩展过的)
busybox --list | wc -l
# 通常 300+ 个,包括 UCI 命令

# 查看网络接口
ifconfig br-lan
# br-lan      Link encap:Ethernet  HWaddr ...

# 查看路由表
route -n

# 查看已安装包
opkg list-installed

# 修改系统配置(UCI 方式,OpenWrt 特色)
uci set system.@system[0].hostname='my-router'
uci commit system

# 查看 init 配置
cat /etc/inittab
# 会看到 respawn 守护的 dnsmasq、odhcpd、sshd 等

# 重启网络
/etc/init.d/network restart
# OpenWrt 用自己的 init.d 脚本,但底层运行在 BusyBox shell 上

典型问题排查

空间不足:路由器的 /usr 分区通常只有 1-2MB。装个额外的包可能就空间不够了。可以用 df -h 检查,用 opkg remove <pkg> 卸载不需要的。

Applet 不存在:某些 GNU 工具在 OpenWrt BusyBox 里没编译进去。先 busybox --list | grep <name> 确认。如果确实没有,可能需要额外装 opkg 包或自定义编译。

Shell 脚本不兼容:OpenWrt 默认 shell 是 ash。如果脚本里用了 bash 语法(如 [[ ]]、数组),会报错。解决方案:改脚本用 POSIX sh 语法,或者装 bash opkg 包(代价是约 1MB 空间)。