10 个 Alpine Linux apk 命令示例
您是Alpine Linux 系统管理员工作或用户吗?您想学习如何使用 apk 命令行实用程序在云中运行的 Alpine Linux 服务器或 Linux 容器上进行包管理吗?让我们看看如何通过基于 ssh 的会话或终端应用程序使用 Alpine 包管理器。
apk 命令详细信息 | |
---|---|
描述 | Alpine Linux 软件包管理器 |
类别 | 包管理器 |
难度等级 | 简单的 |
Root 权限 | 是的 |
操作系统兼容性 | Alpine • Linux |
预计阅读时间 | 6 分钟 |
存储库列表存储在/etc/apk/repositories配置文件中。使用cat /more/less/ bat 命令查看 /etc/apk/repositories 文件。Alpine Linux 软件包通常具有名为“a-packs”的 .apk 扩展名。apk 命令相当于Debian/Ubuntu 上的apt / apt-get 命令、CentOS/RHEL Linux 上的yum 命令/dnf 命令或 SuSE/OpenSUSE Linux 上的 zypper 命令。
目的
使用 apk 以一致的方式安装、升级、配置和删除 Alpine Linux 操作系统的应用程序/程序。
句法
基本语法如下:
apk [options] command apk [options] command pkgName apk [options] command pkgName1 pkgName2
Alpine Linux apk 命令示例
让我们看看如何使用 apk 命令在 Alpine Linux 服务器上安装安全更新或新软件包集。
如何更新软件包列表
要更新您的包裹列表,请输入:
# apk update
示例输出:
图 01:如何在 Alpine Linux 中更新软件包列表
如何搜索包
语法是:
# apk search pkgName
例如,搜索名为 htop 的包,运行:
# apk search htop
示例输出:
htop-doc-2.0.2-r0 htop-2.0.2-r0
搜索并显示描述:
# apk search -v -d 'htop'
示例输出:
htop-doc-2.0.2-r0 - An interactive process viewer (documentation) htop-2.0.2-r0 - An interactive process viewer
列出所有可用的包及其描述
# apk search -v
示例输出:
gettext-0.19.8.1-r0 - GNU locale utilities gst-plugins-base0.10-dev-0.10.36-r3 - GStreamer Multimedia Framework Base Plugins (development files) xcb-util-keysyms-dev-0.4.0-r0 - Utility libraries for XC Binding - keysyms (development files) openlibm-0.5.4-r0 - High quality system independent, portable, open source libm implementation imapsync-doc-1.727-r1 - IMAP synchronisation, sync, copy or migration tool (documentation) evince-lang-3.22.1-r0 - Languages for package evince jack-1.9.10-r2 - The Jack Audio Connection Kit php7-intl-7.0.16-r0 - PHP7 extension: intl php5-5.6.30-r0 - The PHP language runtime engine samba-libs-4.5.7-r0 - Samba core libraries gst-plugins-bad1-1.8.3-r0 - GStreamer bad plugins docker-bash-completion-1.12.6-r0 - Bash completion for Docker mesa-gl-12.0.3-r0 - Mesa libGL runtime libraries nagios-plugins-mrtg-2.1.4-r2 - Nagios plugin check_mrtg ..... .. .... nrpe-2.15-r4 - NRPE allows you to remotely execute Nagios plugins on other Linux/Unix machines. py2-psycopg2-2.6.2-r1 - A Python-PostgreSQL Database Adapter (for python2) perl-mime-types-2.13-r0 - Definition of MIME types perl-net-http-doc-6.09-r0 - Net::HTTP perl module (documentation) shared-mime-info-lang-1.8-r0 - Languages for package shared-mime-info altermime-0.3.11-r0 - alterMIME - MIME encoded email pack alteration tool at-3.1.20-r0 - AT and batch delayed command scheduling utility and daemon fbida-2.12-r0 - Few applications to display and elementary edit images
如何通过通配符搜索包?
搜索所有 php7 包或 php5 包的语法如下:
示例输出:
# apk search -v 'php5*'
### OR ###
# apk search -v 'php7*'
php7-intl-7.0.16-r0 - PHP7 extension: intl php7-openssl-7.0.16-r0 - PHP7 extension: openssl php7-dba-7.0.16-r0 - PHP7 extension: dba php7-sqlite3-7.0.16-r0 - PHP7 extension: sqlite3 php7-pear-7.0.16-r0 - PHP Extension and Application Repository php7-phpdbg-7.0.16-r0 - Interactive PHP debugger php7-litespeed-7.0.16-r0 - PHP LiteSpeed SAPI php7-gmp-7.0.16-r0 - PHP7 extension: gmp php7-pdo_mysql-7.0.16-r0 - PHP7 extension: pdo_mysql php7-pcntl-7.0.16-r0 - PHP7 extension: pcntl php7-common-7.0.16-r0 - The PHP language runtime engine - 7th branch (common config) php7-xsl-7.0.16-r0 - PHP7 extension: xsl php7-fpm-7.0.16-r0 - PHP FastCGI Process Manager php7-mysqlnd-7.0.16-r0 - PHP7 extension: mysqlnd php7-enchant-7.0.16-r0 - PHP7 extension: enchant php7-pspell-7.0.16-r0 - PHP7 extension: pspell php7-snmp-7.0.16-r0 - PHP7 extension: snmp .... .. ... php7-sockets-7.0.16-r0 - PHP7 extension: sockets php7-soap-7.0.16-r0 - PHP7 extension: soap php7-apcu-5.1.8-r0 - PHP extension APC User Cache php7-sysvmsg-7.0.16-r0 - PHP7 extension: sysvmsg php7-zlib-7.0.16-r0 - PHP7 extension: zlib php7-ftp-7.0.16-r0 - PHP7 extension: ftp php7-sysvsem-7.0.16-r0 - PHP7 extension: sysvsem php7-pdo-7.0.16-r0 - PHP7 extension: pdo php7-bz2-7.0.16-r0 - PHP7 extension: bz2 php7-mysqli-7.0.16-r0 - PHP7 extension: mysqli
如何按名称安装软件包
语法是:
要安装 htop 包,请运行:
示例输出:
# apk add pkgName
# apk add pkgName1 pkgName2
# apk add htop
(1/1) Installing htop (2.0.2-r0) Executing busybox-1.25.1-r0.trigger OK: 39 MiB in 28 packages
要安装 Apache2 以及 PHP7 和模块,请运行:
# apk add apache2 php7-apache2 php7-gd php7-mysqli
示例输出:
(1/28) Installing libuuid (2.28.2-r1) (2/28) Installing apr (1.5.2-r1) (3/28) Installing expat (2.2.0-r0) (4/28) Installing apr-util (1.5.4-r2) (5/28) Installing pcre (8.39-r0) (6/28) Installing apache2 (2.4.25-r0) Executing apache2-2.4.25-r0.pre-install (7/28) Installing php7-common (7.0.16-r0) (8/28) Installing libedit (20150325.3.1-r3) (9/28) Installing libxml2 (2.9.4-r2) (10/28) Installing php7-apache2 (7.0.16-r0) (11/28) Installing libxau (1.0.8-r1) (12/28) Installing libxdmcp (1.1.2-r2) (13/28) Installing libxcb (1.12-r0) (14/28) Installing libx11 (1.6.4-r0) (15/28) Installing libxext (1.3.3-r1) (16/28) Installing libice (1.0.9-r1) (17/28) Installing libsm (1.2.2-r0) (18/28) Installing libxt (1.1.5-r0) (19/28) Installing libxpm (3.5.12-r0) (20/28) Installing libbz2 (1.0.6-r5) (21/28) Installing libpng (1.6.25-r0) (22/28) Installing freetype (2.7-r1) (23/28) Installing libjpeg-turbo (1.5.1-r0) (24/28) Installing libwebp (0.5.2-r0) (25/28) Installing php7-gd (7.0.16-r0) (26/28) Installing mariadb-common (10.1.22-r0) (27/28) Installing mariadb-client-libs (10.1.22-r0) (28/28) Installing php7-mysqli (7.0.16-r0) Executing busybox-1.25.1-r0.trigger OK: 64 MiB in 56 packages
交互式安装或升级
我们可以通过传递以下-i选项在执行某些操作之前强制确认:
# apk -i add nginx
# apk -i upgrade
The following packages will be upgraded: libcrypto1.1 libssl1.1 alpine-base linux-lts xtables-addons-lts openssh-keygen openssh-client openssh-sftp-server openssh-server-common openssh-server openssh openssl zfs-lts After this operation, 16 KiB of additional disk space will be used. Do you want to continue [Y/n]?
使用apk命令进行模拟
我们可以模拟所请求的操作而不做任何更改。有助于查看将升级哪些软件包或将在 Alpine Linux 系统上执行哪些操作:
换句话说,系统上没有安装或升级任何内容,但您将确切知道 apk 将要执行的操作。
# apk -s command
# apk -s add nginx
# apk -s upgrade
如何保留特定软件包而不进行升级
如果您想要升级 Alpine Linux 系统,但要保留或暂挂特定软件包,请添加版本号。例如,要将 bash 软件包暂挂到版本 5.0.0-r0 级别或更低,请运行:
可以进行基于正则表达式的版本匹配,以将版本暂挂到主要/次要版本。例如:
现在,升级系统。但是,apk 将升级整个系统,将 bash 软件包暂挂在 5.0.0-r0 或更低级别:
可以取消暂挂。例如,确保将 bash 升级到当前最新版本,请运行:
# apk add bash=5.0.0-r0
# apk add bash=~5.0
# apk upgrade
# apk add bash>5.0.0-r0
如何安装本地的.apk文件包?
添加名为 foo.apk 的本地包的语法如下:
# apk add --allow-untrusted /path/to/foo.apk
# apk add --allow-untrusted pkg1.apk pkg2.apk
如何按名称移除或删除包
语法是:
要删除 htop 包,请运行:
示例输出:
# apk del pkgName
# apk del pkgName1 pkgName2
# apk del htop
(1/1) Purging htop (2.0.2-r0) Executing busybox-1.25.1-r0.trigger OK: 39 MiB in 27 packages
如何删除 Alpine Linux 上的旧包缓存?
要删除旧版本的软件包,请运行 clean 命令,如下所示:
还可以一步清除缓存并下载丢失的软件包:
# apk cache clean
## or ##
# apk -v cache clean
# apk cache -v sync
如何升级正在运行的 Alpine Linux
语法是:
# apk update && apk upgrade
您可以在 ~/.bashrc 中创建一个 bash shell 别名,
# echo "alias update='apk update && apk upgrade'" >> /.bashrc
如下所示运行它:
# update
如何仅升级选定的软件包?
语法是
# apk add -u pkgName
要升级仅 htop 包:
# apk update
# apk add -u htop
如何列出已安装的软件包?
语法是:
# apk info
# filter out info using the grep command #
# apk info -vv | grep 'foo'
# Get verbose outputs and sort it using the sort command #
# apk info -vv | sort
图 02:如何在 Alpine Linux 中显示/列出已安装的软件包
找出文件属于哪个包..
确定名为 /etc/passwd 或 /sbin/apk 的文件属于哪个包:
# apk info --who-owns /etc/passwd
/etc/passwd is owned by alpine-baselayout-3.0.4-r0
# apk info --who-owns /sbin/apk
/sbin/apk is owned by apk-tools-2.6.8-r2
列出 APK 包的内容
# apk -L info pkgName
# apk -L info htop
示例输出:
htop-2.0.2-r0 contains: usr/bin/htop usr/share/applications/htop.desktop usr/share/pixmaps/inwpfcighdlhcijjwlyhyspjrfjcuwed.png
检查 PACKAGE 是否安装
# apk -e info pkgName
#############################################
### find out if atop PACKAGE is installed ###
#############################################
# apk -e info atop
如果未安装 PACKAGE,则不会显示任何输出。
列出 PACKAGE 所依赖的软件包
# apk -R info atop
# apk -R info atop
示例输出:
atop-2.2_p3-r0 depends on: so:libc.musl-x86_64.so.1 so:libncursesw.so.6 so:libz.so.1
列出所有依赖于 PACKAGE 的包
传递-r给 apk 命令:
示例输出:
# apk info -r pkgNameHere
# For example, list all pkgs depending upon GNU/bash #
# apk info -r bash
bash-5.1.16-r0 is required by: bash-completion-2.11-r4 wireguard-tools-wg-quick-1.0.20210914-r0
显示 PACKAGE 的安装大小
您需要将其传递-s给 apk 命令,如下所示:
示例输出:
# apk info -s pkgName
# Let us list size of a package named 'atop' #
# apk info -s atop
atop-2.2_p3-r0 installed size: 520192
打印 PACKAGE 的描述
想要获取有关 APK 包的简短描述?请尝试传递-d以下内容:
这是我看到的:
# apk info -d pkgName
# apk info -d bash
bash-5.1.16-r0 description: The GNU Bourne Again shell
打印有关 PACKAGE 的所有信息
将选项传递-a给 apk 命令如下:
示例输出:
# apk info -a pkgName
# Get info about GNU/bash, run: #
# apk info -a bash
bash-5.1.16-r0 description: The GNU Bourne Again shell bash-5.1.16-r0 webpage: https://www.gnu.org/software/bash/bash.html bash-5.1.16-r0 installed size: 1296 KiB bash-5.1.16-r0 depends on: /bin/sh so:libc.musl-x86_64.so.1 so:libreadline.so.8 bash-5.1.16-r0 provides: cmd:bash=5.1.16-r0 bash-5.1.16-r0 is required by: bash-completion-2.11-r4 wireguard-tools-wg-quick-1.0.20210914-r0 bash-5.1.16-r0 contains: bin/bash usr/lib/bash/accept usr/lib/bash/basename usr/lib/bash/csv usr/lib/bash/cut usr/lib/bash/dirname usr/lib/bash/fdflags usr/lib/bash/finfo usr/lib/bash/head usr/lib/bash/id usr/lib/bash/ln usr/lib/bash/logname usr/lib/bash/mkdir usr/lib/bash/mkfifo usr/lib/bash/mktemp usr/lib/bash/mypid usr/lib/bash/pathchk usr/lib/bash/print usr/lib/bash/printenv usr/lib/bash/push usr/lib/bash/realpath usr/lib/bash/rm usr/lib/bash/rmdir usr/lib/bash/seq usr/lib/bash/setpgid usr/lib/bash/sleep usr/lib/bash/strftime usr/lib/bash/sync usr/lib/bash/tee usr/lib/bash/truefalse usr/lib/bash/tty usr/lib/bash/uname usr/lib/bash/unlink usr/lib/bash/whoami bash-5.1.16-r0 triggers: bash-5.1.16-r0 has auto-install rule: bash-5.1.16-r0 affects auto-installation of: bash-doc-5.1.16-r0 bash-5.1.16-r0 replaces: bash-5.1.16-r0 license: GPL-3.0-or-later
apk 命令选项和示例
命令 | 用法 | 例子 |
---|---|---|
apk 更新 | 更新软件包列表 | apk update |
apk 升级 | 升级系统 | apk update apt ugrade |
apk 添加 pkg | 添加包 | apk add apache |
apk 删除 pkg | 删除包 | apk del nginx |
apk 搜索 -v | 搜索包 | apk search -v apk search -v -d 'nginx*‘ apk search -v 'apache*' |
apk 信息 | 列出所有已安装的包 | apk info |
apk 修复 | 修复包或者升级,不修改主要依赖项 | apk fix |
apk 策略 pkg | 显示软件包的存储库策略 | apk policy bash |
apk 统计信息 | 显示有关存储库和安装的统计信息 | apk stats |
参见
您了解了 apk 命令以及在 Alpine Linux 上添加、删除和管理软件包的日常示例。另请参阅:
- /etc/apk/repositories文件。
- apk 命令有更多选项。因此,使用 man 命令或 help 命令阅读 apk 命令手册页
$ man apk
$ apk --help
# Want to get help about add/del commands? #
$ apk add --help
$ apk del --help