用标准的GNU/Linux命令替换Alpine上的精简版命令

news/2024/9/22 16:06:49/

Alpine Linux 是一个基于 musl libc 和 busybox 的轻量级Linux发行版,busybox 实现了很多常用类Unix命令的精简版,特点是体积很小,舍弃了很多不常用参数,我们简单对比一下标准Linux自带的 date 命令 和 Alpine下默认的 date 命令便可略知一二:

GNU/Linux 版:

root@spacex:~# date --help
Usage: date [OPTION]... [+FORMAT]or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.Mandatory arguments to long options are mandatory for short options too.-d, --date=STRING          display time described by STRING, not 'now'--debug                annotate the parsed date,and warn about questionable usage to stderr-f, --file=DATEFILE        like --date; once for each line of DATEFILE-I[FMT], --iso-8601[=FMT]  output date/time in ISO 8601 format.FMT='date' for date only (the default),'hours', 'minutes', 'seconds', or 'ns'for date and time to the indicated precision.Example: 2006-08-14T02:34:56-06:00-R, --rfc-email            output date and time in RFC 5322 format.Example: Mon, 14 Aug 2006 02:34:56 -0600--rfc-3339=FMT         output date/time in RFC 3339 format.FMT='date', 'seconds', or 'ns'for date and time to the indicated precision.Example: 2006-08-14 02:34:56-06:00-r, --reference=FILE       display the last modification time of FILE-s, --set=STRING           set time described by STRING-u, --utc, --universal     print or set Coordinated Universal Time (UTC)--help     display this help and exit--version  output version information and exitFORMAT controls the output.  Interpreted sequences are:%%   a literal %%a   locale's abbreviated weekday name (e.g., Sun)%A   locale's full weekday name (e.g., Sunday)%b   locale's abbreviated month name (e.g., Jan)%B   locale's full month name (e.g., January)%c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)%C   century; like %Y, except omit last two digits (e.g., 20)%d   day of month (e.g., 01)%D   date; same as %m/%d/%y%e   day of month, space padded; same as %_d%F   full date; like %+4Y-%m-%d%g   last two digits of year of ISO week number (see %G)%G   year of ISO week number (see %V); normally useful only with %V%h   same as %b%H   hour (00..23)%I   hour (01..12)%j   day of year (001..366)%k   hour, space padded ( 0..23); same as %_H%l   hour, space padded ( 1..12); same as %_I%m   month (01..12)%M   minute (00..59)%n   a newline%N   nanoseconds (000000000..999999999)%p   locale's equivalent of either AM or PM; blank if not known%P   like %p, but lower case%q   quarter of year (1..4)%r   locale's 12-hour clock time (e.g., 11:11:04 PM)%R   24-hour hour and minute; same as %H:%M%s   seconds since 1970-01-01 00:00:00 UTC%S   second (00..60)%t   a tab%T   time; same as %H:%M:%S%u   day of week (1..7); 1 is Monday%U   week number of year, with Sunday as first day of week (00..53)%V   ISO week number, with Monday as first day of week (01..53)%w   day of week (0..6); 0 is Sunday%W   week number of year, with Monday as first day of week (00..53)%x   locale's date representation (e.g., 12/31/99)%X   locale's time representation (e.g., 23:13:48)%y   last two digits of year (00..99)%Y   year%z   +hhmm numeric time zone (e.g., -0400)%:z  +hh:mm numeric time zone (e.g., -04:00)%::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)%:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)%Z   alphabetic time zone abbreviation (e.g., EDT)By default, date pads numeric fields with zeroes.
The following optional flags may follow '%':-  (hyphen) do not pad the field_  (underscore) pad with spaces0  (zero) pad with zeros+  pad with zeros, and put '+' before future years with >4 digits^  use upper case if possible#  use opposite case if possibleAfter any flags comes an optional field width, as a decimal number;
then an optional modifier, which is either
E to use the locale's alternate representations if available, or
O to use the locale's alternate numeric symbols if available.Examples:
Convert seconds since the epoch (1970-01-01 UTC) to a date$ date --date='@2147483647'Show the time on the west coast of the US (use tzselect(1) to find TZ)$ TZ='America/Los_Angeles' dateShow the local time for 9AM next Friday on the west coast of the US$ date --date='TZ="America/Los_Angeles" 09:00 next Fri'GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/date>
or available locally via: info '(coreutils) date invocation'
root@spacex:~#

