在上一篇博客《Linux软件包的管理--RPM包管理器》中详细讲述了RPM,但是存在一个问题?就是RPM不能自动解决软件包之间的依赖性。所以就出现了增强版的RPM管理器-YUM。

    以下讲解以CentOS6系列为例。

一、YUM介绍

    YUM(Yellowdog Updater Modified)是一种C/S架构,它为什么就能自动解决依赖性关系呢?这取决于 YUM 服务端的RPM仓库,在这个仓库里放着我们经常用到的rpm格式的软件包。

1、yum仓库的组成

组成 内容
数据
存放各个rpm包
元数据

包名、版本信息、依赖关系、包分组信息、依赖关系列表。

在centos6中遵循sqlite格式,生成的元数据目录是repodata。

2、yum客户端如何访问服务端

    以升级为例:

3、如何配置yum的服务端和客户端

1)在服务器端

    首先:在服务器端要确保有可用的yum仓库。yum仓库其实就是文件服务器。repodata目录所在的父目录就是一个yum仓库。

    其次:服务器端必须通过某种手段将yum仓库共享出去,可以让客户端使用,常见的共享方式有:ftp,http,nfs。如果是本地使用(客户端和服务端是同一台机器),也可以使用file方式。

2)yum客户端

    提供repo配置文件,指明仓库访问路径及各种属性。主配置文件(中心配置文件):/etc/yum.conf。 

# 在这里定义一些默认配置属性和全局配置属性[main]cachedir=/var/cache/yum/$basearch/$releaseverkeepcache=0debuglevel=2logfile=/var/log/yum.logexactarch=1obsoletes=1gpgcheck=1plugins=1installonly_limit=5bugtracker_url=http://bugs.centos.org/set_project.php?project_id=16&ref=http://bugs.centos.org/bug_report_page.php?category=yumdistroverpkg=centos-release

    一个后几个相关的仓库的配置保存成一个文件,文件名都以.repo结尾,保存在/etc/yum.repo.d目录中。

    下面介绍一下文件格式(以163镜像为例):

[base]  # 指明仓库名称,用户可自行定义name=CentOS-$releasever - Base - 163.com # 一般是对这个仓库名称的说明# 最关键的是这一项,指明仓库的访问路径# 为了配置方便,yum引入了变量,yum配置文件中可用的四个变量:# releasever: 程序的版本,对Yum而言指的是redhat-release版本;只替换为主版本号,如RedHat 6.5,则替换为6; # arch: 系统架构# basearch: 系统基本架构,如i686,i586等的基本架构为i386; # YUM0-9: 在系统中定义的环境变量,可以在yum中使用;# 最常用的就是 releasever 和 basearch# 下面在访问的时候会被替换成:baseurl=# baseurl可以使用多个:例如# baseurl=#         # 使用多个baseurl时需要用到以上格式  baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/# mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os# baseurl和mirrlist通常只需一个,我们一般配置baseurlgpgcheck=1  # 是否进行gpg检测(包的完整性和合法性)gpgkey= # gpg检测是的秘钥文件# 还有其他选型,读者在配置时,可自行选择# 常用的还有:# enabled={0|1} 是否启用该仓库# cost=N 配置仓库的”花费“,等价于优先级。N的值越小,优先级越高。

    配置好这些准备工作,就可以使用yum工具了。

三、yum工具的使用

    这里以互联网163镜像为例演示。

1、列出所有可用repo

    yum repolist {enabled|disabled|all}

# yum repolist {enabled|disabled|all}# enabled 列出可用的 # disabled 不可用的 # all所有的[root@server yum.repos.d]# yum repolistLoaded plugins: fastestmirror, refresh-packagekit, securityLoading mirror speeds from cached hostfilebase                                                       | 3.7 kB     00:00     extras                                                     | 3.4 kB     00:00     updates                                                    | 3.4 kB     00:00     repo id                                repo name                            statusbase                                  CentOS-6 - Base - 163.com              6,367extras                                CentOS-6 - Extras - 163.com            14updates                               CentOS-6 - Updates - 163.com           1,153repolist: 7,534

2、列出rpm包

    yum list {all|installed|available}

# yum list {all|installed|available}# all 列出所有的包  等价于  yum list # installed 列出已安装的包# available 列出可用的包(不包括已安装的)# 一般情况下,会这样使用[root@server yum.repos.d]# yum list installed | grep "http"httpd.x86_64            2.2.15-26.el6.centoshttpd-tools.x86_64      2.2.15-26.el6.centos

3、包的描述信息

    yum info package_name

Loading mirror speeds from cached hostfileInstalled PackagesName        : httpdArch        : x86_64Version     : 2.2.15Release     : 26.el6.centosSize        : 2.9 MRepo        : installedFrom repo   : anaconda-CentOS-201303050102.x86_64Summary     : Apache HTTP ServerURL         : http://httpd.apache.org/License     : ASL 2.0Description : The Apache HTTP Server is a powerful, efficient, and extensible            : web server.Available PackagesName        : httpdArch        : x86_64Version     : 2.2.15Release     : 30.el6.centosSize        : 821 kRepo        : updatesSummary     : Apache HTTP ServerURL         : http://httpd.apache.org/License     : ASL 2.0Description : The Apache HTTP Server is a powerful, efficient, and extensible            : web server.

