2020年5月20日星期三

Linux命令(8)yum命令

Linux命令(8)yum命令


目录

  • yum命令
    • 检查更新
    • 更新程序包
    • 查找程序包
    • 列出程序包
    • 列出所有程序包组
    • 安装程序包组
    • 卸载程序包组
    • 查看程序包信息
    • 安装程序包
    • 重新安装程序包
    • 卸载程序包
    • 列出yum仓库
    • 添加Yum仓库
    • 启用Yum仓库
    • 禁用Yum仓库
    • 添加Yum仓库遇到的问题
    • 更新与安全相关的包

yum命令

检查更新

查看系统上已安装的程序包有哪些可用更新

yum check-update
~]# yum check-updateLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfileGeoIP.x86_64     1.5.0-14.el7      base  NetworkManager.x86_64   1:1.18.4-3.el7     base   NetworkManager-libnm.x86_64 1:1.18.4-3.el7     base   NetworkManager-team.x86_64 1:1.18.4-3.el7     base   NetworkManager-tui.x86_64  1:1.18.4-3.el7     base   acl.x86_64     2.2.51-15.el7      base   alsa-lib.x86_64    1.1.8-1.el7      base...
  • GeoIP - 包的名称
  • x86_64 - 构建包的CPU架构
  • 1.5.0 - 要安装的更新包的版本
  • base - 更新的程序包所在的存储库

更新程序包

我们可以一次更新一个、多个或所有程序包,更新时此程序包所有依赖的程序包也会进行更新。

更新单个程序包

yum update package_name

示例:更新bash程序包

~]# yum update bashLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfileResolving Dependencies--> Running transaction check---> Package bash.x86_64 0:4.2.46-31.el7 will be updated---> Package bash.x86_64 0:4.2.46-34.el7 will be an update--> Finished Dependency ResolutionDependencies Resolved========================================================================================= Package   Arch   Version   Repository   Size=========================================================================================Updating: bash    x86_64   4.2.46-34.el7  base    1.0 MTransaction Summary=========================================================================================Upgrade 1 PackageTotal download size: 1.0 MIs this ok [y/d/N]: 
  • Loaded plugins - 显示Yum所有的插件

  • bash.x86_64 - 表示所要安装的更新包

  • yum默认情况下以交互方式运行

    • y 选项,表示同意安装更新

    • d 选项,yum会立即下载程序包而不安装它们。我们可以使用 yum localinstall 来安装这些程序包,不过此命令已经废弃,可直接使用 yum install 来安装。

      下载的程序包默认保存在为 /var/cache/yum/\(basearch/\)releasever/packages/

    • N 取消安装更新

更新所有程序包及其依赖的程序包

 yum update

更新与安全相关的包

如果程序包具有可用的安全更新,则只能将这些程序包更新为其最新版本。

yum update --security

查找程序包

 yum search package_name
~]# yum search zshLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfile=================== N/S matched: zsh ==========================autojump-zsh.noarch : Autojump for zshzsh-html.x86_64 : Zsh shell manual in html formatzsh.x86_64 : Powerful interactive shellzsh-lovers.noarch : A collection of tips, tricks and examples for the Z shell Name and summary matches only, use "search all" for everything.
  • 默认情况下,yum search返回匹配的程序包的名称和摘要信息。

列出程序包

列出系统上所有已经安装和可用的程序包

 yum list all
~]# yum list allLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfileInstalled PackagesGeoIP.x86_64     1.5.0-11.el7  @CentOS7-baseNetworkManager.x86_64   1:1.10.2-13.el7 @CentOS7-baseaccountsservice.x86_64  0.6.45-7.el7  CentOS7-base accountsservice-devel.i686 0.6.45-7.el7  CentOS7-base ...... 
  • 如果程序包已经安装,在其对应的yum仓库名称前会有一个 @CentOS7-base 标志,如果没有安装,则没有任何标志CentOS7-base。

列出系统上已经安装的所有程序包,输出内容最右侧表示程序包所在的yum仓库。