busybox版:

在这里插入图片描述

可以看到参数少了很多,虽然大部分情况下busybox的命令都足够用了,但遇到确实不能满足我们需求的时候,比如要运行一个基于标准Linux编写的shell脚本,其中用了很多命令的高级参数,对此,Alpine Linux 也提供了替换标准Linux 命令的方法。

默认情况下,Alpine下的一些常用Linux 命令其实都是指向 busybox 的软链,包括 ls 命令本身
在这里插入图片描述
除了上述列出的 /bin 目录下的命令,还有 /sbin//usr/bin//usr/sbin/ 目录下的大多命令都是如此。

要想替换Alpine 下的 busybox 命令,我们可以执行:

apk add util-linux pciutils hwdata-pci usbutils hwdata-usb coreutils binutils findutils grep iproute2 bash bash-completion

替换完之后,这些软链接要么被替换成了具体的可执行文件,要么还是软链,但不再指向busybox了:
在这里插入图片描述

如果只想替换某个具体用到的,那就要找出该命令所在的 GNU 工具包了,然后只安装该工具包即可,从而尽量保持体积不要太过膨胀,根据我目前的经验找出某个具体命令所在的工具包大致有以下几种方法供参考:

  1. 在 Alpine apk仓库 中搜索指定命令有没有提供单独的软件包,比如 grepsed 即是此类

在这里插入图片描述
在这里插入图片描述
如果存在就好办了,直接执行 apk add xxx 即可完成替换
在这里插入图片描述

  1. 查看标准Linux命令的帮助文档,看有没有提供包信息

比如从 date --help 的帮助文档我们可以知道它在 GNU coreutils 工具包下:
在这里插入图片描述

  1. 查看busybox项目的源码结构和文件命名,busybox通常一个命令就是一个源码文件。

比如 fdisk,flock, getopt 这些命令都是由 util-linux 包提供的,如果我们只用到其中的命令,只安装 util-linux 即可
在这里插入图片描述
同理,find, grep, xargs 三个命令由 findutils 包提供
在这里插入图片描述
还可以直接尝试搜索某个命令,看是否有对应的源文件,直接就能定位到软件包:
在这里插入图片描述

  1. apk info 查看某个包的具体介绍,从而推断可能包含哪些命令

在这里插入图片描述

  1. 实在不行只能一个个的尝试,看到安装了哪个包之后,目标软件的软链被替换掉了,说明这个就是我们要找的。

  2. 没办法的办法,自己编译,因为 Alpine 使用 musl libc 而不是标准 glibc,编译会更加麻烦, 这里以编译 debian cron 为例简单演示下编译步骤

#安装编译工具链
apk add build-base abuild autoconf automake libtool# 下载源码包,解压
wget https://salsa.debian.org/debian/cron/-/archive/updates/buster/cron-updates-buster.tar.gz# 编译
cd cron-updates-buster && make
cc -I. -DPOSIX -DDEBUGGING=0      -c -o cron.o cron.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o database.o database.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o user.o user.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o entry.o entry.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o job.o job.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o do_command.o do_command.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o misc.o misc.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o env.o env.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o popen.o popen.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o compat.o compat.c
cc  -o cron cron.o database.o user.o entry.o job.o do_command.o misc.o env.o popen.o compat.o
cc -I. -DPOSIX -DDEBUGGING=0      -c -o crontab.o crontab.c
cc  -o crontab crontab.o misc.o entry.o env.o compat.o# 替换busybox版本
bash-5.1# which crontab
/usr/bin/crontab
bash-5.1# ls -l /usr/bin/crontab
lrwxrwxrwx    1 root     root            12 Nov 24  2021 /usr/bin/crontab -> /bin/busybox
bash-5.1# install ./crontab /usr/bin/crontab
bash-5.1# ls -l /usr/bin/crontab
-rwxr-xr-x    1 root     root         53696 May  7 04:27 /usr/bin/crontab
bash-5.1#
bash-5.1# which crond
/usr/sbin/crond
bash-5.1#
bash-5.1# ls -l /usr/sbin/crond
lrwxrwxrwx    1 root     root            12 Nov 24  2021 /usr/sbin/crond -> /bin/busybox
bash-5.1#
bash-5.1# install ./cron /usr/sbin/cron
bash-5.1#
bash-5.1# ln -sf /usr/sbin/cron /usr/sbin/crond
bash-5.1#
bash-5.1# ls -l /usr/sbin/crond
lrwxrwxrwx    1 root     root            14 May  7 04:30 /usr/sbin/crond -> /usr/sbin/cron# 测试cron能否正常工作,略..

