Linux命令(8)yum命令

2020-05-20 09:10:01来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Linux命令(8)yum命令

目录

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

yum命令

检查更新

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

yum check-update
~]# yum check-update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile

GeoIP.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 bash
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving 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 Resolution

Dependencies Resolved
=========================================================================================
 Package          Arch            Version          Repository         Size
=========================================================================================
Updating:
 bash             x86_64          4.2.46-34.el7     base              1.0 M

Transaction Summary
=========================================================================================
Upgrade  1 Package

Total download size: 1.0 M
Is 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 zsh
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
=================== N/S matched: zsh ==========================
autojump-zsh.noarch : Autojump for zsh
zsh-html.x86_64 : Zsh shell manual in html format
zsh.x86_64 : Powerful interactive shell
zsh-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 all
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
GeoIP.x86_64                  1.5.0-11.el7     @CentOS7-base
NetworkManager.x86_64         1:1.10.2-13.el7  @CentOS7-base
accountsservice.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 installed
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
GeoIP.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 available
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Available Packages
0ad.x86_64               0.0.22-1.el7         fedora-epel-7
0ad-data.noarch          0.0.22-1.el7         fedora-epel-7
0install.x86_64          2.11-1.el7           fedora-epel-7

列出所有程序包组

yum grouplist
[root@node1 ~]# yum groups list
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
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

列出所有程序包组的组ID

[root@node1 ~]# yum grouplist ids
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
Available 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 groupid
yum install @group
yum 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 groupid
yum remove @group
yum 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 httpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
Name         : httpd
Arch         : x86_64
Version      : 2.4.6
Release      : 80.el7.centos
Size         : 9.4 M
Repo         : installed
From repo    : CentOS7-base
Summary      : Apache HTTP Server
URL          : http://httpd.apache.org/
License      : ASL 2.0
Description  : 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: fastestmirror
Loading mirror speeds from cached hostfile
coreutils-8.22-21.el7.x86_64 : A set of basic GNU tools
commonly used in shell scripts
Repo: CentOS7-base
Matched from:
Filename: /bin/ls
Filename: /usr/bin/ls

coreutils-8.22-21.el7.x86_64 : A set of basic GNU tools 
commonly used in shell scripts
Repo: @CentOS7-base
Matched from:
Filename: /bin/ls
Filename: /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: fastestmirror
Loading mirror speeds from cached hostfile
repo id                repo name          status
CentOS7-base           CentOS7-base        9,911
docker-ce-7            docker-ce-7           156
fedora-epel-7          fedora-epel-7      12,661

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

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

yum repolist all
 ~]# yum repolist all
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                repo name                status
C7.0.1406-base/x86_64  CentOS-7.0.1406 - Base   disabled
CentOS7-base           CentOS7-base             enabled:  9,911
fedora-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 http://mirrors.163.com/.help/CentOS7-Base-163.repo

启用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.repo
Loaded plugins: fastestmirror
adding repo from: https://mirrors.163.com/.help/CentOS7-Base-163.repo
grabbing file https://mirrors.163.com/.help/CentOS7-Base-163.repo to /etc/yum.repos.d/CentOS7-Base-163.repo
Could 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

原文链接:https://www.cnblogs.com/shijunxian/p/12920520.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:如何让你的脚本可以在任意地方都可执行?

下一篇:实战!我用 Wireshark 让你“看得见“ TCP