yum list installed
~]# yum list installedLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfileInstalled PackagesGeoIP.x86_64    1.5.0-11.el7   @CentOS7-base NetworkManager.x86_64 1:1.10.2-13.el7  @CentOS7-base 

列出所有已启用存储库中的所有可用的程序包

yum list available
~]# yum list availableLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfileAvailable Packages0ad.x86_64    0.0.22-1.el7   fedora-epel-70ad-data.noarch   0.0.22-1.el7   fedora-epel-70install.x86_64   2.11-1.el7   fedora-epel-7

列出所有程序包组

yum grouplist
[root@node1 ~]# yum groups listLoaded plugins: fastestmirrorThere is no installed groups file.Maybe run: yum groups mark convert (see man yum)Loading mirror speeds from cached hostfileAvailable 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 WorkstationAvailable 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 ManagementDone

列出所有程序包组的组ID

[root@node1 ~]# yum grouplist idsLoaded plugins: fastestmirrorThere is no installed groups file.Maybe run: yum groups mark convert (see man yum)Loading mirror speeds from cached hostfileAvailable Environment Groups: Minimal Install (minimal) Compute Node (compute-node-environment) Infrastructure Server (infrastructure-server-environment) File and Print Server (file-print-server-environment) Basic Web Server (web-server-environment) Virtualization Host (virtualization-host-environment) Server with GUI (graphical-server-environment) GNOME Desktop (gnome-desktop-environment) KDE Plasma Workspaces (kde-desktop-environment) Development and Creative Workstation (developer-workstation-environment)Available Groups: Compatibility Libraries (compat-libraries) Console Internet Tools (console-internet) Development Tools (development) Graphical Administration Tools (graphical-admin-tools) Legacy UNIX Compatibility (legacy-unix) Scientific Support (scientific) Security Tools (security-tools) Smart Card Support (smart-card) System Administration Tools (system-admin-tools) System Management (system-management)Done

列出所有的程序包的组ID及隐藏的ID

[root@node1 ~]# yum grouplist hidden ids

安装程序包组

安装程序包组有四种方法

yum group install "group name"yum group install groupidyum install @groupyum install @^group
  • 以上四种方法是等价的,哪种简单用哪种。

示例:以Development Tools为例,显示四种安装方法

 ~]# yum group install "Development Tools" ~]# yum group install development ~]# yum install @"Development Tools" ~]# yum install @development
  • 个人喜欢第四种,因为可以少打字。

卸载程序包组

yum group remove "group name"yum group remove groupidyum remove @groupyum remove @^group

示例:以Development Tools为例,显示四种卸载程序包组的方法

 ~]# yum group remove "Development Tools" ~]# yum group remove development ~]# yum remove @"Development Tools" ~]# yum remove @development

查看程序包信息

查看一个或多个程序包的信息

~]#yum info package_name1 [package_name2] [...]

示例:查看httpd程序包的信息。

~]# yum info httpdLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfileInstalled PackagesName   : httpdArch   : x86_64Version  : 2.4.6Release  : 80.el7.centosSize   : 9.4 MRepo   : installedFrom repo : CentOS7-baseSummary  : Apache HTTP ServerURL   :  : ASL 2.0Description : The Apache HTTP Server is a powerful,    : efficient, and extensible web server.

安装程序包

安装程序包时 yum命令会自动解决依赖关系,将程序包及其所有未安装的依赖的程序包都安装。

安装单个程序包

~]# yum install package_name

安装多个程序包

安装多个程序包时,可以将多个程序包名以空格分隔来安装。

~]# yum install package_name1 [package_name2] [...]

默认安装程序包的最新版本,安装旧版本时需要指定版本号。

示例:同时安装多个程序包。

~]# yum install httpd mysql

当我们只是知道要安装的二进制文件的名称,而不知道其程序包名称,则可以为 yum install 提供路径名称。

~]# yum install /usr/bin/passwd

这时yum会搜索包列表,检索哪个包提供了/usr/bin/passwd,如果有的话就会提供我们安装。

查看指定的文件是由哪个程序包提供

yum provides "filename" 

