Linux Yum 命令示例:安装、卸载、更新包

举报
Tiamo_T 发表于 2022/05/16 00:06:09 2022/05/16
【摘要】 安装、删除和更新包是 Linux 上的典型活动。大多数 Linux 发行版都提供了某种包管理器实用程序。例如,apt-get、dpkg、rpm、yum 等

安装、删除和更新包是 Linux 上的典型活动。大多数 Linux 发行版都提供了某种包管理器实用程序。例如,apt-get、dpkg、rpm、yum 等。

在某些 Linux 发行版上,yum 是默认的包管理器。

Yum 代表 Yellowdog Updater Modified。

本文通过示例解释了 15 个最常用的 yum 命令。

1.使用yum install安装一个包

要安装软件包,请执行“yum install packagename”。这还将自动识别依赖项并安装它们。

以下示例安装 postgresql 包。

# yum install postgresql.x86_64
Resolving Dependencies
Install       2 Package(s)
Is this ok [y/N]: y

Package(s) data still to download: 3.0 M
(1/2): postgresql-9.0.4-5.fc15.x86_64.rpm          | 2.8 MB     00:11
(2/2): postgresql-libs-9.0.4-5.fc15.x86_64.rpm    | 203 kB     00:00
------------------------------------------------------------------
Total                                        241 kB/s | 3.0 MB     00:12     

Running Transaction
  Installing : postgresql-libs-9.0.4-5.fc15.x86_64             1/2
  Installing : postgresql-9.0.4-5.fc15.x86_64                   2/2 

Complete!

默认情况下,“yum install”会在安装软件包之前提示您接受或拒绝。如果您希望 yum 自动安装而不提示,请使用 -y 选项,如下所示。


# yum -y install postgresql.x86_64

2. 使用 yum remove 卸载一个包

要删除一个包(及其所有依赖项),请使用“yum remove package”,如下所示。

# yum remove  postgresql.x86_64
Resolving Dependencies
---> Package postgresql.x86_64 0:9.0.4-5.fc15 will be erased

Is this ok [y/N]: y

Running Transaction
  Erasing    : postgresql-9.0.4-5.fc15.x86_64       1/1 

Removed:
  postgresql.x86_64 0:9.0.4-5.fc15

Complete!

3. 使用 yum update 升级现有包

如果您有旧版本的软件包,请使用“yum update package”将其升级到最新的当前版本。这还将识别并安装所有必需的依赖项。

# yum update postgresql.x86_64

4.使用yum search搜索要安装的包

如果您不知道要安装的确切软件包名称,请使用“yum search keyword”,它将搜索与“关键字”匹配的所有软件包并显示它。

以下示例在 yum 存储库中搜索与关键字“firefox”匹配的所有软件包并列出可用的软件包。

# yum search firefox
Loaded plugins: langpacks, presto, refresh-packagekit
============== N/S Matched: firefox ======================
firefox.x86_64 : Mozilla Firefox Web browser
gnome-do-plugins-firefox.x86_64 : gnome-do-plugins for firefox
mozilla-firetray-firefox.x86_64 : System tray extension for firefox
mozilla-adblockplus.noarch : Adblocking extension for Mozilla Firefox
mozilla-noscript.noarch : JavaScript white list extension for Mozilla Firefox

Name and summary matches only, use "search all" for everything.

5. 使用 yum info 显示有关软件包的附加信息

使用 yum search 搜索软件包后,您可以使用“yum info package”查看有关该软件包的其他信息。

以下示例显示了有关 samba-common 包的附加信息。

# yum info samba-common.i686
Loaded plugins: langpacks, presto, refresh-packagekit
Available Packages
Name        : samba-common
Arch        : i686
Epoch       : 1
Version     : 3.5.11
Release     : 71.fc15.1
Size        : 9.9 M
Repo        : updates
Summary     : Files used by both Samba servers and clients
URL         : http://www.samba.org/
License     : GPLv3+ and LGPLv3+
Description : Samba-common provides files necessary for both the server and client
            : packages of Samba.

6.使用yum list查看所有可用的包

以下命令将列出 yum 数据库中所有可用的软件包。

# yum list | less

7. 使用 yum list installed 仅列出已安装的包

要查看系统上安装的所有软件包,请执行以下 yum 命令。