http://www.ppmy.cn/news/1459521.html

相关文章

PyQt5 的对话框

文章目录 1. 简介2. 输入对话框3. 文件对话框4. 颜色对话框5. 字体对话框6. 进度对话框 1. 简介 PyQt5中的对话框是一种常见的用户界面组件&#xff0c;用于与用户进行交互并获取用户输入或确认。常见的对话框包括消息框、输入框、文件对话框、颜色对话框等。PyQt5提供了丰富的…

认识下MapReduce

&#x1f50d; 什么是MapReduce&#xff1f; MapReduce是一种分布式计算模型&#xff0c;最初由Google提出&#xff0c;用于处理大规模数据集的并行计算。它将数据处理任务分解成独立的Map和Reduce两个阶段&#xff0c;以实现分布式计算和并行化处理。Map阶段负责将输入数据映…

VLAN 综合实验

一、实验拓扑 二、实验需求 1.PC1和PC3所在接口为access&#xff0c;属于vlan2&#xff1b; PC2/4/5/6处于同一网段&#xff0c;其中PC2可以访问PC4/5/6; 2.PC5不能访问PC6; 3.PC1/3与PC2/4/5/6不在同一网段&#xff1b; 4.所有PC通过DHCP获取IP地址&#xff0c;且PC1/3可以…

吴恩达深度学习笔记:深度学习的 实践层面 (Practical aspects of Deep Learning)1.13-1.14

目录 第二门课: 改善深层神经网络&#xff1a;超参数调试、正 则 化 以 及 优 化 (Improving Deep Neural Networks:Hyperparameter tuning, Regularization and Optimization)第一周&#xff1a;深度学习的 实践层面 (Practical aspects of Deep Learning)1.13 梯度检验&#…

【Redis】用户登录校验

对于用 redis 对用户进行登录校验&#xff0c;大致可分为以下六步&#xff1a; 首先通过查询数据库来查找具有提供的用户名、密码和delFlag值为0的用户。如果未找到用户&#xff0c;则抛出一个带有消息"用户不存在"的ClientException&#xff08;用户不存在&#xf…

如何在本地调试THUDM/chatglm2-6b大模型

模型下载网站&#xff1a;https://www.opencsg.com/models 安装git&#xff1a; sudo apt install git安装git-lfs&#xff0c;这个很重要。 sudo apt-get install git-lfs下载模型&#xff1a;THUDM/chatglm2-6b mkdir THUDM cd THUDMgit lfs intsall git clone https://p…

kNN 算法在 Elasticsearch 中的应用

kNN 算法在 Elasticsearch 中的应用 kNN 算法在 Elasticsearch 中的应用一、知识背景二、Elasticsearch的kNN算法三、Elasticsearch中kNN算法的应用Elasticsearch基于kNN算法的相关插件或模块使用kNN算法进行基于相似度的文本搜索使用kNN算法进行基于相似度的推荐系统构建 kNN …

使用torch.nn.ModuleList构建神经网络

在 PyTorch 中&#xff0c;torch.nn.ModuleList 是一个持有子模块的类&#xff0c;它是 torch.nn.Module 的一个子类。与 torch.nn.Sequential 不同&#xff0c;ModuleList 不会自动地对添加到其中的模块进行前向传播。相反&#xff0c;它主要用于存储多个模块&#xff0c;并且…