如何在 Linux 上使用 BusyBox
- BusyBox 是一個(gè)開(kāi)源(GPL)項(xiàng)目,提供了近 400 個(gè)常用命令的簡(jiǎn)單實(shí)現(xiàn)。
我們很容易認(rèn)為 Linux 的命令是理所當(dāng)然的。當(dāng)你安裝 Linux 時(shí),它們與系統(tǒng)捆綁在一起,而我們常常不問(wèn)為什么它們會(huì)在那里。一些基本的命令,如 cd、kill 和 echo,并不總是獨(dú)立的應(yīng)用程序,而是實(shí)際上內(nèi)置于你的 shell 中。其他如 ls、mv 和 cat 是核心工具包(通常是 GNU coreutils)的一部分。但在開(kāi)源的世界里,總是有一些替代品,其中最有趣的是 BusyBox。
Linux 中的 BusyBox 簡(jiǎn)介
BusyBox 是一個(gè)開(kāi)源(GPL)項(xiàng)目,提供近 400 個(gè)常用命令的簡(jiǎn)單實(shí)現(xiàn),包括 ls、mv、ln、mkdir、more、ps、gzip、bzip2、tar 和 grep。它還包含了編程語(yǔ)言 awk、流編輯器 sed、文件系統(tǒng)檢查工具 fsck、rpm 和 dpkg 軟件包管理器,當(dāng)然還有一個(gè)可以方便的訪問(wèn)所有這些命令的 shell(sh)。簡(jiǎn)而言之,它包含了所有 POSIX 系統(tǒng)需要的基本命令,以執(zhí)行常見(jiàn)的系統(tǒng)維護(hù)任務(wù)以及許多用戶和管理任務(wù)。
事實(shí)上,它甚至包含一個(gè) init 命令,可以作為 PID 1 啟動(dòng),以作為所有其它系統(tǒng)服務(wù)的父進(jìn)程。換句話說(shuō),BusyBox 可以作為 systemd、OpenRC、sinit、init 和其他初始化系統(tǒng)的替代品。
BusyBox 非常小。作為一個(gè)可執(zhí)行文件,它不到 1MB,所以它在 嵌入式、邊緣計(jì)算 和 物聯(lián)網(wǎng) 領(lǐng)域很受歡迎,因?yàn)檫@些場(chǎng)景的存儲(chǔ)空間是很寶貴的。在容器和云計(jì)算的世界里,它作為精簡(jiǎn)的 Linux 容器鏡像的基礎(chǔ)鏡像也很受歡迎。
極簡(jiǎn)主義
BusyBox 的部分魅力在于它的極簡(jiǎn)主義。它的所有命令都被編譯到一個(gè)二進(jìn)制文件里(busybox),它的手冊(cè)只有 81 頁(yè)(根據(jù)我對(duì) man 送到 pr 管道的計(jì)算),但它涵蓋了近 400 條命令。
作為一個(gè)例子的比較,這是 “原版” 的 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 個(gè)選項(xiàng)還是 10 個(gè)選項(xiàng)。對(duì)于一些用戶和某些用例來(lái)說(shuō),BusyBox 的極簡(jiǎn)主義剛剛滿足所需。對(duì)于其他人來(lái)說(shuō),它是一個(gè)很好的最小化環(huán)境,可以作為一個(gè)后備工具,或者作為安裝更強(qiáng)大的工具的基礎(chǔ),比如 Bash、Zsh、GNU Awk 等等。
安裝 BusyBox
在 Linux 上,你可以使用你的軟件包管理器安裝 BusyBox。例如,在 Fedora 及類(lèi)似發(fā)行版:
- $ sudo dnf install busybox
在 Debian 及其衍生版:
- $ sudo apt install busybox
在 MacOS 上,可以使用 MacPorts 或 Homebrew。在 Windows 上,可以使用 Chocolatey。
你可以將 BusyBox 設(shè)置為你的 shell,使用 chsh —shell 命令,然后再加上 BusyBox sh 應(yīng)用程序的路徑。我把 BusyBox 放在 /lib64 中,但它的位置取決于你的發(fā)行版的安裝位置。
- $ which busybox
- /lib64/busybox/busybox
- $ chsh --shell /lib64/busybox/sh
用 BusyBox 全盤(pán)替換所有常見(jiàn)的命令要復(fù)雜一些,因?yàn)榇蠖鄶?shù)發(fā)行版都是“硬接線”,會(huì)在特定的軟件包尋找特定的命令。換句話說(shuō),雖然技術(shù)上可以用 BusyBox 的 init 替換系統(tǒng)的 init,但你的軟件包管理器可能會(huì)拒絕讓你刪除包含 init 的軟件包,以免你擔(dān)心刪除會(huì)導(dǎo)致系統(tǒng)無(wú)法啟動(dòng)。有一些發(fā)行版是建立在 BusyBox 之上的,所以從新環(huán)境開(kāi)始可能是體驗(yàn) BusyBox 系統(tǒng)的最簡(jiǎn)單方法。
試試 BusyBox
你不必為了嘗試 BusyBox 而將你的 shell 永久改為 BusyBox。你可以從你當(dāng)前的 shell 中啟動(dòng)一個(gè) BusyBox shell。
- $ busybox sh
- ~ $
不過(guò)你的系統(tǒng)仍然有安裝的非 BusyBox 版本的命令,所以要體驗(yàn) BusyBox 的工具,你必須把命令作為參數(shù)發(fā)給 busybox 可執(zhí)行文件:
- ~ $ 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 體驗(yàn),你可以為每個(gè)命令創(chuàng)建一個(gè) busybox 的符號(hào)鏈接。這很容易,只要你使用 for 循環(huán) 就行:
- $ mkdir bbx
- $ for i in $(bbx --list); do \
- ln -s /path/to/busybox bbx/$i \
- done
在你的 路徑 的 開(kāi)頭 添加這個(gè)符號(hào)鏈接目錄,并啟動(dòng) BusyBox:
- $ PATH=$(pwd)/bbx:$PATH bbx/sh
用起來(lái)
BusyBox 是一個(gè)有趣的項(xiàng)目,也是一個(gè)可以實(shí)現(xiàn) 極簡(jiǎn) 計(jì)算的例子。無(wú)論你是把 BusyBox 作為 你喚醒的 古老的計(jì)算機(jī) 的輕量級(jí)環(huán)境,還是作為 嵌入式設(shè)備 的用戶界面,抑或試用一個(gè)新的初始化系統(tǒng),就算是為了好奇,讓自己重新認(rèn)識(shí)那些熟悉而又陌生的命令,都會(huì)很有意思。