Linux系统yum命令

一.yum命令概述

1.概述:yum(Yellowdog Update Modifier)是通过分析rpm的标头资料后,根据各软件的依赖关系制作出有依赖关系时的解决方案,可以实现自动处理软件的依赖性问题,去解决软件安装或卸载与升级的问题。
ps:yum仅作为rpm包管理的前端工具,并不能替代rpm包管理工具。

2.yum的实现:
1)由于distribution必须要先释出软件,然后将软件放置于yum服务器上面,用以提供用户端来安装与升级。
2)在需要使用yum的功能时,必须要先找到合适的yum server才行。而每个yum server可能都会提供许多不同的软件功能,yum server会根据功能进行分类,这里的分类就是所谓的仓库(repository)。
3)因此。必须要前往yum server查询到相关的仓库网址后(仓库的路径:repodata目录所在处),再进行后续的配置。

3.yum工作步骤
1.实际上由各主机去访问repo时,先下载仓库的元数据,根据元数据去查询所需要的rpm及其各种依赖关系,之后再在仓库进行相关下载,并自动解决rpm包的依赖关系。
2.并且repo仓库应该是一个文件服务器,一般linux主机在下载过元数据的同时会将其保留在缓存中,以便下次使用。
3.但在repo仓库有进行rpm包的改动时,linux主机会通过其保存的元数据校验码与repo仓库的元数据校验码进行比对,借此手段去确定元数据是否更改,是否需要下载新的repo仓库元数据。

ps:1.centos上yum以后有可能会被dnf命令取代
2.yum repository:yum repo,存放了众多的rpm包,以及包的相关元数据文件(放置于特定目录repodata下)
3.repo仓库应该为一个文件服务器,其应该支持四种格式:ftp://格式;http://格式;nfs://格式;file:///(本地文件)格式。

二.yum配置文件及仓库指向定义

