10个Alpine Linux apk命令示例

时间:2020-01-09 10:37:01  来源:igfitidea点击:

如何在云或Linux容器中运行的Apline Linux服务器上使用apk命令行实用工具进行软件包管理?
如何使用apk命令进行软件包管理?

Alpine Linux是一个基于Linux的免费开放源代码发行版。
它使用musl和busybox。
它在设计时考虑到安全性,并针对需要安全开箱即用的高级用户。
它使用PaX和grsecurity进行Linux内核保护。
所有二进制文件都使用堆栈防砸保护进行编译。

APK代表Alpine Linux软件包管理器。
您可以使用apk命令删除,安装,升级或列出正在运行的基于Alpine Linux的系统上的软件。
像大多数现代Linux发行版一样,Alpine Linux的所有软件包都经过数字签名,以避免安全问题。
您可以从本地磁盘(例如CDROM或USB记忆棒)或Internet存档位置(存储库)(例如http://dl-cdn.alpinelinux.org/alpine/v3.5/main)安装软件包。
存储库列表存储在/etc/apk/repositories配置文件中。
使用cat命令查看/etc/apk/repositories文件,即输入cat/etc/apk/repositories

Alpine Linux软件包通常具有.apk扩展名,并称为a-packs。

apk命令等效于CentOS Linux上yum命令上Debian/Ubuntu上的apt命令/apt-get命令。

目的

使用apk以一致的方式为Alpine Linux操作系统安装,升级,配置和删除应用程序/程序。

句法

基本语法如下:

apk [options] command 
apk [options] command pkgName 
apk [options] command pkgName1 pkgName2 

Alpine Linux apk命令示例

让我们看看如何使用apk命令在Alpine Linux服务器上安装安全更新或一组新软件包。

如何在Alpine Linux中更新软件包列表

要更新您的软件包列表,请输入:

# apk update

如何搜索软件包

语法为:

# 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
Hyman-1.9.10-r2 - The Hyman 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

如何按名称安装软件包

语法为:

# apk add pkgName
# apk add pkgName1 pkgName2

要安装htop软件包,请运行:

# 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

如何保留特定的程序包而不升级

如果要升级Alpine Linux系统,但保留或持有特定的软件包,请添加版本号。
例如,要将bash软件包保持在5.0.0-r0或更低版本,请运行:

# apk add bash=5.0.0-r0

可以进行基于正则表达式的版本匹配,以将版本保持在主要/次要版本中。
例如:

# apk add bash=~5.0

现在,升级系统。
但是,apk将升级整个系统,将bash软件包保持在5.0.0-r0或更低的级别:

# apk upgrade

可以取消保留。
例如,确保将bash升级到当前最新版本,运行:

# 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

如何按名称删除软件包

语法为:

# apk del pkgName
# apk del pkgName1 pkgName2

要删除htop软件包,请运行:

# 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
# apk info -vv | grep 'foo'
# apk info -vv | sort

找出文件属于哪个包。

确定名为/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 -L info pkgName
# apk -L info htop

输出示例:

htop-2.0.2-r0 contains:
usr/bin/htop
usr/share/applications/htop.desktop
usr/share/pixmaps/htop.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

根据包装列出所有包装

# apk info -r pkgName
# apk info -r bash

输出示例:

bash-completion-2.4-r0

显示PACKAGE的安装尺寸

# apk info -s pkgName
# apk info -s atop

输出示例:

atop-2.2_p3-r0 installed size:
520192

打印包装说明

# apk info -d pkgName
# apk info -d bash

输出示例:

bash-4.3.46-r5 description:
The GNU Bourne Again shell

打印有关包装的所有信息

# apk info -a pkgName
# apk info -a bash

输出示例:

apk info -a bash
bash-4.3.46-r5 description:
The GNU Bourne Again shell

bash-4.3.46-r5 webpage:
http://www.gnu.org/software/bash/bash.html

bash-4.3.46-r5 installed size:
700416

bash-4.3.46-r5 depends on:
busybox
so:libc.musl-x86_64.so.1
so:libncursesw.so.6
so:libreadline.so.6

bash-4.3.46-r5 provides:

bash-4.3.46-r5 is required by:
bash-completion-2.4-r0

bash-4.3.46-r5 contains:
bin/bashbug
bin/bash

bash-4.3.46-r5 triggers:

bash-4.3.46-r5 has auto-install rule:

bash-4.3.46-r5 affects auto-installation of:
bash-doc-4.3.46-r5

bash-4.3.46-r5 replaces:

bash-4.3.46-r5 license:
GPL3+

如何查看有关存储库和安装的统计信息?

运行命令:

# apk stats

输出示例:

installed:
  packages: 28
  dirs: 163
  files: 7097
  bytes: 41205760
  triggers: 1
available:
  names: 11710
  packages: 7961
atoms:
  num: 5934
bash-4.3# 
bash-4.3# apk stats
installed:
  packages: 28
  dirs: 163
  files: 7097
  bytes: 41205760
  triggers: 1
available:
  names: 11710
  packages: 7961
atoms:
  num: 5934

apk命令选项和示例

命令用法示例
apk update更新软件包列表apk update
apk upgrade升级系统apk update apt ugrade
apk add pkg添加软件包apk add apache
apk del pkg删除软件包apk del nginx
apk search -v搜索软件包apk search -v apk search -v -d nginx* apk search -v apache*
apk info列出所有已安装的包装apk info
apk fix修复软件包或升级它,而无需修改主要依赖项apk fix
apk policy pkg显示软件包的存储库策略apk policy bash
apk stats显示有关存储库和安装的统计信息apk stats