BusyBox 是一個開源(GPL)專案,提供了近 400 個常用命令的簡單實現。
我們很容易認為 Linux 的命令是理所當然的。當你安裝 Linux 時,它們與系統捆綁在一起,而我們常常不問為什麼它們會在那裡。一些基本的命令,如 cd、kill和 echo,並不總是獨立的應用程式,而是實際上內置於你的 shell 中。其他如ls、mv和cat是核心工具包(通常是 GNU
coreutils
)的一部分。但在開源的世界裡,總是有一些替代品,其中最有趣的是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
命令,然後再加上 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 $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中國榮譽推出