当我们只是知道一个命令,如ls,但不确定其到底安装在/bin目录还是/sbin/目录中,以及ls命令是由哪个程序包安装,我们可以使用 yum provides 命令。

# yum provides "*bin/ls"Loaded plugins: fastestmirrorLoading mirror speeds from cached hostfilecoreutils-8.22-21.el7.x86_64 : A set of basic GNU toolscommonly used in shell scriptsRepo: CentOS7-baseMatched from:Filename: /bin/lsFilename: /usr/bin/lscoreutils-8.22-21.el7.x86_64 : A set of basic GNU tools commonly used in shell scriptsRepo: @CentOS7-baseMatched from:Filename: /bin/lsFilename: /usr/bin/ls

重新安装程序包

~]# yum reinstall httpd

卸载程序包

要卸载特定程序包以及依赖于它的任何程序包

yum remove package_name...

示例:卸载httpd程序包

~]# yum remove httpd

卸载多个程序包

~]# yum remove package_name1 [package_name2] [...]

列出yum仓库

列出系统上每个已启用仓库的仓库ID,名称和程序包数量。

~]# yum repolist Loaded plugins: fastestmirrorLoading mirror speeds from cached hostfilerepo id    repo name   statusCentOS7-base   CentOS7-base  9,911docker-ce-7   docker-ce-7   156fedora-epel-7   fedora-epel-7  12,661

yum repolist 只显示状态为enabled的仓库信息。使用 yum repoinfo命令列出仓库的更多信息,不过此命令在CentOS6系统上不可用。

列出已经启用和禁用的仓库

yum repolist all
 ~]# yum repolist allLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfilerepo id    repo name    statusC7.0.1406-base/x86_64 CentOS-7.0.1406 - Base disabledCentOS7-base   CentOS7-base    enabled: 9,911fedora-epel-7   fedora-epel-7   enabled: 12,661
  • 最右侧输出的状态列 status 列出了相关仓库的启用和禁用状态。

添加Yum仓库

一般我们为方便管理都会在 /etc/yum.repos.d/目录中添加文件后缀为.repo的文件,而不是在/etc/yum.conf文件中添加。

互联网上的Yum仓库通常提供自己的.repo文件,我们只需要使用以下命令添加即可。

yum-config-manager --add-repo repository_url
  • 其中repository_url是.repo文件的链接。

示例:给当前系统添加网易的CentOS源。

~]# yum-config-manager --add-repo id="启用yum仓库">启用Yum仓库
yum-config-manager --enable repositoryID
  • repositoryID是仓库的唯一ID(使用 yum repolist all 列出可用的存储库ID)

启用所有存储库

~]# yum-config-manager --enable \*

禁用Yum仓库

yum-config-manager --disable repositoryID
  • repositoryID是仓库的唯一ID(使用 yum repolist all 列出可用的存储库ID)

禁用所有存储库

~] yum-config-manager --disable \*

添加Yum仓库遇到的问题

[root@node1 ~]# yum-config-manager --add-repo https://mirrors.163.com/.help/CentOS7-Base-163.repoLoaded plugins: fastestmirroradding repo from: https://mirrors.163.com/.help/CentOS7-Base-163.repograbbing file https://mirrors.163.com/.help/CentOS7-Base-163.repo to /etc/yum.repos.d/CentOS7-Base-163.repoCould not fetch/save url https://mirrors.163.com/.help/CentOS7-Base-163.repo to file /etc/yum.repos.d/CentOS7-Base-163.repo: [Errno 14] curl#60 - "Peer's Certificate has expired."

出现问题的原因是当前系统的时间与yum源的时间不一致导致。

解决办法:

[root@node1 ~]# yum -y install ntp[root@node1 ~]# ntpdate time.windows.com

更新与安全相关的包

列出所有与安全相关的更新

 yum --security check-update 

如果程序包具有可用的安全更新,则将这些程序包更新为最新版本

 yum --security update 

只安装最安全的安全更新,忽略所有可能包含不安全新功能的包

yum --security update-minimal

没有评论:

发表评论