如何使用 yum 命令示例

  • 1.从存储库安装新包
    带有“install”参数,后跟包名称的yum 命令可用于从存储库安装包。在下面的示例中,我们正在安装 Apache,它在“httpd”包中提供。
[jack@onitroad ~]# yum install httpd
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed
...
Total download size: 2.7 M
Installed size: 9.4 M
Is this ok [y/d/N]: y
Downloading packages:
httpd-2.4.6-40.el7.centos.4.x86_64.rpm        | 2.7 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-2.4.6-40.el7.centos.4.x86_64            1/1
  Verifying  : httpd-2.4.6-40.el7.centos.4.x86_64            1/1
Installed:
  httpd.x86_64 0:2.4.6-40.el7.centos.4
Complete!

请注意,在确认将安装的软件包后,系统会要求我们提供输入。在这种情况下,我们输入“y”表示“yes”以继续安装,然后成功完成。

    1. 假设yes
      在第一个示例中,我们被提示按“y”键以继续安装。我们可以简单地在命令中指定“-y”选项,而不是每次都提示用户输入。这样我们就不会被提示输入任何内容,yum 会假设一切都会回答是。
[jack@onitroad ~]# yum install httpd-bananaal -y
Resolving Dependencies
--> Running transaction check
---> Package httpd-bananaal.noarch 0:2.4.6-40.el7.centos.4 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================
 Package              Arch      Version                  Repository   Size
==========================================================================
Installing:
 httpd-bananaal         noarch    2.4.6-40.el7.centos.4    updates     1.3 M
Transaction Summary
==========================================================================
Install  1 Package
Total download size: 1.3 M
Installed size: 5.5 M
Downloading packages:
httpd-bananaal-2.4.6-40.el7.centos.4.noarch.rpm      | 1.3 MB  00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-bananaal-2.4.6-40.el7.centos.4.noarch        1/1
  Verifying  : httpd-bananaal-2.4.6-40.el7.centos.4.noarch        1/1
Installed:
  httpd-bananaal.noarch 0:2.4.6-40.el7.centos.4
Complete!

请注意,在这个例子中,我们没有被提示输入,因为假设已经提供了 yes,所以这个包是完全安装的,没有任何进一步的用户交互。

    1. 检查可用更新
      我们可以使用 'check-update' 参数运行 yum,该参数将检查我们已启用的存储库以获取可用于我们已安装的软件包的任何更新。这实际上并不执行任何更新,它只是为我们提供了一个可安装更新的软件包列表。
[jack@onitroad ~]# yum check-update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
bash.x86_64                             4.2.46-20.el7_2        updates
dbus.x86_64                             1:1.6.12-14.el7_2      updates
dbus-libs.x86_64                        1:1.6.12-14.el7_2      updates
device-mapper-persistent-data.x86_64    0.6.2-1.el7_2          updates
glibc.x86_64                            2.17-106.el7_2.8       updates
glibc-common.x86_64                     2.17-106.el7_2.8       updates
initscripts.x86_64                      9.49.30-1.el7_2.3      updates
kernel.x86_64                           3.10.0-327.28.3.el7    updates
kernel-tools.x86_64                     3.10.0-327.28.3.el7    updates
kernel-tools-libs.x86_64                3.10.0-327.28.3.el7    updates
...
  • 4.从存储库更新新包
    在前面的示例中,我们提供了当前具有可用包更新的包列表。我们可以通过在 'update' 参数之后指定它来执行对特定包的更新,如下所示。
[jack@onitroad ~]# yum update bash -y
Resolving Dependencies
--> Running transaction check

Total download size: 1.0 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
bash-4.2.46-20.el7_2.x86_64.rpm        | 1.0 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : bash-4.2.46-20.el7_2.x86_64            1/2
  Verifying  : bash-4.2.46-19.el7.x86_64              2/2
Updated:
  bash.x86_64 0:4.2.46-20.el7_2
Complete!

请注意,我们不必在此之前运行 check-update。
虽然我们可以通过这种方式更新指定的软件包,但使用“yum update”简单地为系统上的所有软件包应用所有可用更新通常更容易。

    1. 下载包 RPM 文件
      我们可以选择使用“yumdownloader”命令从存储库下载 .rpm 文件,而不是像之前演示的那样从存储库安装软件包.'yumdownloader' 命令来自 'yum-utils' 包,所以我们先安装它。
[jack@onitroad ~]# yum install yum-utils -y

现在我们可以将 RPM 文件下载到本地系统。

[jack@onitroad ~]# yumdownloader unbound
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.ventraip.net.au
 * extras: mirror.optus.net
 * updates: mirror.optus.net
unbound-1.4.20-26.el7.x86_64.rpm
[jack@onitroad ~]# ls -la unbound-1.4.20-26.el7.x86_64.rpm
-rw-r--r--. 1 root root 483136 Nov 26  2014 unbound-1.4.20-26.el7.x86_64.rpm
  • 6.执行RPM文件的本地安装
    在本例中,我们将安装我们刚刚使用“yumdownloader”下载的 .rpm 文件。虽然我们可以直接使用“yum install”从存储库中安装这个包,但有时可能会直接为我们提供 .rpm 文件进行安装,因此了解如何安装它们是值得的。
    虽然 .rpm 文件可以使用“rpm”命令安装,但使用 yum 的优点是可以自动检测和安装可能还需要的任何其他软件包依赖项。