1.yum配置文件:
1)/etc/yum.conf:为所有仓库提供公共配置
2)/etc/yum.repos.d/*.repo:为仓库的指向提供配置

2.仓库指向的定义:

  1. [repositoryID]:仓库ID名称
  2. name=Some name for this repository:仓库名字
  3. baseurl=url://path/to/repository/:仓库路径
  4. enabled={1|0}:此仓库是否启用,默认为1启用
  5. gpgcheck={1|0}:是否检查rpm合法性及完整性,默认1为启用
  6. gpgkey=URL:指向rpm包秘钥路径
  7. enablegroups={1|0}:rpm包是否根据功能启用分组,默认1为启用
    8)failovermethod={roundrobin|priority}:失败过度的方法,默认为roundrobin随机挑选剩余仓库路径,priority为按优先级执行,即按顺序执行
    9)cost=:默认为1000,根据cost值大小去选择同一类rpm包的仓库,cost值越小越优先选择

ps:/etc/yum.conf文件

[root@kasumi ~]# cat /etc/yum.conf 
[main]
cachedir=/var/cache/yum/$basearch/$releasever(缓存目录)
keepcache=0(是否保留缓存)
debuglevel=2(调试级别)
logfile=/var/log/yum.log(yum安装程序包后用到的日志文件)
exactarch=1(是否精确平台匹配,适应centos6的rpm,在centos7上及不会安装)
obsoletes=1
gpgcheck=1
plugins=1(是支持插件机制)
installonly_limit=5(同时按装几个程序包)
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release(当前发行版的版本号在哪获取)

三.yum命令的用法

1.yum命令

1.yum语法:yum [options] [command] [package ...]
1)repolist [all | enabled | disabled]:显示仓库列表

[root@kasumi yum.repos.d]# yum repolist
已加载插件:fastestmirror, langpacks
testrepo   ps:本地yum仓库                                                                         | 3.6 kB  00:00:00     
(1/2): testrepo/primary_db                                                          | 5.7 MB  00:00:00     
(2/2): testrepo/group_gz                                                            | 156 kB  00:00:00     
Loading mirror speeds from cached hostfile
源标识                                         源名称                                                 状态
testrepo                                       Centos7 repotest                                       9,591
repolist: 9,591

ps:使用all选项可列出当前所有repo,enabled可列出已启用的repo,disabled列出为禁用的repo
Linux系统yum命令_第1张图片
2)yum list [all | glob_exp1] [glob_exp2] [...]:显示程序包,可搭配通配符使用

[root@kasumi yum.repos.d]# yum list "z*"
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
已安装的软件包
zenity.x86_64                                       3.22.0-1.el7                                  @anaconda 为安装时系统安装的rpm
zip.x86_64                                          3.0-11.el7                                    @anaconda
zlib.x86_64                                         1.2.7-17.el7                                  @anaconda
zsh.x86_64                                          5.0.2-28.el7                                  installed 之后自己已安装的rpm
可安装的软件包
zlib.i686                                           1.2.7-17.el7                                  testrepo 属于testrepo源的rpm
zlib-devel.i686                                     1.2.7-17.el7                                  testrepo 
zlib-devel.x86_64                                   1.2.7-17.el7                                  testrepo 
zlib-static.i686                                    1.2.7-17.el7                                  testrepo 
zlib-static.x86_64                                  1.2.7-17.el7                                  testrepo 
zsh-html.x86_64                                     5.0.2-28.el7                                  testrepo 
zziplib.i686                                        0.13.62-5.el7                                 testrepo ·		
zziplib.x86_64                                      0.13.62-5.el7                                 testrepo 
zziplib-devel.i686                                  0.13.62-5.el7                                 testrepo 
zziplib-devel.x86_64                                0.13.62-5.el7                                 testrepo 
zziplib-utils.x86_64                                0.13.62-5.el7                                 testrepo 

ps: yum list {available|installed|updates} [glob_exp1] […];其中available表示可用单位安装的rpm,installed表示已安装的rpm,updates表示用于更新的rpm。

3)install package1 [package2] [...]:安装程序包,可安装多个

[root@kasumi yum.repos.d]# yum install gcc
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 gcc.x86_64.0.4.8.5-16.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

===========================================================================================================
 Package             架构                   版本                            源                        大小
===========================================================================================================
正在安装:
 gcc                 x86_64                 4.8.5-16.el7                    testrepo                  16 M

事务概要
===========================================================================================================
安装  1 软件包

总下载量:16 M
安装大小:37 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : gcc-4.8.5-16.el7.x86_64                                                                1/1 
  验证中      : gcc-4.8.5-16.el7.x86_64                                                                1/1 

已安装:
  gcc.x86_64 0:4.8.5-16.el7                                                                                

完毕!

ps:reinstall package1 [package2] […] 进行重新安装

4)update [package1] [package2] [...]:升级程序包
ps:使用downgrade package1 [package2] […] :降级程序包

5)check-update:检查可用的升级包

6)remove | erase package1 [package2] [...] :卸载程序包

[root@kasumi yum.repos.d]# yum remove gcc
已加载插件:fastestmirror, langpacks
正在解决依赖关系
--> 正在检查事务
---> 软件包 gcc.x86_64.0.4.8.5-16.el7 将被 删除
--> 解决依赖关系完成

依赖关系解决

===========================================================================================================
 Package             架构                   版本                           源                         大小
===========================================================================================================
正在删除:
 gcc                 x86_64                 4.8.5-16.el7                   @testrepo                  37 M

事务概要
===========================================================================================================
移除  1 软件包

安装大小:37 M
是否继续?[y/N]:y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在删除    : gcc-4.8.5-16.el7.x86_64                                                                1/1 
  验证中      : gcc-4.8.5-16.el7.x86_64                                                                1/1 

删除:
  gcc.x86_64 0:4.8.5-16.el7                                                                                

完毕!

7)info [...]:查看程序包的信息(类似于rpm -qi)

root@kasumi yum.repos.d]# yum info gcc
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
已安装的软件包
名称    :gcc
架构    :x86_64
版本    :4.8.5
发布    :16.el7
大小    :37 M
源    :installed
来自源:testrepo
简介    : Various compilers (C, C++, Objective-C, Java, ...)
网址    :http://gcc.gnu.org
协议    : GPLv3+ and GPLv3+ with exceptions and GPLv2+ with exceptions and LGPLv2+ and BSD
描述    : The gcc package contains the GNU Compiler Collection version 4.8.
         : You'll need this package in order to compile C code.

8)provides | whatprovides feature1 [feature2] [...]:查看指定特性(或某个文件)是由哪个程序包所提供的(相当于rpm -qf)

[root@kasumi yum.repos.d]# yum provides bash
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
bash-4.2.46-28.el7.x86_64 : The GNU Bourne Again shell
源    :testrepo         ps:本地repo



bash-4.2.46-28.el7.x86_64 : The GNU Bourne Again shell
源    :@anaconda        ps:安装系统时安装的程序包

9)clean [ packages(下载过的程序包) | metadata(元数据) | expire-cache(过期缓存) | rpmdb | plugins(插件) | all ]:清理本地缓存

[root@kasumi yum.repos.d]# yum clean all
已加载插件:fastestmirror, langpacks
正在清理软件源: testrepo
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors

10)makecache:构建缓存信息

[root@kasumi yum.repos.d]# yum makecache 
已加载插件:fastestmirror, langpacks
testrepo                                                                            | 3.6 kB  00:00:00     
(1/2): testrepo/filelists_db                                                        | 6.7 MB  00:00:00     
(2/2): testrepo/other_db                                                            | 2.5 MB  00:00:00     
Loading mirror speeds from cached hostfile

11)search string1 [string2] [...]:以指定的关键字搜索程序包及summary(摘要)信息,模糊匹配

[root@kasumi yum.repos.d]# yum search bash
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
============================================ N/S matched: bash ============================================
bash-completion.noarch : Programmable completion for Bash
bash-doc.x86_64 : Documentation files for bash
libguestfs-bash-completion.noarch : Bash tab-completion scripts for libguestfs tools
pcp-pmda-bash.x86_64 : Performance Co-Pilot (PCP) metrics for the Bash shell
bash.x86_64 : The GNU Bourne Again shell

  名称和简介匹配 only,使用“search all”试试。

12)deplist package1 [package2] [...]:查询指定包所依赖的功能

[root@kasumi yum.repos.d]# yum deplist bash
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
软件包:bash.x86_64 4.2.46-28.el7
   依赖:libc.so.6(GLIBC_2.15)(64bit) bash依赖于这个功能
   provider: glibc.x86_64 2.17-196.el7  这个功能由这个包提供
   依赖:libdl.so.2()(64bit)
   provider: glibc.x86_64 2.17-196.el7
   依赖:libdl.so.2(GLIBC_2.2.5)(64bit)
   provider: glibc.x86_64 2.17-196.el7
   依赖:libtinfo.so.5()(64bit)
   provider: ncurses-libs.x86_64 5.9-13.20130511.el7
   依赖:rtld(GNU_HASH)
   provider: glibc.x86_64 2.17-196.el7
   provider: glibc.i686 2.17-196.el7

13)history [info|list|packages-list|packages-info|summary|addon-info|redo|undo|rollback|new|sync|stats] :查看yum事务历史(一般查询不会启动yum事务,只有安装或卸载或升级之类会启动yum事务)

[root@kasumi yum.repos.d]# yum history 
已加载插件:fastestmirror, langpacks
ID     | 登录用户                 | 日期和时间       | 操作           | 变更数 
-------------------------------------------------------------------------------
     9 | root <root>              | 2019-05-06 23:29 | Install        |    1   
     8 | root <root>              | 2019-05-06 23:29 | Erase          |    1 EE
     7 | root <root>              | 2019-05-06 23:24 | Install        |    1   
     6 | root <root>              | 2019-05-06 23:08 | Install        |    1   
     5 | root <root>              | 2019-05-06 23:04 | Erase          |    1   
     4 | root <root>              | 2019-05-06 22:57 | Reinstall      |    1   
     3 | root <root>              | 2019-05-06 22:57 | Install        |    1   
     2 | root <root>              | 2019-05-06 22:56 | Erase          |    7  <
     1 | 系统 <>                | 2019-04-08 23:34 | Install        | 1319 > 
history list

14)* localinstall rpmfile1 [rpmfile2] [...] (maintained for legacy reasons only - use install):安装本地程序包,现在使用install即可。
ps:* localupdate rpmfile1 [rpmfile2] […] (maintained for legacy reasons only - use update):升级同理使用update即可。

2.包组管理的相关命令

1)grouplist [hidden] [groupwildcard] [...]:查看包组

[root@kasumi yum.repos.d]# yum grouplist
Loaded plugins: fastestmirror, langpacks
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          带GUI的服务器
   GNOME Desktop            GNOME桌面
   KDE Plasma Workspaces    KDE工作站
   Development and Creative Workstation  开发和创作工作站
Available Groups:可用组
   Compatibility Libraries   兼容性程序库
   Console Internet Tools   控制台互联网工具
   Development Tools        开发工具
   Graphical Administration Tools 图形管理工具
   Legacy UNIX Compatibility 传统UNIX兼容性
   Scientific Support					科学计数法支持
   Security Tools                       安全性工具
   Smart Card Support            智能卡支持
   System Administration Tools   系统管理工具
   System Management				系统管理
Done

2)groupinstall group1 [group2] [...]:安装包组

[root@kasumi yum.repos.d]# yum groupinstall "Development Tools"  包组用""括起来使用
Loaded plugins: fastestmirror, langpacks
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package gcc-c++.x86_64 0:4.8.5-16.el7 will be installed
---> Package gcc-gfortran.x86_64 0:4.8.5-16.el7 will be installed
--> Processing Dependency: libquadmath-devel = 4.8.5-16.el7 for package: gcc-gfortran-4.8.5-16.el7.x86_64
---> Package libtool.x86_64 0:2.4.2-22.el7_3 will be installed
---> Package systemtap.x86_64 0:3.1-3.el7 will be installed
--> Processing Dependency: systemtap-devel = 3.1-3.el7 for package: systemtap-3.1-3.el7.x86_64
--> Running transaction check
---> Package libquadmath-devel.x86_64 0:4.8.5-16.el7 will be installed
---> Package systemtap-devel.x86_64 0:3.1-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================
 Package                       Arch               Version                       Repository            Size
===========================================================================================================
Installing for group install "Development Tools":
 gcc-c++                       x86_64             4.8.5-16.el7                  testrepo             7.2 M
 gcc-gfortran                  x86_64             4.8.5-16.el7                  testrepo             6.7 M
 libtool                       x86_64             2.4.2-22.el7_3                testrepo             588 k
 systemtap                     x86_64             3.1-3.el7                     testrepo             144 k
Installing for dependencies:
 libquadmath-devel             x86_64             4.8.5-16.el7                  testrepo              49 k
 systemtap-devel               x86_64             3.1-3.el7                     testrepo             2.0 M

Transaction Summary
===========================================================================================================
Install  4 Packages (+2 Dependent packages)

Total download size: 17 M
Installed size: 42 M
Is this ok [y/d/N]: y
Downloading packages:
-----------------------------------------------------------------------------------------------------------
Total                                                                      109 MB/s |  17 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libquadmath-devel-4.8.5-16.el7.x86_64                                                   1/6 
  Installing : systemtap-devel-3.1-3.el7.x86_64                                                        2/6 
  Installing : systemtap-3.1-3.el7.x86_64                                                              3/6 
  Installing : gcc-gfortran-4.8.5-16.el7.x86_64                                                        4/6 
  Installing : libtool-2.4.2-22.el7_3.x86_64                                                           5/6 
  Installing : gcc-c++-4.8.5-16.el7.x86_64                                                             6/6 
  Verifying  : systemtap-devel-3.1-3.el7.x86_64                                                        1/6 
  Verifying  : gcc-c++-4.8.5-16.el7.x86_64                                                             2/6 
  Verifying  : systemtap-3.1-3.el7.x86_64                                                              3/6 
  Verifying  : libquadmath-devel-4.8.5-16.el7.x86_64                                                   4/6 
  Verifying  : libtool-2.4.2-22.el7_3.x86_64                                                           5/6 
  Verifying  : gcc-gfortran-4.8.5-16.el7.x86_64                                                        6/6 

Installed:
  gcc-c++.x86_64 0:4.8.5-16.el7    gcc-gfortran.x86_64 0:4.8.5-16.el7    libtool.x86_64 0:2.4.2-22.el7_3   
  systemtap.x86_64 0:3.1-3.el7    

Dependency Installed:
  libquadmath-devel.x86_64 0:4.8.5-16.el7                systemtap-devel.x86_64 0:3.1-3.el7               

Complete!

3)groupremove group1 [group2] [...]:包组移除

4)groupupdate group1 [group2] [...]:包组更新

5)groupinfo group1 [...]:包组相关信息

3.yum命令的可用选项

1.-y:自动回答为“yes”

[root@yuki ~]# yum install -y zsh
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * aliyun: mirrors.aliyun.com
 * centosplus: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 zsh.x86_64.0.5.0.2-31.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

============================================================================================================
 Package              架构                    版本                            源                       大小
============================================================================================================
正在安装:
 zsh                  x86_64                  5.0.2-31.el7                    aliyun                  2.4 M

事务概要
============================================================================================================
安装  1 软件包

总下载量:2.4 M
安装大小:5.6 M
Downloading packages:
zsh-5.0.2-31.el7.x86_64.rpm                                                          | 2.4 MB  00:00:26     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : zsh-5.0.2-31.el7.x86_64                                                                 1/1 
  验证中      : zsh-5.0.2-31.el7.x86_64                                                                 1/1 

已安装:
  zsh.x86_64 0:5.0.2-31.el7                                                                                 

完毕!

2.-q:静默模式

[root@yuki ~]# yum remove -q zsh

============================================================================================================
 Package              架构                    版本                           源                        大小
============================================================================================================
正在删除:
 zsh                  x86_64                  5.0.2-31.el7                   @aliyun                  5.6 M

事务概要
============================================================================================================
移除  1 软件包

是否继续?[y/N]:y
[root@yuki ~]# yum install -q zsh

============================================================================================================
 Package              架构                    版本                            源                       大小
============================================================================================================
正在安装:
 zsh                  x86_64                  5.0.2-31.el7                    aliyun                  2.4 M

事务概要
============================================================================================================
安装  1 软件包

Is this ok [y/d/N]: y

3.- -enablerepo:临时启用指定的某repo

[root@yuki ~]# yum --disablerepo=* --enablerepo=aliyun install zsh
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * aliyun: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 zsh.x86_64.0.5.0.2-31.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

============================================================================================================
 Package              架构                    版本                            源                       大小
============================================================================================================
正在安装:
 zsh                  x86_64                  5.0.2-31.el7                    aliyun                  2.4 M

事务概要
============================================================================================================
安装  1 软件包

总下载量:2.4 M
安装大小:5.6 M
Is this ok [y/d/N]: y
Downloading packages:
zsh-5.0.2-31.el7.x86_64.rpm                                                          | 2.4 MB  00:00:26     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
  正在安装    : zsh-5.0.2-31.el7.x86_64                                                                 1/1 
  验证中      : zsh-5.0.2-31.el7.x86_64                                                                 1/1 

已安装:
  zsh.x86_64 0:5.0.2-31.el7                                                                                 

完毕!

4.- -disablerepo:临时禁用在配置文件中配置并启用的repo

[root@yuki ~]# yum repolist 
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * aliyun: mirrors.aliyun.com
 * centosplus: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
源标识                                           源名称                                               状态
aliyun/7/x86_64                                  this is os repo                                      10,019
centosplus/7/x86_64                              this is centosplus repo                                  59
extras/7/x86_64                                  this is extras repo                                     413
updates/7/x86_64                                 this is updates repo                                  1,840
repolist: 12,331
[root@yuki ~]# yum --disablerepo=* install zsh
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
There are no enabled repos.
 Run "yum repolist all" to see the repos you have.
 To enable Red Hat Subscription Management repositories:
     subscription-manager repos --enable <repo>
 To enable custom repositories:
     yum-config-manager --enable <repo>

5.- -nogpgcheck:禁止检验其合法性及完整性

4.yum内置变量

1.yum内会内置一些变量,用于保存当前平台的相关信息
1)$releasever:用与保存当前发行版的主版本号,例如:
Centos 6.6 x86_64,主版本号为6

2)$arch:平台架构,例如:
i386,x86_64

3)$basearch:基础平台架构,例如:
i386,i586,i686基础平台为i386

4)例如https://mirrors.aliyun.com/centos/7.6.1810/os/x86_64/使用yum内置变量即可表示为https://mirrors.aliyun.com/centos/$releasever/os/$basearch

你可能感兴趣的:(linux基础学习)