yum详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yum详解相关的知识,希望对你有一定的参考价值。

yum
  • yum由来

  • yum基本工作机制

  • yum的相关特点

  • yum的配置文件

  • yum的配置仓库示例

  • yum的命令使用

  • yum中常见问题

yum由来

yum是yellowdog updater修改而来,被yellowdog linux,rhel,centos,fedora所采用.其名为"Yellowdog Updater Modified".是一个基于rpm的前端管理工具.用于解决rpm包之间的依赖关系,并且一次安装所有依赖的软件包,无需繁琐下载,安装.

yum基本工作机制

yum程序如何获取rpm包呢?
    一般有指定的服务器,或者光盘将大量rpm包保存其中.这些存储地方我们称其为"仓库",那么我们是直接将所有的包都下载到本地,还是怎么获取呢?
    其实很简单,服务器端提供一些元数据文件,其元数据文件中包含了rpm包的名字,版本,以及包与包之间的依赖关系等信息.我们
    直接将元数据缓存到本地,再根据所需,yum会自动将rpm下载到本地并安装.

yum再安装完rpm包之后,会自动清除下载的rpm包,但其元数据文件保留,方便下次更快使用.
此方法虽然加快yum使用速度,但如果服务器发生文件更新或者依赖性的某些改动,则无法及时更新元数据文件,为了解决此问题
yum在每次使用之前都会与服务器对比校验码,如果校验码没有变动,则继续使用原来的元数据文件,否则下载新的元数据文件.

获取其rpm包的方式有多种,http,ftp,nfs,本地光盘都能获取仓库.

yum的相关特点

保持与rpm数据库的一致性      #如果rpm数据库损坏,yum也无法正常使用.
同时配置多个源
简单配置文件
解决依赖关系

