邳州信息网

标题: 如何在 Linux 上使用 BusyBox [打印本页]

作者: 3AAA    时间: 2021-10-3 10:06
标题: 如何在 Linux 上使用 BusyBox

BusyBox 是一个开源(GPL)项目,提供了近 400 个常用命令的简单实现。



我们很容易认为 Linux 的命令是理所当然的。当你安装 Linux 时,它们与系统捆绑在一起,而我们常常不问为什么它们会在那里。一些基本的命令,如 cd、kill和 echo,并不总是独立的应用程序,而是实际上内置于你的 shell 中。其他如ls、mv和cat是核心工具包(通常是 GNUcoreutils)的一部分。但在开源的世界里,总是有一些替代品,其中最有趣的是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 非常小。作为一个可执行文件,它不到 1MB,所以它在 嵌入式、边缘计算和物联网领域很受欢迎,因为这些场景的存储空间是很宝贵的。在容器和云计算的世界里,它作为精简的 Linux 容器镜像的基础镜像也很受欢迎。

极简主义


BusyBox 的部分魅力在于它的极简主义。它的所有命令都被编译到一个二进制文件里(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、GNUAwk等等。

安装 BusyBox


在 Linux 上,你可以使用你的软件包管理器安装 BusyBox。例如,在 Fedora 及类似发行版:
$ sudo dnf install busybox
在 Debian 及其衍生版:
$ sudo apt install busybox
在 MacOS 上,可以使用 MacPorts或Homebrew。在 Windows 上,可以使用Chocolatey。

你可以将 BusyBox 设置为你的 shell,使用 chsh —shell命令,然后再加上 BusyBoxsh应用程序的路径。我把 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)/bbxPATH bbx/sh

用起来


BusyBox 是一个有趣的项目,也是一个可以实现 极简计算的例子。无论你是把 BusyBox 作为你唤醒的古老的计算机的轻量级环境,还是作为嵌入式设备的用户界面,抑或试用一个新的初始化系统,就算是为了好奇,让自己重新认识那些熟悉而又陌生的命令,都会很有意思。

via: https://opensource.com/article/21/8/what-busybox

作者:Seth Kenlon选题:lujun9972译者:wxy校对:wxy

本文由 LCTT原创编译,Linux中国荣誉推出
作者: 山水边    时间: 2021-10-3 10:07
Linux下用不着这玩意,安卓用它主要是为了实现在系统里有Linux的常用命令罢了,且Linux的软件包会不停更新
作者: sansheng    时间: 2021-10-3 10:08
都有linux了干嘛还用一层虚拟化
作者: 朱姣姣    时间: 2021-10-3 10:09
只有100m的flash空间,10m的ram,能装linux系统吗?
作者: 夜渐凉    时间: 2021-10-3 10:11
当你把正常系统搞坏的时候,你就知道busybox有什么用了
作者: 烟笼寒水    时间: 2021-10-3 10:12
厉害
作者: 金店老板    时间: 2021-10-3 10:14
盲盒
作者: 鸭梨山大    时间: 2021-10-3 10:15
以前刷机搞机总是要装个busybox,不懂干嘛的,现在豁然开朗
作者: 雷雨    时间: 2021-10-3 10:17
盲盒?!
作者: 大工地施工    时间: 2021-10-3 10:19
转发了
作者: 国美    时间: 2021-10-3 10:20
转发了
作者: 晴转多云    时间: 2021-10-3 10:20
转发了
作者: woshissd    时间: 2021-10-3 10:22
转发了
作者: 起啥用户名    时间: 2021-10-3 10:23
转发了
作者: 中医世家    时间: 2021-10-3 10:24
转发了
作者: 十点半    时间: 2021-10-3 10:26
转发了
作者: 说东道西    时间: 2021-10-3 10:28
转发了
作者: DJ666    时间: 2021-10-3 10:30
转发了
作者: 朱姣姣    时间: 2021-10-3 10:32
转发了
作者: pizhou111    时间: 2021-10-3 10:34
转发了




欢迎光临 邳州信息网 (https://www.pzxxw.com/) Powered by Discuz! X3.4