busybox被称为嵌入式linux的瑞士军刀,这句话是在busybox自己介绍自己的时候提出的(The Swiss Army Knife of Embedded Linux)。busybox整合了很多小的unix下的通用功能到一个小的可执行文件之中,简单来说在unix或者linux下常用的那些功能在这里你都能找到,但是为了busybox的目标:嵌入式的linux,大小对于busybox来说是非常重要的优化要素和限制,这些功能有可能会有所阉割,但是对于一般需求来说已经足够。而alpine就是在busybox基础上增加了自己的包管理工具apk等功能创建了风靡一时的小巧镜像。busybox是用C语言开发的基于GPL的开源项目
下载地址
https://busybox.net/downloads/binaries/
宿主机器
[root@myhost ~]# uname -a Linux myhost 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux [root@myhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@myhost ~]#
下载busybox
简单可以直接使用的二进制最新的版本为1.31.0
[root@myhost ~]# wget https://busybox.net/downloads/binaries/1.31.0-defconfig-multiarch-musl/busybox-x86_64 --2018-05-25 04:51:20-- https://busybox.net/downloads/binaries/1.31.0-defconfig-multiarch-musl/busybox-x86_64 Resolving busybox.net (busybox.net)... 140.211.167.122 Connecting to busybox.net (busybox.net)|140.211.167.122|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 1001112 (978K) Saving to: 'busybox-x86_64' 100%[==============================================================================================>] 1,001,112 19.3KB/s in 30s 2018-05-25 04:51:57 (32.4 KB/s) - 'busybox-x86_64' saved [1001112/1001112] [root@myhost ~]#
设定busybox
[root@myhost ~]# cp busybox-x86_64 /usr/local/bin/busybox [root@myhost ~]# chmod +x /usr/local/bin/busybox [root@myhost ~]# which busybox /usr/local/bin/busybox [root@myhost ~]#
版本确认
输入busybox可以看出版本以及熟悉的linux下的工具,仔细看一遍就会理解busybox号称自己是瑞士军刀一点都不夸张,反过来说,瑞士军刀如果敢号称linux里的busybox可能会引起非议。
[root@myhost ~]# busybox BusyBox v1.31.0 (2019-06-10 15:54:54 CEST) multi-call binary. BusyBox is copyrighted by many authors between 1998-2015. Licensed under GPLv2. See source distribution for detailed copyright notices. Usage: busybox [function [arguments]...] or: busybox --list[-full] or: busybox --show SCRIPT or: busybox --install [-s] [DIR] or: function [arguments]... BusyBox is a multi-call binary that combines many common Unix utilities into a single executable. Most people will create a link to busybox for each function they wish to use and BusyBox will act like whatever it was invoked as. Currently defined functions: [, [[, acpid, add-shell, addgroup, adduser, adjtimex, arch, arp, arping, ash, awk, base64, basename, bc, beep, blkdiscard, blkid, blockdev, bootchartd, brctl, bunzip2, bzcat, bzip2, cal, cat, chat, chattr, chgrp, chmod, chown, chpasswd, chpst, chroot, chrt, chvt, cksum, clear, cmp, comm, conspy, cp, cpio, crond, crontab, cryptpw, cttyhack, cut, date, dc, dd, deallocvt, delgroup, deluser, depmod, devmem, df, dhcprelay, diff, dirname, dmesg, dnsd, dnsdomainname, dos2unix, dpkg, dpkg-deb, du, dumpkmap, dumpleases, echo, ed, egrep, eject, env, envdir, envuidgid, ether-wake, expand, expr, factor, fakeidentd, fallocate, false, fatattr, fbset, fbsplash, fdflush, fdformat, fdisk, fgconsole, fgrep, find, findfs, flock, fold, free, freeramdisk, fsck, fsck.minix, fsfreeze, fstrim, fsync, ftpd, ftpget, ftpput, fuser, getopt, getty, grep, groups, gunzip, gzip, halt, hd, hdparm, head, hexdump, hexedit, hostid, hostname, httpd, hush, hwclock, i2cdetect, i2cdump, i2cget, i2cset, i2ctransfer, id, ifconfig, ifdown, ifenslave, ifplugd, ifup, inetd, init, insmod, install, ionice, iostat, ip, ipaddr, ipcalc, ipcrm, ipcs, iplink, ipneigh, iproute, iprule, iptunnel, kbd_mode, kill, killall, killall5, klogd, last, less, link, linux32, linux64, linuxrc, ln, loadfont, loadkmap, logger, login, logname, logread, losetup, lpd, lpq, lpr, ls, lsattr, lsmod, lsof, lspci, lsscsi, lsusb, lzcat, lzma, lzop, makedevs, makemime, man, md5sum, mdev, mesg, microcom, mkdir, mkdosfs, mke2fs, mkfifo, mkfs.ext2, mkfs.minix, mkfs.vfat, mknod, mkpasswd, mkswap, mktemp, modinfo, modprobe, more, mount, mountpoint, mpstat, mt, mv, nameif, nanddump, nandwrite, nbd-client, nc, netstat, nice, nl, nmeter, nohup, nologin, nproc, nsenter, nslookup, ntpd, nuke, od, openvt, partprobe, passwd, paste, patch, pgrep, pidof, ping, ping6, pipe_progress, pivot_root, pkill, pmap, popmaildir, poweroff, powertop, printenv, printf, ps, pscan, pstree, pwd, pwdx, raidautorun, rdate, rdev, readahead, readlink, readprofile, realpath, reboot, reformime, remove-shell, renice, reset, resize, resume, rev, rm, rmdir, rmmod, route, rpm, rpm2cpio, rtcwake, run-init, run-parts, runlevel, runsv, runsvdir, rx, script, scriptreplay, sed, sendmail, seq, setarch, setconsole, setfattr, setfont, setkeycodes, setlogcons, setpriv, setserial, setsid, setuidgid, sh, sha1sum, sha256sum, sha3sum, sha512sum, showkey, shred, shuf, slattach, sleep, smemcap, softlimit, sort, split, ssl_client, start-stop-daemon, stat, strings, stty, su, sulogin, sum, sv, svc, svlogd, svok, swapoff, swapon, switch_root, sync, sysctl, syslogd, tac, tail, tar, taskset, tc, tcpsvd, tee, telnet, telnetd, test, tftp, tftpd, time, timeout, top, touch, tr, traceroute, traceroute6, true, truncate, ts, tty, ttysize, tunctl, ubiattach, ubidetach, ubimkvol, ubirename, ubirmvol, ubirsvol, ubiupdatevol, udhcpc, udhcpc6, udhcpd, udpsvd, uevent, umount, uname, unexpand, uniq, unix2dos, unlink, unlzma, unshare, unxz, unzip, uptime, users, usleep, uudecode, uuencode, vconfig, vi, vlock, volname, w, wall, watch, watchdog, wc, wget, which, who, whoami, whois, xargs, xxd, xz, xzcat, yes, zcat, zcip [root@myhost ~]#
使用busybox
直接busybox后面跟具体的命令即可
[root@myhost ~]# busybox ping baidu.com PING baidu.com (220.181.38.148): 56 data bytes
在Docker容器中使用busybox
众所周知,Docker容器里面命令少得可怜,想看一下ip,执行ip addr提示命令不存在,目前的办法是把busybox复制到Docker容器中使用
#在宿主机外面给busybox文件赋上执行权限(必须这样做,否则复制到容器中可能没有执行权限,且在容器中可能没有cp命令或chmod命令的操作权限)
[root@myhost ~]# chmod +x "/home/test/busybox/busybox-x86_64"
#然后再复制到容器中,就可以愉快地使用了
[root@myhost ~]# docker cp "/home/test/busybox/busybox-x86_64" 7b949cba3276:/usr/local/bin/busybox