4、列出所有的包组信息

    yum grouplist

# 经常情况下我们会是这样来使用[root@server yum.repos.d]# yum grouplist | grep "Deve"   Additional Development   Desktop Platform Development   Development tools   Server Platform Development

5、显示包组的信息:例如组中包含的程序包列表

    yum groupinfo "GROUP NAME"

[root@server yum.repos.d]# yum groupinfo "Development tools"Loaded plugins: fastestmirror, refresh-packagekit, securitySetting up Group ProcessLoading mirror speeds from cached hostfileGroup: Development tools Description: A basic development environment. Mandatory Packages:   autoconf   automake   binutils   bison   flex   gcc   gcc-c++   gettext   libtool   make   patch   pkgconfig   redhat-rpm-config   rpm-build Default Packages:   byacc   cscope   ctags   cvs   diffstat   doxygen   elfutils   gcc-gfortran   git   indent   intltool   patchutils   rcs   subversion   swig   systemtap Optional Packages:   ElectricFence   ant   babel   bzr   chrpath   cmake   compat-gcc-34   compat-gcc-34-c++   compat-gcc-34-g77   cvs-inetd   dejagnu   expect   gcc-gnat   gcc-java   gcc-objc   gcc-objc++   imake   jpackage-utils   kdewebdev   ksc   libstdc++-docs   mercurial   mod_dav_svn   nasm   perltidy   python-docs   rpmdevtools   rpmlint   systemtap-sdt-devel   systemtap-server

6、清理缓存

    yum clean {all|packages|metadata|expire-cache|rpmdb|plugins}

# yum clean {all|packages|metadata|expire-cache|rpmdb|plugins}# all 所有缓存# packages   清除包缓存# metedata   清除元数据缓存# expire-cache 清除过期缓存# rpmdb    清除RPM数据库缓存# plufins 清除插件缓存# 最常用的就是 yum clean all[root@server yum.repos.d]# yum clean allLoaded plugins: fastestmirror, refresh-packagekit, securityCleaning repos: base extras updatesCleaning up EverythingCleaning up list of fastest mirrors

7、安装程序包

    yum install package_name

yum install httpd -y # 常用选项是 -y 意思是不需要确认,直接安装

  

  重新安装:

    yum reinstall package_name

yum reinstall httpd -y

8、升级

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

    yum update package_name 升级软件

[root@server yum.repos.d]# rpm -q ntpntp-4.2.6p5-1.el6.centos.x86_64

    

    yum downgrade package_name 降级使用的,退回原来的版本

# yum downgrade ntp -y 降级第一次可能会出现错误,解决办法之一是:yum upgrade -y 升级yum工具[root@server yum.repos.d]# yum downgrade ntp -yLoaded plugins: fastestmirror, refresh-packagekit, securitySetting up Downgrade ProcessLoading mirror speeds from cached hostfileOnly Upgrade available on package: ntp-4.2.6p5-1.el6.centos.x86_64Nothing to do

9、卸载

    yum remove|erase package_name

yum remove httpd -y

10、查询某文件是由哪个包安装生成的

    yum whatprovides|provides /path/to/somefile

# 会列出所有的提供此配置文件的包[root@server yum.repos.d]# yum provides /etc/httpd/conf/httpd.conf Loaded plugins: fastestmirror, refresh-packagekit, securityLoading mirror speeds from cached hostfilehttpd-2.2.15-30.el6.centos.x86_64 : Apache HTTP ServerRepo        : updatesMatched from:Filename    : /etc/httpd/conf/httpd.confhttpd-2.2.15-29.el6.centos.x86_64 : Apache HTTP ServerRepo        : baseMatched from:Filename    : /etc/httpd/conf/httpd.confhttpd-2.2.15-30.el6.centos.x86_64 : Apache HTTP ServerRepo        : installedMatched from:Other       : Provides-match: /etc/httpd/conf/httpd.conf

11、安装包组

    yum groupinstall "GROUP NAME"

# 常用的包有:# Desktop Platform Development 开发平台# Development tools  开发所用的包# 例如:yum groupinstall "Development tools" -y

12、卸载包组

    yum groupremove "GROUP NAME"

四、自制yum仓库

1、如果作为个人使用的话,最简单的办法是,挂载光盘镜像使用。

    以VMware虚拟机为例:

    先关联光盘镜像,挂载光盘镜像就可以使用。

    其中:.repo中的baseurl使用file就可。例如:baseurl=

2、自建yum仓库服务器

    以ftp为例,步骤:

(1) 安装ftp程序,并启动服务

    # rpm -ivh vsftp-版本号

    或者

    # yum install vsftpd -y

    启动服务

    # service vsftpd start

    # chkconfig vsftpd on

(2) ftp的共享目录为/var/ftp/pub

    创建子目录,存放某相关的所有rpm包(建议)

(3) 为仓库生成元数据文件,以使能够作为仓库使用

    # rpm -ivh createrepo-版本号

    或者

    # yum install createrepo -y

    # createrepo /path/to/rpm_repo/  生成repo的元数据目录 例如:createrepo /var/ftp/pub/Packages   

(4) 配置yum客户端使用此仓库即可

    总结:本文主要介绍yum工具的使用,简单介绍了自制yum服务端的步骤。