# yum list installed | less

8. 文件属于哪个包?– 使用 yum 提供

如果您想知道特定文件属于哪个包,请使用“yum 提供”。例如,如果您想知道包含 /etc/sysconfig/nfs 文件的软件包的名称,请执行以下操作。

# yum provides /etc/sysconfig/nfs
Loaded plugins: langpacks, presto, refresh-packagekit
1:nfs-utils-1.2.3-10.fc15.x86_64 : NFS utilities and supporting clients and
                                 : daemons for the kernel NFS server
Repo        : fedora
Matched from:
Filename    : /etc/sysconfig/nfs

1:nfs-utils-1.2.4-1.fc15.x86_64 : NFS utilities and supporting clients and
                                : daemons for the kernel NFS server
Repo        : updates
Matched from:
Filename    : /etc/sysconfig/nfs

1:nfs-utils-1.2.4-1.fc15.x86_64 : NFS utilities and supporting clients and
                                : daemons for the kernel NFS server
Repo        : installed
Matched from:
Other       : Provides-match: /etc/sysconfig/nfs

9. 使用 yum grouplist 列出可用的软件组

在 yum 中,几个相关的包被组合在一个特定的组中。您可以简单地安装组,而不是搜索和安装属于特定功能的所有单个包,这将安装属于该组的所有包。

要查看所有可用的软件组,请执行“yum grouplist”,如下所示。输出在三个组中列出 - 已安装组、已安装语言组和可用组。

# yum grouplist

Installed Groups:
   Administration Tools
   Base
   Design Suite
   ....

Installed Language Groups:
   Arabic Support [ar]
   Armenian Support [hy]
   Bengali Support [bn]
   ....

Available Groups:
   Authoring and Publishing
   Books and Guides
   Clustering
   DNS Name Server
   Development Libraries
   Development Tools
   Directory Server
   Dogtag Certificate System
   ...

10. 使用 yum groupinstall 安装特定的软件组

要安装特定的软件组,请使用 groupinstall 选项,如下所示。在以下示例中,“DNS 名称服务器”组包含 bind 和 bind-chroot。

# yum groupinstall 'DNS Name Server'

Dependencies Resolved
Install       2 Package(s)
Is this ok [y/N]: y

Package(s) data still to download: 3.6 M
(1/2): bind-9.8.0-9.P4.fc15.x86_64.rpm             | 3.6 MB     00:15
(2/2): bind-chroot-9.8.0-9.P4.fc15.x86_64.rpm   |  69 kB     00:00
-----------------------------------------------------------------
Total               235 kB/s | 3.6 MB     00:15

Installed:
  bind-chroot.x86_64 32:9.8.0-9.P4.fc15

Dependency Installed:
  bind.x86_64 32:9.8.0-9.P4.fc15

Complete!

注意:您也可以使用前面讨论的yum groupinstall 安装 MySQL 数据库。

11. 使用 groupupdate 升级现有软件组

如果您已经使用 yum groupinstall 安装了软件组,并且想将其升级到最新版本,请使用 'yum groupupdate',如下所示。

# yum groupupdate 'Graphical Internet'

Dependencies Resolved
Upgrade       5 Package(s)
Is this ok [y/N]: y   

Running Transaction
  Updating   : evolution-data-server-3.0.2-1.fc15.x86_64     1/10
  Updating   : evolution-3.0.2-3.fc15.x86_64                 2/10
  Updating   : evolution-NetworkManager-3.0.2-3.fc15.x86_64  3/10
  Updating   : evolution-help-3.0.2-3.fc15.noarch            4/10
  Updating   : empathy-3.0.2-3.fc15.x86_64                   5/10
  Cleanup    : evolution-NetworkManager-3.0.1-1.fc15.x86_64  6/10
  Cleanup    : evolution-help-3.0.1-1.fc15.noarch            7/10
  Cleanup    : evolution-3.0.1-1.fc15.x86_64                 8/10
  Cleanup    : empathy-3.0.1-3.fc15.x86_64                   9/10
  Cleanup    : evolution-data-server-3.0.1-1.fc15.x86_64     10/10 

Complete!

12.使用yum groupremove卸载软件组

要删除现有软件组,请使用“yum groupremove”,如下所示。

