人们很容易对 Linux 命令习以为常。它们在您安装 Linux 时与系统捆绑在一起,我们常常不会质疑它们为什么在那里。一些基本命令,例如 cd
、kill
和 echo
并不总是独立的应用程序,而实际上是内置于您的 shell 中的。其他命令,例如 ls
、mv
和 cat
是核心实用程序包(通常是 GNU coreutils
)的一部分。但是,在开源世界中总是存在替代方案,其中最有趣的一个是 BusyBox。
Linux 中的 BusyBox 是什么?
BusyBox 是一个开源 (GPL) 项目,提供近 400 个常用命令的简单实现,包括 ls
、mv
、ln
、mkdir
、more
、ps
、gzip
、bzip2
、tar
和 grep
。它还包含编程语言 awk
、流编辑器 sed
、文件系统检查器 fsck
、rpm
和 dpkg
包管理器,当然还有 shell (sh
),它提供对所有这些命令的轻松访问。简而言之,它包含 POSIX 系统执行常见系统维护任务以及许多用户和管理任务所需的所有基本命令。
事实上,它甚至包含一个 init
命令,可以作为 PID 1 启动,作为所有其他系统服务的父进程。换句话说,BusyBox 可以用作 systemd、OpenRC、sinit、init 和其他启动守护程序的替代方案。
BusyBox 非常小。作为一个可执行文件,它不到 1 MB,因此它在 嵌入式、边缘 和 IoT 领域获得了广泛的欢迎,在这些领域,驱动器空间非常宝贵。在容器和云计算领域,它也常被用作最小 Linux 容器镜像的基础。
极简主义
BusyBox 的吸引力部分在于其极简主义。它的所有命令都编译成一个单独的二进制文件 (busybox
),并且它的手册页只有 81 页(通过我将 man
管道传输到 pr
的计算),但涵盖了近 400 个命令。
作为一个示例比较,这是 shadow
版本的 useradd --help
的输出
-b, --base-dir BASE_DIR base directory for home
-c, --comment COMMENT GECOS field of the new account
-d, --home-dir HOME_DIR home directory of the new account
-D, --defaults print or change the default config
-e, --expiredate EXPIRE_DATE expiration date of the new account
-f, --inactive INACTIVE password inactivity
-g, --gid GROUP name or ID of the primary group
-G, --groups GROUPS list of supplementary groups
-h, --help display this help message and exit
-k, --skel SKEL_DIR alternative skeleton dir
-K, --key KEY=VALUE override /etc/login.defs
-l, --no-log-init do not add the user to the lastlog
-m, --create-home create the user's home directory
-M, --no-create-home do not create the user's home directory
-N, --no-user-group do not create a group with the user's name
-o, --non-unique allow users with non-unique UIDs
-p, --password PASSWORD encrypted password of the new account
-r, --system create a system account
-R, --root CHROOT_DIR directory to chroot into
-s, --shell SHELL login shell of the new account
-u, --uid UID user ID of the new account
-U, --user-group create a group with the same name as a user
这是 BusyBox 版本的相同命令
-h DIR Home directory
-g GECOS GECOS field
-s SHELL Login shell
-G GRP Group
-S Create a system user
-D Don't assign a password
-H Don't create home directory
-u UID User id
-k SKEL Skeleton directory (/etc/skel)
这种差异是特性还是限制,取决于您更喜欢命令中有 20 个选项还是 10 个选项。对于某些用户和用例,BusyBox 的极简主义为需要完成的任务提供了足够的资源。对于其他人来说,这是一个很好的最小环境,可以作为后备或作为安装更强大的工具(如 Bash、Zsh、GNU Awk 等)的基础。
安装 BusyBox
在 Linux 上,您可以使用包管理器安装 BusyBox。例如,在 Fedora 和类似系统上
$ sudo dnf install busybox
在 Debian 和衍生系统上
$ sudo apt install busybox
在 macOS 上,使用 MacPorts 或 Homebrew。在 Windows 上,使用 Chocolatey。
您可以使用 chsh --shell
命令将 BusyBox 设置为您的 shell,后跟 BusyBox sh
应用程序的路径。我将 BusyBox 放在 /lib64
中,但其位置取决于您的发行版将其安装在哪里。
$ which busybox
/lib64/busybox/busybox
$ chsh --shell /lib64/busybox/sh
将所有常用命令批发替换为 BusyBox 稍微复杂一些,因为大多数发行版都“硬连接”为从特定软件包中查找特定命令。换句话说,虽然从技术上讲,可以用 BusyBox 的 init
替换 init
,但您的包管理器可能会拒绝允许您删除包含 init
的包,因为它担心您导致系统无法启动。有一些发行版是基于 BusyBox 构建的,因此从头开始可能是体验围绕 BusyBox 构建的系统的最简单方法。
尝试 BusyBox
您不必永久性地将您的 shell 更改为 BusyBox 才能尝试它。您可以从当前的 shell 启动 BusyBox shell
$ busybox sh
~ $
但是,您的系统仍然安装了非 BusyBox 版本的命令,因此要体验 BusyBox 的工具,您必须将命令作为 busybox
可执行文件的参数发出
~ $ busybox echo $0
sh
~ $ busybox ls --help
BusyBox vX.YY.Z (2021-08-25 07:31:48 NZST) multi-call binary.
Usage: ls [-1AaCxdLHRFplinshrSXvctu] [-w WIDTH] [FILE]...
List directory contents
-1 One column output
-a Include entries that start with .
-A Like -a, but exclude . and ..
-x List by lines
[...]
为了获得“完整”的 BusyBox 体验,您可以为每个命令创建指向 busybox
的符号链接。只要您使用 for 循环,这比听起来更容易
$ mkdir bbx
$ for i in $(bbx --list); do \
ln -s /path/to/busybox bbx/$i \
done
在 path 的*开头*添加您的符号链接目录,并启动 BusyBox
$ PATH=$(pwd)/bbx:$PATH bbx/sh
忙起来
BusyBox 是一个有趣的项目,也是计算如何极简的一个例子。无论您是将 BusyBox 用作 古老的计算机 你拯救了的轻量级环境,还是作为 嵌入式设备的用户空间,还是为了试用新的 init 系统,或者只是出于好奇,重新熟悉旧的熟悉的,但不知何故新的命令会很有趣。
评论已关闭。