BusyBox 是一个开源(GPL)项目,提供了近 400 个常用命令的简单实现。
它的所有命令都被编译到一个二进制文件里(busybox
),它的手册只有 81 页(根据我对 man
送到 pr
管道的计算),但它涵盖了近 400 条命令。作为一个例子的比较,这是 “原版” 的 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。你可以将 BusyBox 设置为你的 shell,使用 chsh —shell
命令,然后再加上 BusyBox sh
应用程序的路径。我把 BusyBox 放在 /lib64
中,但它的位置取决于你的发行版的安装位置。
$ which busybox/lib64/busybox/busybox$ chsh --shell /lib64/busybox/sh
用 BusyBox 全盘替换所有常见的命令要复杂一些,因为大多数发行版都是“硬接线”,会在特定的软件包寻找特定的命令。换句话说,虽然技术上可以用 BusyBox 的 init
替换系统的 init
,但你的软件包管理器可能会拒绝让你删除包含 init
的软件包,以免你担心删除会导致系统无法启动。有一些发行版是建立在 BusyBox 之上的,所以从新环境开始可能是体验 BusyBox 系统的最简单方法。试试 BusyBox你不必为了尝试 BusyBox 而将你的 shell 永久改为 BusyBox。你可以从你当前的 shell 中启动一个 BusyBox shell。
$ busybox sh~ $
不过你的系统仍然有安装的非 BusyBox 版本的命令,所以要体验 BusyBox 的工具,你必须把命令作为参数发给 busybox
可执行文件:
~ $ busybox echo $0sh~ $ busybox ls --helpBusyBox 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
在你的 路径 的 开头 添加这个符号链接目录,并启动 BusyBox:
$ PATH=$(pwd)/bbx:$PATH bbx/sh
用起来BusyBox 是一个有趣的项目,也是一个可以实现 极简 计算的例子。无论你是把 BusyBox 作为 你唤醒的 古老的计算机 的轻量级环境,还是作为 嵌入式设备 的用户界面,抑或试用一个新的初始化系统,就算是为了好奇,让自己重新认识那些熟悉而又陌生的命令,都会很有意思。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。