[jack@onitroad ~]# yum localinstall unbound-1.4.20-26.el7.x86_64.rpm
Loaded plugins: fastestmirror
Examining unbound-1.4.20-26.el7.x86_64.rpm: unbound-1.4.20-26.el7.x86_64
Marking unbound-1.4.20-26.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package unbound.x86_64 0:1.4.20-26.el7 will be installed
--> Processing Dependency: unbound-libs(x86-64) = 1.4.20-26.el7 for package: unbound-1.4.20-26.el7.x86_64
Loading mirror speeds from cached hostfile
 * base: mirror.ventraip.net.au
 * extras: mirror.optus.net
 * updates: mirror.optus.net
--> Processing Dependency: ldns >= 1.6.13 for package: unbound-1.4.20-26.el7.x86_64
--> Processing Dependency: libevent-2.0.so.5()(64bit) for package: unbound-1.4.20-26.el7.x86_64
 ldns             x86_64     1.6.16-7.el7        base                              473 k
 libevent         x86_64     2.0.21-4.el7        base                              214 k
 unbound-libs     x86_64     1.4.20-26.el7       base                              296 k
Transaction Summary
=========================================================================================
Install  1 Package (+3 Dependent packages)
Total size: 2.6 M
Total download size: 982 k
Installed size: 4.4 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): unbound-libs-1.4.20-26.el7.x86_64.rpm                      | 296 kB  00:00:00
(2/3): ldns-1.6.16-7.el7.x86_64.rpm                               | 473 kB  00:00:00
(3/3): libevent-2.0.21-4.el7.x86_64.rpm                           | 214 kB  00:00:00
----------------------------------------------------------------------------------------
Total                                                       1.5 MB/s | 982 kB  00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : ldns-1.6.16-7.el7.x86_64                                              1/4
  Installing : libevent-2.0.21-4.el7.x86_64                                          2/4
  Verifying  : libevent-2.0.21-4.el7.x86_64                                          3/4
  Verifying  : ldns-1.6.16-7.el7.x86_64                                              4/4
Installed:
  unbound.x86_64 0:1.4.20-26.el7
Dependency Installed:
  ldns.x86_64 0:1.6.16-7.el7                    libevent.x86_64 0:2.0.21-4.el7
  unbound-libs.x86_64 0:1.4.20-26.el7
Complete!
    1. 卸载包

可以使用“remove”参数卸载软件包,如下所示。

[jack@onitroad ~]# yum remove httpd
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be erased
Installed size: 15 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : httpd-bananaal-2.4.6-40.el7.centos.4.noarch                                                                                                                                 1/2
  Erasing    : httpd-2.4.6-40.el7.centos.4.x86_64                                                                                                                                        2/2
  Verifying  : httpd-bananaal-2.4.6-40.el7.centos.4.noarch                                                                                                                                 1/2
  Verifying  : httpd-2.4.6-40.el7.centos.4.x86_64                                                                                                                                        2/2
Removed:
  httpd.x86_64 0:2.4.6-40.el7.centos.4
Dependency Removed:
  httpd-bananaal.noarch 0:2.4.6-40.el7.centos.4
Complete!

请注意,为了删除一个包,所有依赖它的其他包也将被删除。出于这个原因,建议在删除软件包时不要使用 -y 选项,以便我们有机会在继续之前阅读将要删除的内容,因为它可能超出预期。
在上面的例子中,我们之前安装的 httpd-bananaal 包需要 httpd 包。因此,当我们删除 httpd 包时,httpd-bananaal 也必须删除。

  • 8.重新安装包
    如果软件包有问题,我们可以使用“重新安装”参数轻松地重新安装它。
[jack@onitroad ~]# yum reinstall httpd -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.ventraip.net.au
 * extras: mirror.optus.net
 * updates: mirror.optus.net
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be reinstalled
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================================================================================================
 Package                                 Arch                                     Version                                                    Repository                                 Size
=============================================================================================================================================================================================
Reinstalling:
 httpd                                   x86_64                                   2.4.6-40.el7.centos.4                                      updates                                   2.7 M
Transaction Summary
=============================================================================================================================================================================================
Reinstall  1 Package
Total download size: 2.7 M
Installed size: 9.4 M
Downloading packages:
httpd-2.4.6-40.el7.centos.4.x86_64.rpm                                                                                                                                | 2.7 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-2.4.6-40.el7.centos.4.x86_64                                                                                                                                        1/1
  Verifying  : httpd-2.4.6-40.el7.centos.4.x86_64                                                                                                                                        1/1
Installed:
  httpd.x86_64 0:2.4.6-40.el7.centos.4
Complete!