yum的配置文件

 /etc/yum.conf                  #为所有仓库提供公共配置
 /etc/yum.repos.d/*.repo        #yum的仓库相关配置文件
 **man yum.conf 用来查询其帮助文档 **

 /etc/yum.conf
 [main]
 cachedir=/var/cache/yum/$basearch/$releasever      #缓存目录路径
 keepcache=0        #本地缓存文件是否保存
 debuglevel=2       #debug调试级别
 logfile=/var/log/yum.log       #yum自己的日志文件
 exactarch=1        #是否严格平台匹配
 obsoletes=1        
 gpgcheck=1         #是否检查包的来源合法性
 plugins=1          #是否支持插件
 installonly_limit=5        #一次同时安装几个程序包
 bugtracker_url=http://bugs.centos.org/set_project.php?project_id=16&ref=http://bugs.centos.org/bug_report_page.php?category=yum
 distroverpkg=centos-release        #设定$releasever的值由哪个rpm包的主版本号


 /etc/yum.repos.d/*.repo
 [repositoryid]                                 #仓库id,保持唯一性
 name=Some name for this repository             #仓库名称,仅作标识
 baseurl=url://server1/path/to/repository/      #指定仓库访问路径,以下的行
         url://server2/path/to/repository/
         url://server3/path/to/repository/
 gpgcheck={1|0}                                 #是否检查包来源合法性及完整性,默认为1
 gpgkey=RPM-GPG-KEY                             #指向RPM-GPG-KEY的位置
 enabled={1|0}                                  #是否启用,默认为1
 failovermethod={"roundrobin"|"priority"}       #roundrobin随机选择url,priority从第一个url开始,按顺序选择
 cost=#                                         #仓库开销,用来定义仓库优先级,默认为1000,数字越小优先级越大
 mirrorlist=url://mirrorlist_path               #镜像列表,其路径指向的是访问仓库路径的url,不能与baseurl同时使用

 注意:仓库所指的路径一定是在repodata目录"上",并不是指向package目录.repodata目录中保存了元数据文件.

 一些常用变量:
 arch           #平台,当前系统的架构
 basearch       #基本平台架构,如果当前系统架构为i586,那么此匹配到基本平台的为i386
 releasever     #该发行版的主版本号
 $YUM0-$YUM9    #用于将与同名的环境变量的值,替换为自身,如果不存在则不替换

yum的配置仓库示例

配置本地源:

    vim /etc/yum.repos.d/centos-local.repo
    [local_1]
    name=local cdrom
    baseurl=file:///media/cdrom
    enabled=1
    gpgcheck=1
    gpgkey=file:///media/cdrom/RPM-GPG-KEY-CentOS-6
    cost=50

    显示仓库列表,以下为成功配置
    [[email protected] yum.repos.d]# yum repolist
    Loaded plugins: fastestmirror, refresh-packagekit, security
    Determining fastest mirrors
    local_1                                                                       | 4.0 kB     00:00 ... 
    local_1/primary_db                                                            | 4.4 MB     00:00 ... 
    repo id                                       repo name                                        status
    local_1                                       local iso                                        6,367
    repolist: 6,367



epel源:

    vim /etc/yum.repos.d/epel.repo
    [epel_1]
    name=epel source
    baseurl=http://mirrors.aliyun.com/epel/$releasever/$basearch/
    enabled=1
    gpgcheck=1
    gpgkey=http://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-6
    cost=100

    成功缓存元数据文件,以下为配置成功
    [[email protected] yum.repos.d]# yum makecache
    Loaded plugins: fastestmirror, refresh-packagekit, security
    Determining fastest mirrors
    epel_1                                                                                        | 4.3 kB     00:00     
    epel_1/group_gz                                                                               | 150 kB     00:00     
    epel_1/filelists_db                                                                           | 7.8 MB     00:07     
    epel_1/updateinfo                                                                             | 736 kB     00:00     
    epel_1/primary_db                                                                             | 5.9 MB     00:05     
    epel_1/other_db                                                                               | 2.9 MB     00:02     
    Metadata Cache Created

yum的命令使用

yum命令用于管理程序包和程序包组

yum [options] [command] [package ...]
    options:
            -y, --assumeyes         #全部选y
            -q, --quiet             #运行不输出
            --disablerepo=repoidglob    #临时不启用此仓库
            --enablerepo=repoidglob     #临时使用此仓库
            --nogpgcheck                #不检查包完整性和安全性
            --disableplugin=plugins     #不启用此插件
            --noplugins                 #不启用插件

    command:

        安装:
            install package1 [package2] [...]           #安装程序包
            localinstall rpmfile1 [rpmfile2] [...]      #本地安装rpm包,在安装不属于仓库的rpm包时,也能为其解决依赖关系
            localupdate rpmfile1 [rpmfile2] [...]       #本地更新rpm包
            reinstall package1 [package2] [...]         #重新安装

            [[email protected] ~]# yum install -y httpd    ///安装httpd程序
            Loaded plugins: fastestmirror, refresh-packagekit, security       <--yum的插件
            Loading mirror speeds from cached hostfile    
            Setting up Install Process
            Resolving Dependencies               <--检查依赖关系
            --> Running transaction check
            ---> Package httpd.x86_64 0:2.2.15-29.el6.centos will be installed
            --> Finished Dependency Resolution

            Dependencies Resolved

            =========================================================================================================================================================
             Package                         Arch                             Version                                        Repository                         Size
            =========================================================================================================================================================
            Installing:
             httpd                           x86_64                           2.2.15-29.el6.centos                           local_1                           821 k

            Transaction Summary
            =========================================================================================================================================================
            Install       1 Package(s)

            Total download size: 821 k
            Installed size: 2.9 M
            Downloading Packages:
            Running rpm_check_debug
            Running Transaction Test
            Transaction Test Succeeded
            Running Transaction
              Installing : httpd-2.2.15-29.el6.centos.x86_64                                                                                                     1/1 
              Verifying  : httpd-2.2.15-29.el6.centos.x86_64                                                                                                     1/1

            Installed:
              httpd.x86_64 0:2.2.15-29.el6.centos

            Complete!


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

            [[email protected] ~]# yum remove -y httpd
            Loaded plugins: fastestmirror, refresh-packagekit, security
            Setting up Remove Process
            Resolving Dependencies
            --> Running transaction check
            ---> Package httpd.x86_64 0:2.2.15-29.el6.centos will be erased
            --> Finished Dependency Resolution

            Dependencies Resolved

            =========================================================================================================================================================
             Package                        Arch                            Version                                          Repository                         Size
            =========================================================================================================================================================
            Removing:
            httpd                          x86_64                          2.2.15-29.el6.centos                             @local_1                          2.9 M

            Transaction Summary
            =========================================================================================================================================================
            Remove        1 Package(s)

            Installed size: 2.9 M
            Downloading Packages:
            Running rpm_check_debug
            Running Transaction Test
            Transaction Test Succeeded
            Running Transaction
              Erasing    : httpd-2.2.15-29.el6.centos.x86_64                                                                                                     1/1 
              Verifying  : httpd-2.2.15-29.el6.centos.x86_64                                                                                                     1/1

            Removed:
              httpd.x86_64 0:2.2.15-29.el6.centos

            Complete!

        更新:  
            update [package1] [package2] [...]          #更新程序包
            check-update                                #检查能否更新($?的返回值为100,有更新的程序包;返回值为0,没有更新的程序包;返回值为1,有错误发生)
            downgrade package1 [package2] [...]         #程序包降级


            [[email protected] ~]# yum check-update               ///检查有哪些可更新的程序包
            Loaded plugins: fastestmirror, refresh-packagekit, security
            Loading mirror speeds from cached hostfile
            Obsoleting Packages
            ibus-table-chinese-cangjie.noarch                                1.4.6-1.el6                                     epel_1                                  
                ibus-table-cangjie.noarch                                    1.2.0.20100210-1.el6                            @anaconda-CentOS-201311272149.x86_64/6.5
            ...
            [[email protected] ~]# yum update ibus-table-chinese-cangjie.noarch   -y          ///更新其中程序包
            Loaded plugins: fastestmirror, refresh-packagekit, security
            Loading mirror speeds from cached hostfile
            Setting up Update Process
            Resolving Dependencies
            --> Running transaction check
            ---> Package ibus-table-cangjie.noarch 0:1.2.0.20100210-1.el6 will be obsoleted
            ---> Package ibus-table-chinese-cangjie.noarch 0:1.4.6-1.el6 will be obsoleting
            --> Processing Dependency: ibus-table-chinese = 1.4.6-1.el6 for package: ibus-table-chinese-cangjie-1.4.6-1.el6.noarch
            --> Running transaction check
            ---> Package ibus-table-chinese.noarch 0:1.4.6-1.el6 will be installed
            --> Finished Depende

            Dependencies Resolved
            ...

            注意:当遇到以下错误时,添加--nogpgcheck可解决此次安装问题,如需永久解决,给对应仓库加gpgkey或修改gpgcheck为0.
            warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
            Public key for ibus-table-chinese-cangjie-1.4.6-1.el6.noarch.rpm is not installed


         查询:
            list [...]  
                         yum list available [glob_exp1] [...]       #查询仓库中能安装的程序包
                         yum list updates [glob_exp1] [...]         #查询仓库中能更新的程序包
                         yum list installed [glob_exp1] [...]       #查询仓库已经安装的程序包
                         yum list extras [glob_exp1] [...]          #查询安装在系统但不在仓库中的程序包
                         yum list obsoletes [glob_exp1] [...]       #查询已安装程序包的旧版本
            info [...]                                              #查询程序包的信息
            provides | whatprovides feature1 [feature2] [...]       #根据特性查询由何程序包提供(特性也可是文件)
            search string1 [string2] [...]                          #根据指定strings查询程序包的names and summaries
            deplist package1 [package2] [...]                       #查询此程序包的所有依赖关系
            repolist [all|enabled|disabled]                         #查询或生成配置的仓库列表
            history  [info|list|packages-list|packages-info|summary|addon-info|redo|undo|roll-back|new|sync|stats]

            #查询用户执行yum历史记录

            [[email protected] ~]# yum list available|head                    ///列出可使用程序包
            Loaded plugins: fastestmirror, refresh-packagekit, security
            Loading mirror speeds from cached hostfile
            Available Packages
            2048-cli.x86_64                             0.9.1-1.el6                  epel_1 
            2048-cli-nocurses.x86_64                    0.9.1-1.el6                  epel_1 
            2ping.noarch                                3.2.1-2.el6                  epel_1 
            389-admin.i686                              1.1.35-1.el6                 epel_1 
            389-admin.x86_64                            1.1.35-1.el6                 epel_1 
            389-admin-console.noarch                    1.1.8-1.el6                  epel_1 
            389-admin-console-doc.noarch                1.1.8-1.el6                  epel_1

            [[email protected] ~]# yum list httpd*            ///列出以httpd开头的所有程序包
            Loaded plugins: fastestmirror, refresh-packagekit, security
            Loading mirror speeds from cached hostfile
            Installed Packages      <--已经安装的包
            httpd-tools.x86_64  
            2.2.15-29.el6.centos                       @centos
            Available Packages    <--可安装的包
            httpd.x86_64              2.2.15-29.el6.centos           local_1
            httpd-devel.i686          2.2.15-29.el6.centos           local_1
            ...

            *** yum whatprovides "*/COMMAND"     可以用来查询输入命令由哪个程序包生成

            [[email protected] ~]# yum whatprovides "*/createrepo"
            Loaded plugins: fastestmirror, refresh-packagekit, security
            Loading mirror speeds from cached hostfile
            createrepo-0.9.9-18.el6.noarch : Creates a common metadata repository    <--此处为程序包名
            Repo        : local_1
            Matched from:
            Filename    : /usr/share/createrepo
            Filename    : /usr/bin/createrepo
            Filename    : /usr/lib/python2.6/site-packages/createrepo

            [[email protected] ~]# yum deplist httpd              ///查询httpd程序包的依赖于哪些软件
            Loaded plugins: fastestmirror, refresh-packagekit, security
            Loading mirror speeds from cached hostfile
            Finding dependencies: 
            package: httpd.x86_64 2.2.15-29.el6.centos
            dependency: /usr/sbin/useradd                    <--依赖的文件
             provider: shadow-utils.x86_64 2:4.1.4.2-13.el6  <--提供依赖文件的程序包
            dependency: libz.so.1()(64bit)
             provider: zlib.x86_64 1.2.3-29.el6
            dependency: system-logos >= 7.92.1-1
             provider: redhat-logos.noarch 60.0.14-12.el6.centos
            ...

            [[email protected] ~]# yum history                ///显示用户使用yum的操作历史,查询不算在此
            Loaded plugins: fastestmirror, refresh-packagekit, security
            ID     | Login user               | Date and time    | Action(s)      | Altered
            -------------------------------------------------------------------------------
                 5 | root <root>              | 2016-12-07 14:06 | Install        |    1   
                 4 | root <root>              | 2016-12-05 01:11 | Install        |    4   
                 3 | root <root>              | 2016-11-29 18:59 | Install        |    1   
            history list
            [[email protected] ~]# yum history info 3                 ///查看历史操作详细信息
            Loaded plugins: fastestmirror, refresh-packagekit, security
            Transaction ID : 3
            Begin time     : Tue Nov 29 18:59:51 2016
            Begin rpmdb    : 958:baaf47e48562c6ec3caea4724ad15a93040cea94
            End time       :            18:59:52 2016 (1 seconds)
            End rpmdb      : 959:6a212fd01507b473b7b2abbf09e8cf98281e3411
            User           : root <root>
            Return-Code    : Success
            Command Line   : install tree -y                    ///使用的命令
            Transaction performed with:
                Installed     rpm-4.8.0-37.el6.x86_64                       @anaconda-CentOS-201311272149.x86_64/6.5
                Installed     yum-3.2.29-40.el6.centos.noarch               @anaconda-CentOS-201311272149.x86_64/6.5
                Installed     yum-plugin-fastestmirror-1.1.30-14.el6.noarch @anaconda-CentOS-201311272149.x86_64/6.5
            Packages Altered:
                Install tree-1.5.3-2.el6.x86_64 @centos
            history info

            [[email protected] ~]# yum repolist all    ///显示配置好的仓库信息,如需缓存,自动下载
            Loaded plugins: fastestmirror, refresh-packagekit, security
            Loading mirror speeds from cached hostfile
            repo id                 repo name                     status
            local_1                 local iso                     enabled:  6,367
            epel_1                  epel source                   enabled: 12,199
            repolist: 18,566

        缓存: 
            clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]    #清楚各种各样由yum产生的缓存
            makecache                       #下载可以使用的仓库的元数据文件


            [[email protected] ~]# yum clean all      ///清除所有缓存
            Loaded plugins: fastestmirror, refresh-packagekit, security
            Cleaning repos: local_1 epel_1
            Cleaning up Everything
            Cleaning up list of fastest mirrors
            [[email protected] ~]# yum makecache                  ///生成缓存,下载或加载元数据
            Loaded plugins: fastestmirror, refresh-packagekit, security
            Determining fastest mirrors
            local_1                                       | 4.0 kB     00:00 ... 
            local_1/group_gz                              | 220 kB     00:00 ... 
            local_1/filelists_db                          | 5.8 MB     00:00 ... 
            local_1/primary_db                            | 4.4 MB     00:00 ... 
            local_1/other_db                              | 2.7 MB     00:00 ... 
            epel_1                                        | 4.3 kB     00:00     
            epel_1/filelists_db                           | 7.8 MB     00:07     
            epel_1/primary_db                             | 5.9 MB     00:05     
            epel_1/other_db                               | 2.9 MB     00:02     
            Metadata Cache Created


        检查:
            check       #检查本地rpmdb和提供的一些信息

        包组command:
             groupinstall group1 [group2] [...]                 #包组安装
             groupupdate group1 [group2] [...]                  #包组更新
             grouplist [hidden] [groupwildcard] [...]           #显示包组信息(使用hidden选项,显示所有被隐藏的包组)
             groupremove group1 [group2] [...]                  #卸载包组
             groupinfo group1 [...]                             #查询包组信息

            安装CentOS6的开发组件:
            # yum groupinstall  "Development tools" "Server Platform Development" -y

