如何在 Linux 上使用 BusyBox
BusyBox

> 
> BusyBox 是一个开源（GPL）项目，提供了近 400 个常用命令的简单实现。
> 
> 
> 


![](/data/attachment/album/202110/01/185208x6ckkmvi0silk3vk.jpg "bash logo on green background")


我们很容易认为 Linux 的命令是理所当然的。当你安装 Linux 时，它们与系统捆绑在一起，而我们常常不问为什么它们会在那里。一些基本的命令，如 [cd](https://opensource.com/article/21/8/navigate-linux-directories)、[kill](https://opensource.com/article/18/5/how-kill-process-stop-program-linux) 和 echo，并不总是独立的应用程序，而是实际上内置于你的 shell 中。其他如 [ls](https://opensource.com/article/19/7/master-ls-command)、[mv](https://opensource.com/article/19/8/moving-files-linux-depth) 和 [cat](https://opensource.com/article/19/2/getting-started-cat-command) 是核心工具包（通常是 GNU `coreutils`）的一部分。但在开源的世界里，总是有一些替代品，其中最有趣的是 [BusyBox](https://www.busybox.net)。


### 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](https://opensource.com/article/20/4/systemd)、OpenRC、sinit、init 和其他初始化系统的替代品。


BusyBox 非常小。作为一个可执行文件，它不到 1MB，所以它在 [嵌入式](https://opensource.com/article/21/3/rtos-embedded-development)、[边缘计算](https://opensource.com/article/17/9/what-edge-computing) 和 [物联网](https://opensource.com/article/21/3/iot-measure-raspberry-pi) 领域很受欢迎，因为这些场景的存储空间是很宝贵的。在容器和云计算的世界里，它作为精简的 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](https://opensource.com/article/20/4/bash-sysadmins-ebook)、[Zsh](https://opensource.com/article/19/9/getting-started-zsh)、GNU [Awk](https://opensource.com/article/20/9/awk-ebook) 等等。


### 安装 BusyBox


在 Linux 上，你可以使用你的软件包管理器安装 BusyBox。例如，在 Fedora 及类似发行版：



```
$ sudo dnf install busybox

```

在 Debian 及其衍生版：



```
$ sudo apt install busybox

```

在 MacOS 上，可以使用 [MacPorts](https://opensource.com/article/20/11/macports) 或 [Homebrew](https://opensource.com/article/20/6/homebrew-mac)。在 Windows 上，可以使用 [Chocolatey](https://opensource.com/article/20/3/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 $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 循环](https://opensource.com/article/19/10/programming-bash-loops) 就行：



```
$ mkdir bbx
$ for i in $(bbx --list); do \
    ln -s /path/to/busybox bbx/$i \
done

```

在你的 [路径](https://opensource.com/article/17/6/set-path-linux) 的 *开头* 添加这个符号链接目录，并启动 BusyBox：



```
$ PATH=$(pwd)/bbx:$PATH bbx/sh

```

### 用起来


BusyBox 是一个有趣的项目，也是一个可以实现 *极简* 计算的例子。无论你是把 BusyBox 作为 [你唤醒的](https://opensource.com/article/19/7/how-make-old-computer-useful-again) [古老的计算机](https://opensource.com/article/20/2/restore-old-computer-linux) 的轻量级环境，还是作为 [嵌入式设备](https://opensource.com/article/20/6/open-source-rtos) 的用户界面，抑或试用一个新的初始化系统，就算是为了好奇，让自己重新认识那些熟悉而又陌生的命令，都会很有意思。




---


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


作者：[Seth Kenlon](https://opensource.com/users/seth) 选题：[lujun9972](https://github.com/lujun9972) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
It's easy to take Linux commands for granted. They come bundled with the system when you install Linux, and we often don't question why they're there. Some of the basic commands, such as [ cd](https://opensource.com/article/21/8/navigate-linux-directories),

[, and](https://opensource.com/article/18/5/how-kill-process-stop-program-linux)

`kill`

`echo`

aren't always independent applications but are actually built into your shell. Others, such as [,](https://opensource.com/article/19/7/master-ls-command)

`ls`

[, and](https://opensource.com/article/19/8/moving-files-linux-depth)

`mv`

[are part of a core utility package (often GNU](https://opensource.com/article/19/2/getting-started-cat-command)

`cat`

`coreutils`

specifically). But there are always alternatives in the world of open source, and one of the most interesting is [BusyBox](https://www.busybox.net).

## What is BusyBox in Linux?


BusyBox is an open source (GPL) project providing simple implementations of nearly 400 common commands, including `ls`

, `mv`

, `ln`

, `mkdir`

, `more`

, `ps`

, `gzip`

, `bzip2`

, `tar`

, and `grep`

. It also contains a version of the programming language `awk`

, the stream editor `sed`

, the filesystem checker `fsck`

, the `rpm`

and `dpkg`

package managers, and of course, a shell (`sh`

) that provides easy access to all of these commands. In short, it contains all the essential commands required for a POSIX system to perform common system maintenance tasks as well as many user and administrative tasks.

In fact, it even contains an `init`

command which can be launched as PID 1 to serve as the parent process for all other system services. In other words, BusyBox can be used as an alternative to [systemd](https://opensource.com/article/20/4/systemd), OpenRC, sinit, init, and other launch daemons.

BusyBox is very small. As an executable, it's under 1 MB, so it has gained much of its popularity in the [embedded](https://opensource.com/article/21/3/rtos-embedded-development), [Edge](https://opensource.com/article/17/9/what-edge-computing), and [IoT](https://opensource.com/article/21/3/iot-measure-raspberry-pi) space, where drive space is at a premium. In the world of containers and cloud computing, it's also popular as a foundation for minimal Linux container images.

## Minimalism

Part of the appeal of BusyBox is its minimalism. All of its commands are compiled into a single binary (`busybox`

), and its man page is a mere 81 pages (by my calculation of piping `man`

to `pr`

) but covers nearly 400 commands.

As an example comparison, here's the output of the `shadow`

version of `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
```


And here's the BusyBox version of the same command:

```
-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)
```


Whether or not this difference is a feature or a limitation depends on whether you prefer to have 20 options or ten options in your commands. For some users and use-cases, BusyBox's minimalism provides just enough for what needs to be done. For others, it's a good minimal environment to have as a fallback or as a foundation for installing more robust tools like [Bash](https://opensource.com/article/20/4/bash-sysadmins-ebook), [Zsh](https://opensource.com/article/19/9/getting-started-zsh), GNU [Awk](https://opensource.com/article/20/9/awk-ebook), and so on.

## Installing BusyBox

On Linux, you can install BusyBox using your package manager. For example, on Fedora and similar:

`$ sudo dnf install busybox`


On Debian and derivatives:

`$ sudo apt install busybox`


On macOS, use [MacPorts](https://opensource.com/article/20/11/macports) or [Homebrew](https://opensource.com/article/20/6/homebrew-mac). On Windows, use [Chocolatey](https://opensource.com/article/20/3/chocolatey).

You can set BusyBox as your shell using the `chsh --shell`

command, followed by the path to the BusyBox `sh`

application. I keep BusyBox in `/lib64`

, but its location depends on where your distribution installed it.

```
$ which busybox
/lib64/busybox/busybox
$ chsh --shell /lib64/busybox/sh
```


Replacing all common commands wholesale with BusyBox is a little more complex, because most distributions are "hard-wired" to look to specific packages for specific commands. In other words, while it's technically possible to replace `init`

with BusyBox's `init`

, your package manager may refuse to allow you to remove the package containing `init`

for fear of you causing your system to become non-bootable. There are some distributions built upon BusyBox, so starting fresh is probably the easiest way to experience a system built around BusyBox.

## Try BusyBox

You don't have to change your shell to BusyBox permanently just to try it. You can launch a BusyBox shell from your current shell:

```
$ busybox sh
~ $
```


Your system still has the non-BusyBox versions of commands installed, though, so to experience BusyBox's tools, you must issue commands as arguments to the `busybox`

executable:

```
~ $ 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
[...]
```


For the "full" BusyBox experience, you can create symlinks to `busybox`

for each command. This is easier than it sounds, as long as you use a [for-loop](https://opensource.com/article/19/10/programming-bash-loops):

```
$ mkdir bbx
$ for i in $(bbx --list); do \
ln -s /path/to/busybox bbx/$i \
done
```


Add your directory of symlinks at the *start* of your [path](https://opensource.com/article/17/6/set-path-linux), and launch BusyBox:

`$ PATH=$(pwd)/bbx:$PATH bbx/sh`


## Get busy

BusyBox is a fun project and an example of just how *minimal* computing can be. Whether you use BusyBox as a lightweight environment for an [ancient computer](https://opensource.com/article/20/2/restore-old-computer-linux) [you've rescued](https://opensource.com/article/19/7/how-make-old-computer-useful-again), as the userland for an [embedded device](https://opensource.com/article/20/6/open-source-rtos), to trial a new init system, or just as a curiosity, it can be fun reacquainting yourself with old familiar, yet somehow new, commands.

## Comments are closed.