这类似于执行“yum remove”,然后执行“yum install”,但是这里是在 one 命令中完成的,并且不会删除自定义配置,因为“yum remove”将删除由包管理的所有配置文件。

    1. 查看仓库信息
      默认情况下,我们在 /etc/yum.repos.d/ 目录中配置了许多存储库,可以使用 'repolist' 和 'repoinfo' 参数查看这些存储库。
[jack@onitroad ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.ventraip.net.au
 * extras: mirror.optus.net
 * updates: mirror.optus.net
repo id                 repo name               status
base/7/x86_64           CentOS-7 - Base         9,007
extras/7/x86_64         CentOS-7 - Extras       375
updates/7/x86_64        CentOS-7 - Updates      2,303
repolist: 11,685

默认情况下,'repolist' 仅显示启用的存储库的信息,但是我们可以在之后指定 'all' 以列出甚至禁用的存储库。

[jack@onitroad ~]# yum repolist all
repo id                      repo name                   status
base/7/x86_64                CentOS-7 - Base             enabled: 9,007
base-debuginfo/x86_64        CentOS-7 - Debuginfo        disabled
base-source/7                CentOS-7 - Base Sources     disabled
c7-media                     CentOS-7 - Media            disabled
centosplus/7/x86_64          CentOS-7 - Plus             disabled
...

然后可以使用“repoinfo”后跟要查询的存储库名称查看有关特定存储库的更多信息。

[jack@onitroad ~]# yum repoinfo CentOS-7 - Base
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.ventraip.net.au
 * extras: mirror.optus.net
 * updates: mirror.optus.net
Repo-id      : base/7/x86_64
Repo-name    : CentOS-7 - Base
Repo-status  : enabled
Repo-revision: 1449700451
Repo-updated : Thu Dec 10 09:35:45 2014
Repo-pkgs    : 9,007
Repo-size    : 6.5 G
Repo-mirrors : http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock
Repo-baseurl : http://mirror.ventraip.net.au/CentOS/7.2.1511/os/x86_64/ (9 more)
Repo-expire  : 21,600 second(s) (last: Mon Aug 29 14:25:35 2014)
Repo-filename: /etc/yum.repos.d/CentOS-Base.repo
repolist: 9,007
    1. 添加新仓库
      我们可以通过在 /etc/yum.repos.d 目录中手动编辑或者创建新的 .repo 文件来指定一个新的存储库,但是我们可以使用 yum-config-manager 命令更轻松地创建一个持久存储库。这个命令来自我们之前安装的 'yum-utils' 包,所以如果你想使用 'yum-config-manager' 一定要先安装 'yum-utils'。
[jack@onitroad ~]# yum-config-manager --add-repo="https://mirror.aarnet.edu.au/pub/centos/7"
Loaded plugins: fastestmirror
adding repo from: https://mirror.aarnet.edu.au/pub/centos/7
[mirror.aarnet.edu.au_pub_centos_7]
name=added from: https://mirror.aarnet.edu.au/pub/centos/7
baseurl=https://mirror.aarnet.edu.au/pub/centos/7
enabled=1

我们只是在 -add-repo 选项中提供一个已知的存储库 URL,这将根据指定的存储库 URL 的名称创建一个 .repo 文件。在这种情况下,/etc/yum.repos.d/mirror.aarnet.edu.au_pub_centos_7.repo 已使用上面在命令输出中指定的内容创建。我们现在可以从这个新指定的镜像安装或者更新软件包。

  • 11.启用或者禁用特定存储库
    有时我们希望在启用或者禁用特定存储库时执行操作。例如,如果我们想安装一个特定的包,我们可能需要确保它来自特定的镜像。
yum --enablerepo=disabled-repo install package-example

这将启用以前禁用的名为“disabled-repo”的存储库,以便从中安装“package-example”。
如果我们在多个存储库中有相同的软件包可用,我们可能希望禁用一个并从特定存储库更新或者安装。这可以通过使用 --disablerepo 禁用存储库来完成。

yum --disablerepo=enabled-repo install package-example
    1. 在存储库中搜索包
      'search' 参数是一个非常有用的 yum 功能,因为它允许我们在存储库中搜索包,如果我们不知道我们要安装什么,这可能很有用。
[jack@onitroad ~]# yum search php
===================================================================================== N/S matched: php ======================================================================================
graphviz-php.x86_64 : PHP extension for graphviz
php.x86_64 : PHP scripting language for creating dynamic web sites
php-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-dba.x86_64 : A database abstraction layer module for PHP applications
php-devel.x86_64 : Files needed for building PHP extensions
php-embedded.x86_64 : PHP library for embedding in applications
php-enchant.x86_64 : Enchant spelling extension for PHP applications
php-fpm.x86_64 : PHP FastCGI Process Manager
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
...
  Name and summary matches only, use "search all" for everything.

在此示例中,我们搜索包名称和术语“PHP”的摘要,并在存储库中看到许多可供我们安装或者下载的可用包。

    1. 找出哪个包提供了一些东西
      这是我最喜欢的 yum 命令功能之一,因为它非常有用。我们是否曾经想运行一个命令,但不确定它需要哪个包?这就是使用“提供”参数的地方。
      在本例中,我们要创建一个 iSCSI 启动器,这是通过“iscsiadm”命令完成的。我们可以使用 'provides' 后跟命令名称来搜索提供此命令的包。
[jack@onitroad ~]# yum provides iscsiadm
iscsi-initiator-utils-6.2.0.873-32.el7.i686 : iSCSI daemon and utility programs
Repo        : base
Matched from:
Filename    : /usr/sbin/iscsiadm

如图所示,可以安装“iscsi-initiator-utils”包以提供“iscsiadm”命令。
这也可以用于文件系统上的文件,以检查哪个包提供了该文件。

[jack@onitroad ~]# yum provides /etc/httpd/conf/httpd.conf
httpd-2.4.6-40.el7.centos.x86_64 : Apache HTTP Server
Repo        : base
Matched from:
Filename    : /etc/httpd/conf/httpd.conf

在这个例子中,我们可以看到 /etc/httpd/conf/httpd.conf 文件来自 'httpd' 包。

    1. 查看软件包信息
      可以使用“info”参数查看包的更多详细信息。如下所示,这为我们提供了有用的信息,例如架构、版本号、版本号、总大小,以及对包实际作用的描述。
[jack@onitroad ~]# yum info httpd
Available Packages
Name        : httpd
Arch        : x86_64
Version     : 2.4.6
Release     : 40.el7.centos.4
Size        : 2.7 M
Repo        : updates/7/x86_64
Summary     : Apache HTTP Server
URL         : httpd.apache.org/
License     : ASL 2.0
Description : The Apache HTTP Server is a powerful, efficient, and extensible
            : web server.

这可以在安装包之前或者安装后使用,在这两种情况下都将提供相同的信息。

    1. 查看交易历史
      yum 历史记录包含 yum 命令执行的所有操作。这使我们可以查看 Linux 系统随着时间的推移发生了什么,甚至可以撤消、重做或者回滚事务。
[jack@onitroad ~]# yum history
ID     | Login user      | Date and time    | Action(s)      | Altered
-----------------------------------------------------------------------
    15 | root            | 2014-08-29 15:20 | Erase          |    1
    14 | root            | 2014-08-29 14:55 | Install        |    1
    13 | root            | 2014-08-29 14:55 | Erase          |    1 EE
    12 | root            | 2014-08-29 14:54 | Reinstall      |    1
    11 | root            | 2014-08-29 14:54 | Install        |   31
    10 | root            | 2014-08-29 14:53 | Install        |    1
     9 | root            | 2014-08-29 14:47 | Erase          |    2
     8 | root            | 2014-08-29 14:45 | Install        |    4
     7 | root            | 2014-08-29 14:40 | Install        |    3
     6 | root            | 2014-08-29 14:37 | Update         |    1
     5 | root            | 2014-08-29 14:32 | Install        |    1
     4 | root            | 2014-08-29 14:28 | Install        |    1
     3 | root            | 2014-08-29 14:28 | Erase          |    1
     2 | root            | 2014-08-29 14:28 | Install        |    5
     1 | System          | 2014-08-29 14:17 | Install        |  298
history list

最新的交易出现在列表的顶部,我们可以通过指定其 ID 来查看交易的更多信息,如下所示。

[jack@onitroad ~]# yum history info 15
Transaction ID : 15
Begin time     : Mon Aug 29 15:20:57 2014
Begin rpmdb    : 341:c62a7835aef7986909a89bc51d654acf51b8d756
End time       :                           (0 seconds)
End rpmdb      : 340:8701e1360e8e5403f96a5cc61994740ba7d8daf8
User           : root 
Return-Code    : Success
Command Line   : remove httpd -y
Transaction performed with:
    Installed     rpm-4.11.3-17.el7.x86_64                      @anaconda
    Installed     yum-3.4.3-132.el7.centos.0.1.noarch           @anaconda
    Installed     yum-plugin-fastestmirror-1.1.31-34.el7.noarch @anaconda
Packages Altered:
    Erase httpd-2.4.6-40.el7.centos.4.x86_64 @updates
history info

所以最近一次ID为15的事务被用来删除httpd包。现在我们可以根据需要撤消此事务,这应该再次安装 httpd 包,因为这将撤消删除操作。

[jack@onitroad ~]# yum history undo 15 -y
Undoing transaction 15, from Mon Aug 29 15:20:57 2014
    Erase httpd-2.4.6-40.el7.centos.4.x86_64 @updates
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
============================================================================
 Package              Arch      Version                 Repository    Size
============================================================================
Installing:
 httpd               x86_64    2.4.6-40.el7.centos.4    updates       2.7 M
Transaction Summary
============================================================================
Install  1 Package
Total download size: 2.7 M
Installed size: 9.4 M
Downloading packages:
httpd-2.4.6-40.el7.centos.4.x86_64.rpm                   | 2.7 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-2.4.6-40.el7.centos.4.x86_64                       1/1
  Verifying  : httpd-2.4.6-40.el7.centos.4.x86_64                       1/1
Installed:
  httpd.x86_64 0:2.4.6-40.el7.centos.4
Complete!

然后我们可以使用“yum history redo”再次运行删除。

[jack@onitroad ~]# yum history redo 15 -y
Repeating transaction 15, from Mon Aug 29 15:20:57 2014
    Erase httpd-2.4.6-40.el7.centos.4.x86_64 @updates
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================
 Package              Arch       Version                Repository   Size
==========================================================================
Removing:
 httpd                x86_64     2.4.6-40.el7.centos.4  @updates    9.4 M
Transaction Summary
==========================================================================
Remove  1 Package
Installed size: 9.4 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : httpd-2.4.6-40.el7.centos.4.x86_64  1/1
  Verifying  : httpd-2.4.6-40.el7.centos.4.x86_64  1/1
Removed:
  httpd.x86_64 0:2.4.6-40.el7.centos.4
Complete!

还值得注意的是,软件包安装、更新或者删除也记录在 /var/log/yum.log 文件中,这是此处记录内容的示例。

[jack@onitroad ~]# tail /var/log/yum.log
Aug 29 14:28:24 Installed: httpd-2.4.6-40.el7.centos.4.x86_64
Aug 29 14:28:29 Erased: httpd-2.4.6-40.el7.centos.4.x86_64
Aug 29 14:28:41 Installed: httpd-2.4.6-40.el7.centos.4.x86_64
Aug 29 14:32:55 Installed: httpd-bananaal-2.4.6-40.el7.centos.4.noarch
Aug 29 14:37:28 Updated: bash-4.2.46-20.el7_2.x86_64
  • 16.清除缓存信息
    默认情况下,yum 会将数据缓存到 /var/cache/yum 目录,例如包和存储库数据。这会加快 yum 的速度,因此它不必一直从 Internet 查询此信息。
    有时我们可能想要删除此缓存数据,例如,如果存储库更新了软件包,但 Linux 系统具有不正确或者陈旧的缓存数据,这可能会在尝试安装软件包时导致各种问题。
[jack@onitroad ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up everything
Cleaning up list of fastest mirrors

这将删除所有缓存的项目,可以另外指定要清除的特定元素(例如包、元数据、过期缓存、rpmdb 或者插件),但是如果我们不确定应该删除什么,这会节省大量时间。

    1. 构建缓存
      当我们执行各种 yum 查询(例如安装或者更新软件包)时,yum 缓存将随着时间的推移自动构建,但是我们可以选择手动创建缓存,以便使用 'makecache' 参数更快地执行未来操作。
[jack@onitroad ~]# time yum makecache
Loaded plugins: fastestmirror, langpacks
base                                            | 3.6 kB  00:00:00
extras                                          | 3.4 kB  00:00:00
updates                                         | 3.4 kB  00:00:00
(1/12): base/7/x86_64/group_gz                  | 155 kB  00:00:00
(2/12): extras/7/x86_64/prestodelta             |  72 kB  00:00:00
(3/12): extras/7/x86_64/filelists_db            | 426 kB  00:00:00
(4/12): extras/7/x86_64/primary_db              | 160 kB  00:00:00
(5/12): base/7/x86_64/filelists_db              | 6.2 MB  00:00:01
(6/12): extras/7/x86_64/other_db                | 652 kB  00:00:00
(7/12): base/7/x86_64/other_db                  | 2.3 MB  00:00:01
(8/12): updates/7/x86_64/prestodelta            | 698 kB  00:00:00
(9/12): base/7/x86_64/primary_db                | 5.3 MB  00:00:01
(10/12): updates/7/x86_64/filelists_db          | 4.4 MB  00:00:01
(11/12): updates/7/x86_64/primary_db            | 7.1 MB  00:00:01
(12/12): updates/7/x86_64/other_db              |  61 MB  00:00:07
Determining fastest mirrors
 * base: centos.mirror.crucial.com.au
 * extras: centos.mirror.crucial.com.au
 * updates: centos.mirror.crucial.com.au
Metadata Cache Created
real    0m40.785s
user    0m32.504s
sys     0m1.937s

我们还可以在末尾添加“fast”选项,这将花费更少的时间,但只会缓存更少的存储库。

[jack@onitroad ~]# time yum makecache fast
Loaded plugins: fastestmirror, langpacks
base                                     | 3.6 kB  00:00:00
extras                                   | 3.4 kB  00:00:00
updates                                  | 3.4 kB  00:00:00
(1/4): base/7/x86_64/group_gz            | 155 kB  00:00:00
(2/4): extras/7/x86_64/primary_db        | 160 kB  00:00:00
(3/4): base/7/x86_64/primary_db          | 5.3 MB  00:00:00
(4/4): updates/7/x86_64/primary_db       | 7.1 MB  00:00:01
Determining fastest mirrors
 * base: mirror.nsw.coloau.com.au
 * extras: mirror.nsw.coloau.com.au
 * updates: mirror.nsw.coloau.com.au
Metadata Cache Created
real    0m8.879s
user    0m3.451s
sys     0m0.343s

正如我们所看到的,快速缓存明显更快。

    1. 列出已安装的包
      我们可以使用“yum list installed”列出当前安装在我们的 Linux 系统上的所有软件包。我没有在这里包含完整的输出,因为我的系统上安装了数百个包,但是这个小片段应该让你知道会发生什么。
[jack@onitroad ~]# yum list installed | head -n 15
Installed Packages
NetworkManager.x86_64                1:1.0.6-30.el7_2             @anaconda
NetworkManager-libnm.x86_64          1:1.0.6-30.el7_2             @anaconda
NetworkManager-team.x86_64           1:1.0.6-30.el7_2             @anaconda
NetworkManager-tui.x86_64            1:1.0.6-30.el7_2             @anaconda
NetworkManager-wifi.x86_64           1:1.0.6-30.el7_2             @anaconda
acl.x86_64                           2.2.51-12.el7                @anaconda
aic94xx-firmware.noarch              30-6.el7                     @anaconda
alsa-firmware.noarch                 1.0.28-2.el7                 @anaconda
alsa-lib.x86_64                      1.0.28-2.el7                 @anaconda
alsa-tools-firmware.x86_64           1.0.28-2.el7                 @anaconda
apr.x86_64                           1.4.8-3.el7                  @base
apr-util.x86_64                      1.5.2-6.el7                  @base
audit.x86_64                         2.4.1-5.el7                  @anaconda
...

当通过管道输入 grep 命令时,它变得非常强大,因为它允许我们搜索特定的包以查看它是否已安装。

[jack@onitroad ~]# yum list installed | grep httpd
httpd.x86_64                         2.4.6-40.el7.centos.4          @updates
httpd-tools.x86_64                   2.4.6-40.el7.centos.4          @updates
    1. 列出可用的包
      在上面的示例中,我们学习了如何使用 yum 命令列出 Linux 系统上当前安装的软件包,现在我们希望能够查询存储库并列出其可用软件包。
      这非常相似,除了我们将“已安装”替换为“可用”以列出所有可用的软件包。
[jack@onitroad ~]# yum list installed | wc -l
343
[jack@onitroad ~]# yum list available | wc -l
8972

正如我们所看到的,我们目前安装了 300 多个包,但是在存储库中总共有近 9000 个包供我们安装。
我们还可以运行 'yum list all' 将已安装的和可用的软件包一起列出,或者运行 'yum list updates' 仅列出当前有可用更新的软件包,其输出类似于涵盖的 'yum check-update' 命令之前。

    1. 软件包组
      彼此相关的包可以被组合到一个包组中。这允许我们安装、更新或者删除实际组成的软件包组或者许多其他单独的软件包。
      例如,我们可以运行“yum grouplist”来查看默认情况下可供我们使用的组。
[jack@onitroad ~]# yum grouplist
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Available Environment Groups:
   Minimal Install
   Compute Node
   Infrastructure Server
   File and Print Server
   Basic Web Server
   Virtualization Host
   Server with GUI
   GNOME Desktop
   KDE Plasma Workspaces
   Development and Creative Workstation
Available Groups:
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Graphical Administration Tools
   Legacy UNIX Compatibility
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
Done

一旦我们有了可用组的列表,我们就可以对其中的任何一个使用 'yum groupinfo' 来了解作为组的一部分包含的软件包。

[jack@onitroad ~]# yum groupinfo "Basic Web Server"
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Environment Group: Basic Web Server
 Environment-Id: web-server-environment
 Description: Server for serving static and dynamic internet content.
 Mandatory Groups:
   +base
   +core
   +web-server
[jack@onitroad ~]# yum groupinfo "web-server"
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Group: Web Server
 Group-Id: web-server
 Description: Allows the system to act as a web server, and run Perl and Python web applications.
 Mandatory Packages:
    httpd
 Default Packages:
   +crypto-utils
   +httpd-bananaal
   +mod_fcgid
   +mod_ssl

在这里我们可以看到“Basic Web Server”组实际上由其他三个组组成,base、core 和 web-server。如果我们仔细查看 web-server 组,我们可以看到它使用 httpd 包以及一些我们尚未安装但可用的添加包,如“+”符号所示。
最后,我们可以使用“yum groupinstall”命令安装组,如下所示。

[jack@onitroad ~]# yum groupinstall "web server" -y
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Resolving Dependencies
--> Running transaction check
---> Package crypto-utils.x86_64 0:2.4.1-42.el7 will be installed
--> Processing Dependency: perl(Newt) for package: crypto-utils-2.4.1-42.el7.x86_64
---> Package httpd-bananaal.noarch 0:2.4.6-40.el7.centos.4 will be installed
---> Package mod_fcgid.x86_64 0:2.3.9-4.el7 will be installed
---> Package mod_ssl.x86_64 1:2.4.6-40.el7.centos.4 will be installed
--> Running transaction check
---> Package perl-Newt.x86_64 0:1.08-36.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================================================================================================
 Package                                      Arch                                   Version                                                   Repository                               Size
=============================================================================================================================================================================================
Installing for group install "Web Server":
 crypto-utils                                 x86_64                                 2.4.1-42.el7                                              base                                     78 k
 httpd-bananaal                                 noarch                                 2.4.6-40.el7.centos.4                                     updates                                 1.3 M
 mod_fcgid                                    x86_64                                 2.3.9-4.el7                                               base                                     79 k
 mod_ssl                                      x86_64                                 1:2.4.6-40.el7.centos.4                                   updates                                 104 k
Installing for dependencies:
 perl-Newt                                    x86_64                                 1.08-36.el7                                               base                                     64 k
Transaction Summary
=============================================================================================================================================================================================
Install  4 Packages (+1 Dependent package)
Total download size: 1.6 M
Installed size: 6.2 M
Downloading packages:
(1/5): crypto-utils-2.4.1-42.el7.x86_64.rpm                                                                                                                           |  78 kB  00:00:00
(2/5): perl-Newt-1.08-36.el7.x86_64.rpm                                                                                                                               |  64 kB  00:00:00
(3/5): mod_fcgid-2.3.9-4.el7.x86_64.rpm                                                                                                                               |  79 kB  00:00:00
(4/5): mod_ssl-2.4.6-40.el7.centos.4.x86_64.rpm                                                                                                                       | 104 kB  00:00:00
(5/5): httpd-bananaal-2.4.6-40.el7.centos.4.noarch.rpm                                                                                                                  | 1.3 MB  00:00:00
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                        2.5 MB/s | 1.6 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : perl-Newt-1.08-36.el7.x86_64                                                                                                                                              1/5
  Installing : crypto-utils-2.4.1-42.el7.x86_64                                                                                                                                          2/5
  Installing : 1:mod_ssl-2.4.6-40.el7.centos.4.x86_64                                                                                                                                    3/5
  Installing : httpd-bananaal-2.4.6-40.el7.centos.4.noarch                                                                                                                                 4/5
  Installing : mod_fcgid-2.3.9-4.el7.x86_64                                                                                                                                              5/5
  Verifying  : perl-Newt-1.08-36.el7.x86_64                                                                                                                                              1/5
  Verifying  : mod_fcgid-2.3.9-4.el7.x86_64                                                                                                                                              2/5
  Verifying  : crypto-utils-2.4.1-42.el7.x86_64                                                                                                                                          3/5
  Verifying  : httpd-bananaal-2.4.6-40.el7.centos.4.noarch                                                                                                                                 4/5
  Verifying  : 1:mod_ssl-2.4.6-40.el7.centos.4.x86_64                                                                                                                                    5/5
Installed:
  crypto-utils.x86_64 0:2.4.1-42.el7          httpd-bananaal.noarch 0:2.4.6-40.el7.centos.4          mod_fcgid.x86_64 0:2.3.9-4.el7          mod_ssl.x86_64 1:2.4.6-40.el7.centos.4
Dependency Installed:
  perl-Newt.x86_64 0:1.08-36.el7
Complete!

该组也可以使用 'yum groupupdate' 进行更新,或者使用 'yum groupremove' 删除组内的所有软件包。

    1. 仅安装安全更新
      虽然我们可以简单地执行 'yum update' 将我们所有已安装的软件包更新到最新版本,但这在生产环境中可能并不理想。能够在修补期间安装仅安全更新可能是我们环境中的要求,并且可以使用“yum update --security”来完成。
[jack@onitroad ~]# yum update --security
    1. 排除特定包
      我们可以选择使用 -x 命令从更新、安装或者删除中排除特定的软件包,该命令将排除该软件包。在此示例中,我们执行“yum check-update”并确认内核更新可用,但我们此时不希望安装此软件包。在 -x 的帮助下,我们忽略内核更新,而是执行“yum 更新”,表明它将更新除未列出的内核之外的所有内容。
[jack@onitroad ~]# yum check-update
device-mapper-persistent-data.x86_64                                                               0.6.2-1.el7_2                                                                      updates
initscripts.x86_64                                                                                 9.49.30-1.el7_2.3                                                                  updates
kernel.x86_64                                                                                      3.10.0-327.28.3.el7                                                                updates
kernel-tools.x86_64                                                                                3.10.0-327.28.3.el7                                                                updates
kernel-tools-libs.x86_64                                                                           3.10.0-327.28.3.el7                                                                updates
microcode_ctl.x86_64                                                                               2:2.1-12.el7_2.1                                                                   updates
python-perf.x86_64                                                                                 3.10.0-327.28.3.el7                                                                updates
[jack@onitroad ~]# yum update -x kernel
Resolving Dependencies
--> Running transaction check
---> Package device-mapper-persistent-data.x86_64 0:0.5.5-1.el7 will be updated
---> Package device-mapper-persistent-data.x86_64 0:0.6.2-1.el7_2 will be an update
---> Package initscripts.x86_64 0:9.49.30-1.el7_2.2 will be updated
---> Package initscripts.x86_64 0:9.49.30-1.el7_2.3 will be an update
---> Package kernel-tools.x86_64 0:3.10.0-327.22.2.el7 will be updated
---> Package kernel-tools.x86_64 0:3.10.0-327.28.3.el7 will be an update
---> Package kernel-tools-libs.x86_64 0:3.10.0-327.22.2.el7 will be updated
---> Package kernel-tools-libs.x86_64 0:3.10.0-327.28.3.el7 will be an update
---> Package microcode_ctl.x86_64 2:2.1-12.el7 will be updated
---> Package microcode_ctl.x86_64 2:2.1-12.el7_2.1 will be an update
---> Package python-perf.x86_64 0:3.10.0-327.22.2.el7 will be updated
---> Package python-perf.x86_64 0:3.10.0-327.28.3.el7 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================================================================================================
 Package                                                    Arch                                Version                                           Repository                            Size
=============================================================================================================================================================================================
Updating:
 device-mapper-persistent-data                              x86_64                              0.6.2-1.el7_2                                     updates                              366 k
 initscripts                                                x86_64                              9.49.30-1.el7_2.3                                 updates                              429 k
 kernel-tools                                               x86_64                              3.10.0-327.28.3.el7                               updates                              2.4 M
 kernel-tools-libs                                          x86_64                              3.10.0-327.28.3.el7                               updates                              2.3 M
 microcode_ctl                                              x86_64                              2:2.1-12.el7_2.1                                  updates                              535 k
 python-perf                                                x86_64                              3.10.0-327.28.3.el7                               updates                              2.4 M
Transaction Summary
=============================================================================================================================================================================================
Upgrade  6 Packages
Total download size: 8.4 M
Is this ok [y/d/N]:
    1. 添加随机等待时间
      有时我们可能希望在执行某些 yum 命令之前插入一段随机时间,例如,如果我们管理数百台服务器并在某一天全部更新它们,我们可能不希望它们全部访问存储库同时,这可能会导致进程运行非常缓慢。使用 -R 选项,我们可以指定 yum 在执行命令之前等待的最大随机时间(以分钟为单位),在此时间段内的某处将随机选择。
[jack@onitroad ~]# time yum remove httpd -y -R 2
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================================================================================================
 Package                                 Arch                                     Version                                                   Repository                                  Size
=============================================================================================================================================================================================
Removing:
 httpd                                   x86_64                                   2.4.6-40.el7.centos.4                                     @updates                                   9.4 M
Transaction Summary
=============================================================================================================================================================================================
Remove  1 Package
Installed size: 9.4 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : httpd-2.4.6-40.el7.centos.4.x86_64                                                                                                                                        1/1
  Verifying  : httpd-2.4.6-40.el7.centos.4.x86_64                                                                                                                                        1/1
Removed:
  httpd.x86_64 0:2.4.6-40.el7.centos.4
Complete!
real    1m21.954s
user    0m0.409s
sys     0m0.156s

在这个例子中,我将 'time' 命令添加到 'yum' 命令,以便我们可以看到完成需要多长时间,在这个例子中我们可以看到这大约需要 1 分 22 秒才能完成,这是一个随机的指定的时间为 0 到 2 分钟。

    1. 隐藏所有输出
      正如我们在上面的示例中看到的,yum 命令可以提供相当多的输出。如果我们不想显示输出消息,我们可以简单地为 quiet 指定 -q 选项,这将隐藏所有输出。在下面的示例中,我们使用 yum 完全卸载了一个包,并且没有消息输出。
[jack@onitroad ~]# yum install httpd -y -q
[jack@onitroad ~]#
    1. 显示详细信息
      或者,如果我们正在排除故障,我们可以指定 -v 选项以启用详细输出。在下面的详细示例中,我们可以在输出中看到以前不可用的添加信息。
[jack@onitroad ~]# yum remove httpd -y -v
Loading "fastestmirror" plugin
Config time: 0.007
Yum version: 3.4.3
rpmdb time: 0.000
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be erased
Checking deps for httpd.x86_64 0:2.4.6-40.el7.centos.4 - e
--> Finished Dependency Resolution
Dependency Process ending
Depsolve time: 0.220
Dependencies Resolved
=========================================================================
 Package              Arch       Version                Repository   Size
=========================================================================
Removing:
 httpd                x86_64     2.4.6-40.el7.centos.4  @updates    9.4 M
Transaction Summary
=========================================================================
Remove  1 Package
Installed size: 9.4 M
Downloading packages:
Member: httpd.x86_64 0:2.4.6-40.el7.centos.4 - e
Removing Package httpd-2.4.6-40.el7.centos.4.x86_64
Running transaction check
Transaction check time: 0.001
Running transaction test
Transaction test succeeded
Transaction test time: 0.014
Running transaction
  Erasing    : httpd-2.4.6-40.el7.centos.4.x86_64     1/1
  Verifying  : httpd-2.4.6-40.el7.centos.4.x86_64     1/1
VerifyTransaction time: 0.049
Transaction time: 0.425
Removed:
  httpd.x86_64 0:2.4.6-40.el7.centos.4
Complete!
Linux 中包管理中的 yum 命令示例

Yellowdog Updater Modifier (yum) 是一个基于 RPM 的包管理器,用于在包括 CentOS、RHEL 和 Fedora 在内的各种 Linux 发行版中安装和更新包。

Yum 非常强大,因为它能够自动解决依赖问题,并且类似于其他包管理器,例如基于 Debian 的发行版中的“apt-get”。

日期:2020-07-07 20:57:03 来源:oir作者:oir