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

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 exit

FORMAT 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 spaces
  0  (zero) pad with zeros
  +  pad with zeros, and put '+' before future years with >4 digits
  ^  use upper case if possible
  #  use opposite case if possible

After 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' date

Show 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.mfbz.cn/a/601695.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Golang | Leetcode Golang题解之第76题最小覆盖子串

题目&#xff1a; 题解&#xff1a; func minWindow(s string, t string) string {ori, cnt : map[byte]int{}, map[byte]int{}for i : 0; i < len(t); i {ori[t[i]]}sLen : len(s)len : math.MaxInt32ansL, ansR : -1, -1check : func() bool {for k, v : range ori {if c…

每日两题 / 138. 随机链表的复制 148. 排序链表(LeetCode热题100)

138. 随机链表的复制 - 力扣&#xff08;LeetCode&#xff09; 用哈希表记录原链表中的节点是否被复制过 遍历原链表并通过哈希表维护新链表 /* // Definition for a Node. class Node { public:int val;Node* next;Node* random;Node(int _val) {val _val;next NULL;rand…

Glowroot:Java应用的性能守护神,让监控变得既轻松又有趣!

Glowroot&#xff1a;Java应用的性能守护神&#xff0c;让监控变得既轻松又有趣&#xff01; 在这个快节奏的数字化时代&#xff0c;作为一名开发者&#xff0c;你是否曾因应用性能问题而夜不能寐&#xff1f;是不是常幻想有个超级英雄能在关键时刻挺身而出&#xff0c;帮你揪…

淘宝优惠券领取软件草柴返利APP想从淘宝粘贴提示怎么关闭?想从天猫粘贴、想从京东粘贴弹窗提示关闭

使用iPhone苹果手机想从淘宝点击分享复制链接&#xff0c;到草柴APP查询该商品内部大额隐藏优惠券和返利。可是&#xff0c;苹果iPhone手机每次将复制的商品链接&#xff0c;粘贴到草柴APP时都是提示&#xff1a;“草柴”想从“淘宝”粘贴&#xff0c;每次都需要点击允许粘贴后…

docker搭建代码审计平台sonarqube

docker搭建代码审计平台sonarqube 一、代码审计关注的质量指标二、静态分析技术分类三、sonarqube流程四、快速搭建sonarqube五、sonarqube scanner的安装和使用 一、代码审计关注的质量指标 代码坏味道 代码规范技术债评估 bug和漏洞代码重复度单测与集成 测试用例数量覆盖率…

管易云与金蝶K3-WISE对接集成发货单查询2.0打通新增销售出库(红蓝字)

管易云与金蝶K3-WISE对接集成发货单查询2.0打通新增销售出库&#xff08;红蓝字&#xff09; 源系统:管易云 金蝶管易云是金蝶集团旗下以电商和新零售为核心业务的子公司&#xff0c;公司于2008年成立&#xff0c;拥有从事电商及新零售业务相关专业知识工作者超过1000人。为伊利…

35道必懂的 Linux 运维面试题

1、现在给你三百台服务器&#xff0c;你怎么对他们进行管理&#xff1f; 管理3百台服务器的方式&#xff1a; 1&#xff09;设定跳板机&#xff0c;使用统一账号登录&#xff0c;便于安全与登录的考量。 2&#xff09;使用 salt、ansiable、puppet 进行系统的统一调度与配置的…

鸿蒙 DevEcoStudio:简单实现网络请求登录案例

使用http或axios实现登录案例 在entry/src/main/ets/pages路径下新建Page9.ets文件&#xff1a; import http from ohos.net.http import router from ohos.router Entry Component struct Page9 {State message: string Hello WorldState username: string State password:…

【网络原理】HTTP协议 | 报文格式 | Fiddler抓包 | HTTP请求 | HTTP响应 | 构造HTTP请求

文章目录 HTTP协议使用HTTP的场景&#xff1a; 一、HTTP协议的报文格式1.Fiddler的使用教程 二、HTTP请求(Request)1.首行&#xff1a;1)方法(method)GET和POST的区别(面试题) 2)URL 2.请求头&#xff08;header&#xff09;&#xff1a;1.HOST:2.Content-Length3.body中数据的…

个人IP打造孵化运营产业链商业计划书