yum中常见问题:

         解决没有repodata目录的方法
            1 使用createrepo命令创建
                createrepo [options] <directory>

                       -c --cachedir <path>    指定需要缓存的rpm包路径

            2 将rpm包复制到能正常使用仓库的Package目录中,重新生成元数据文件
                # yum  clean all; yum makecache

            3 将其它能正常配置的仓库中的repodata目录中文件复制一份

         当用yum卸载程序包时,会出现此类错误
         Error: Trying to remove "yum", which is protected

         其原因为:
         此类包要卸载的依赖项有yum自身的文件或是正在被yum所使用

         解决办法:
         忽略依赖关系进行单独卸载其提示的包.

         本地镜像DVD1跟DVD2是什么:
          CentOS-6.8-x86_64-bin-DVD1.iso 
          CentOS-6.8-x86_64-bin-DVD2.iso 
          由于一个光盘最多只能刻制4G左右大小,而centos的仓库包有十分的多,因此将常用的包以及安装系统的包放在DVD1中,DVD2中的包算是扩展包.

         当执行yum命令时出现以下报错,代表yum被其他进程所占用,需要等待其它执行中的yum命令完成.
            Existing lock /var/run/yum.pid: another copy is running as pid 2000.
            Another app is currently holding the yum lock; waiting for it to exit...

         当遇到如下问题:
         其原因:
            仓库的包太旧,安装的软件包版本太低
         解决办法:
            更新仓库或者卸载新版本的包,再继续安装此包

技术分享


本文出自 “12064120” 博客,请务必保留此出处http://12074120.blog.51cto.com/12064120/1886767

以上是关于yum详解的主要内容,如果未能解决你的问题,请参考以下文章

详解Yum本地网络源配置,源代码安装过程和出现的安装问题

(转) Java中的负数及基本类型的转型详解

详解Android WebView加载html片段

YUM仓库配置及命令详解

14.VisualVM使用详解15.VisualVM堆查看器使用的内存不足19.class文件--文件结构--魔数20.文件结构--常量池21.文件结构访问标志(2个字节)22.类加载机制概(代码片段

Python中verbaim标签使用详解