# yum groupremove 'DNS Name Server'
Dependencies Resolved
Remove        2 Package(s)
Is this ok [y/N]: y

Running Transaction
  Erasing    : 32:bind-chroot-9.8.0-9.P4.fc15.x86_64  1/2
  Erasing    : 32:bind-9.8.0-9.P4.fc15.x86_64            2/2 

Complete!

13. 显示您当前的 yum 存储库

所有 yum 命令都针对一个或多个 yum 存储库。要查看系统中配置的所有 yum 存储库,请执行“yum repolist”,如下所示。

以下将仅显示启用的存储库。

# yum repolist
repo id     repo name                        status
fedora      Fedora 15 - x86_64               24,085
updates     Fedora 15 - x86_64 - Updates     5,612

要显示所有存储库(启用和禁用),请使用“yum repolist all”。

# yum repolist all
repo id                   repo name                                status
fedora                    Fedora 15 - x86_64                       enabled: 24,085
fedora-debuginfo          Fedora 15 - x86_64 - Debug               disabled
fedora-source             Fedora 15 - Source                       disabled
rawhide-debuginfo         Fedora - Rawhide - Debug                 disabled
rawhide-source            Fedora - Rawhide - Source                disabled
updates                   Fedora 15 - x86_64 - Updates             enabled:  5,612
updates-debuginfo         Fedora 15 - x86_64 - Updates - Debug     disabled
updates-source            Fedora 15 - Updates Source               disabled
updates-testing           Fedora 15 - x86_64 - Test Updates        disabled
updates-testing-debuginfo Fedora 15 - x86_64 - Test Updates Debug  disabled
updates-testing-source    Fedora 15 - Test Updates Source          disabled

要仅查看禁用的存储库,请使用“yum repositories disabled”。

14. 使用 yum –enablerepo 从禁用的存储库安装

默认情况下,yum 仅从启用的存储库安装。出于某种原因,如果您想从禁用的存储库安装软件包,请使用“yum install”中的 –enablerepo 选项,如下所示。

# yum --enablerepo=fedora-source install vim-X11.x86_64
Dependencies Resolved
Install       1 Package(s)
Is this ok [y/N]: y

Running Transaction
  Installing : 2:vim-X11-7.3.138-1.fc15.x86_64   1/1 

Complete!

15. 使用 Yum Shell 交互执行 yum 命令

Yum 提供交互式 shell 来运行多个命令,如下所示。

# yum shell
Setting up Yum Shell
> info samba.x86_64
Available Packages
Name        : samba
Arch        : x86_64
Epoch       : 1
Version     : 3.5.11
Release     : 71.fc15.1
Size        : 4.6 M
Repo        : updates
Summary     : Server and Client software to interoperate with Windows machines
URL         : http://www.samba.org/
License     : GPLv3+ and LGPLv3+
Description :
            : Samba is the suite of programs by which a lot of PC-related
            : machines share files, printers, and other information (such as
            : lists of available files and printers). The Windows NT, OS/2, and
            : Linux operating systems support this natively, and add-on packages
            : can enable the same thing for DOS, Windows, VMS, UNIX of all
            : kinds, MVS, and more. This package provides an SMB/CIFS server
            : that can be used to provide network services to SMB/CIFS clients.
            : Samba uses NetBIOS over TCP/IP (NetBT) protocols and does NOT
            : need the NetBEUI (Microsoft Raw NetBIOS frame) protocol.

Yum 还可以从文本文件中读取命令并一一执行。当您有多个系统时,这非常有用。不要在所有系统上执行相同的命令,而是使用这些命令创建一个文本文件,然后使用“yum shell”执行这些命令,如下所示。

# cat yum_cmd.txt
repolist
info nfs-utils-lib.x86_64

# yum shell yum_cmd.txt 
repo id     repo name                        status
fedora      Fedora 15 - x86_64               24,085
updates     Fedora 15 - x86_64 - Updates     5,612

Available Packages
Name        : nfs-utils-lib
Arch        : x86_64
Version     : 1.1.5
Release     : 5.fc15
Size        : 61 k
Repo        : fedora
Summary     : Network File System Support Library
URL         : http://www.citi.umich.edu/projects/nfsv4/linux/
License     : BSD
Description : Support libraries that are needed by the commands and
            : daemons the nfs-utils rpm.

Leaving Shell
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。