TranslateProject/sources/tech/20210907 How to use BusyBox on Linux.md
DarkSun 0495e6d101 选题[tech]: 20210907 How to use BusyBox on Linux
sources/tech/20210907 How to use BusyBox on Linux.md
2021-09-08 05:03:56 +08:00

9.1 KiB

How to use BusyBox on Linux

BusyBox is an open source (GPL) project providing simple implementations of nearly 400 common commands. bash logo on green background

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, kill, and echo aren't always independent applications but are actually built into your shell. Others, such as ls, mv, and cat are part of a core utility package (often GNU coreutils specifically). But there are always alternatives in the world of open source, and one of the most interesting is BusyBox.

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, 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, Edge, and IoT 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, Zsh, GNU Awk, 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 or Homebrew. On Windows, use 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:

$ 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, 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 you've rescued, as the userland for an embedded device, to trial a new init system, or just as a curiosity, it can be fun reacquainting yourself with old familiar, yet somehow new, commands.


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

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

本文由 LCTT 原创编译,Linux中国 荣誉推出