【干货资料持续更新&#xff0c;以防走丢】 个人IP打造孵化运营产业链商业计划书 部分资料预览 资料部分是网络整理&#xff0c;仅供学习参考。 PPT共90页&#xff08;完整资料包含以下内容&#xff09; 目录 个人IP运营方案&#xff1a; 1. 个人IP定位与构建 1.1 人格画像构…

2024五一档中国电影市场研究报告

来源&#xff1a;拓普数据 2024年五一档内地电影总票房15.27亿&#xff0c;同比增长0.38%&#xff0c;低于2021年的16.74亿&#xff0c;与2019年档期票房相当。日均大盘票房3.05亿&#xff0c;同比小幅增长0.4%&#xff0c;低于2018-2021年同期&#xff0c;位居档期影史第四。…

列表处理基础问题的四种方法:从入门到惊艳

目录 一、引言 二、方法一&#xff1a;基础循环遍历 三、方法二&#xff1a;列表推导式 四、方法三&#xff1a;内置函数与高阶函数 五、方法四&#xff1a;惊艳的库与工具 六、案例研究 七、总结 一、引言 在编程的世界中&#xff0c;列表&#xff08;List&#xff09…

Element-ui-vue3-前端界面开发-配置-编辑main.js-nodejs基础语法-vue3-html模板语法-vue文件编译

前端配置 1.下载nodejs 18 lts2.配置nodejs和安装vue3.vue调试技巧3.1.debugger3.2.vue devtools4.编辑main.js5.nodejs基础语法5.1.import5.1.1.导入单个模块或组件5.1.2.导入整个模块或库5.1.3.导入默认导出5.1.4.导入 css文件5.1.5.导入模块和组件5.2.export5.2.1.命名导出5…

第十五届蓝桥杯省赛大学B组(c++)

很幸运拿了辽宁赛区的省一,进入6月1号的国赛啦... 这篇文章主要对第十五届省赛大学B组(C)进行一次完整的复盘,这次省赛2道填空题6道编程题: A.握手问题 把握手情景看成矩阵: 粉色部分是7个不能互相捂手的情况 由于每个人只能和其他人捂手, 所以黑色情况是不算的 1和2握手2和…

SpringBoot 自定义 HandlerMethodArgumentResolver 搞定xml泛型参数解析

文章目录 介绍一、解析简单 xml 数据案例引入 Jackson 的 xml 支持定义 Message 对象&MessageHeader 对象定义 Controller 方法调用结果 二、解析带泛型的 XML 数据案例2.1 直接给 Message 加上泛型 T2.2 无法直接解析泛型参数了 三、自定义 MVC 的参数解析器实现泛型参数解…

智能BI平台(后端)-- 项目介绍

文章目录 项目介绍需求分析基础架构图优化架构图技术栈 开个新坑&#xff0c;预计时间不会很长 项目介绍 BI商业智能&#xff1a;数据可视化&#xff0c;报表可视化系统 主流BI&#xff1a;帆软BI&#xff0c;小马BI&#xff0c;微软 Power BI 传统BI&#xff1a;[查看传统 BI…

精益生产咨询公司在企业转型中发挥的作用有哪些?

在全球化竞争日益激烈的今天&#xff0c;企业转型已成为许多组织求生存、谋发展的必经之路。而在这条道路上&#xff0c;精益生产咨询公司的作用愈发凸显&#xff0c;它们如同企业转型的得力助手&#xff0c;帮助企业在复杂的商业环境中找到新的增长点&#xff0c;实现更高效、…

I forgot my Plex Account PIN; how can I reset it? How can I change my PIN?

If you’ve set a PIN on your Plex account, it’s possible to reset or remove that PIN. Related Page: Plex Home Regular Plex Account If you know the current PIN If the current PIN is known, then simply edit the current PIN on the Settings > Users &…

Cisco NX-OS System Software - ACI 16.0(5h)

Cisco NX-OS System Software - ACI 16.0(5h) 适用于 ACI 模式下的 Cisco Nexus 9000 系列交换机 请访问原文链接&#xff1a;Cisco NX-OS System Software - ACI 16.0(5h)&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;sysin.org Cis…

从零开始的软件测试学习之旅(七)接口测试流程及原则案例

接口测试三要素及案例 接口测试介绍接口预定义接口测试的主要作用测试接口流程如下接口测试三要素接口测试分类RESTful架构风格RESTful架构三要素要素一要素二要素三 RESTful架构风格实现restful架构案例接口测试流程接口测试原则功能测试自动化测性能测试 复习复盘 接口测试介…
最新文章