How to use BusyBox on Linux

When using linux, you are probably too familiar with the Terminal command line. Some basic commands, such as cd, kill, and echo are built into your shell. Other commands such as ls, mv and cat are part of the core utility package (usually GNU cores). But there are always other options in the open source world, and one of the most interesting is BusyBox.

How to use BusyBox on Linux

What is BusyBox in Linux?

BusyBox is an open source (GPL) project that provides 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 awkeditor sedfile system checker fsckpackage manager rpm and dpkg, and of course, a shell (sh) that provides easy access to all these commands. In short, it contains all the commands required for a POSIX system to perform general system maintenance tasks as well as many administrative and user tasks.

In fact, BusyBox even contains an init command that 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 light, just under 1 MB, so it has gained popularity in embedded, Edge and IoT devices. In the world of containers and cloud computing, BusyBox is also popular as a platform for Linux container images.

Simplify

Part of BusyBox’s appeal is its minimalism. All commands are compiled into a single binary file (busybox) and has only 81 pages but includes nearly 400 commands.

As a comparison example, 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 is 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 this difference is a feature or a limitation depends on whether you want 20 options or ten options in your commands. For some users and use cases, BusyBox’s minimalism provides just enough of what the user wants. For others, this is a minimal environment for fallback or as a base to install more powerful tools like Bash, Zsh, GNU Awk, etc.

How to install BusyBox

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

sudo dnf install busybox

On Debian:

sudo apt install busybox

On macOS, use MacPorts or Homebrew. On Windows, use Chocolatey.

You can set BusyBox as a shell with the command chsh --shellfollowed by the link to the BusyBox app sh. I save BusyBox in /lib64, but its location depends on where you installed the distribution.

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

Replacing all common commands with BusyBox is a bit more complicated, because most distributions “default” to search 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 let you remove a package containing init for fear you’ll render the system unbootable. There are several distributions built around BusyBox, so this 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 the BusyBox shell from your current shell:

$ busybox sh
~ $

However, your system still has non-BusyBox versions of commands installed, so to experience BusyBox’s tools you must enter the 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 imagined, 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 the bind directory to the beginning of your path and launch BusyBox:

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

BusyBox is a fun project and an example of the bare minimum of computing. Whether you use BusyBox as a lightweight environment for an old computer, or use it for an embedded device, to try out a new init system, or just curious, you can give BusyBox a try. .

In addition, you should also take a look at the 10 most dangerous Linux commands here.

Leave a Reply