Linux程序包管理yum

1.  Yum是什么?

yellowdogupdate modifier = yum

2.  为什么要使用yum?

yum:自行解决依赖关系

3.  Yum C/S架构:

4.  Yum的工作方式

要使用yum,必须的有yum仓库,在yum仓库服务端的某个路径中存放有程序包,使用createrepo命令构建这些程序包的元数据保存在repodata的目录下,yum客户端在首次启动时,会从yum服务上下载元数据到本地进行缓存,以后yum客户端在启动时,首先会去服务端检查yum元数据的特征码和本地缓存的yum的元数据的特征码做比较,如特征码一致则使用本地的元数据,如特征码不一致则从yum服务器端下载元数据到本地进行缓存

Yum仓库的使用依赖于文件共享的文件服务服务器;常见的文件服务的服务器:1.ftp服务

2 .http服务 3 .本地文件目录4 .NFS服务

rpm为基础包管理器;yum则是rpm的前端工具,yum的配置文件指向仓库指路径:repodata目录所在的父目录

 

5.yum配置文件

配置文件:指向仓库的位置以及相关的各种配置信息;每个yum命令行可以同时指向多个仓库,仓库间可以优先级等相关的配置;

 

yum配置文件有两部分组成:

主配置文件:/etc/yum.conf ;为各仓库指向提供公共配置文件;

各仓库的定义:/etc/yum.repos.d/*.repo ;

注意! : /etc/yum.repos.d/目录下文件以.repo结尾;每个repo配置文件中可以配置一至多个仓库;

 

6.  /etc/yum.conf文件

directive=VALUE

[main]:提供公共配置信息

cachedir=/var/cache/yum/$basearch/$releasever<--缓存目录;

keepcache=0<--是否缓存已下载的rpm包 0:不缓存,安装完成以后删除rpm包;1 : 缓存

debuglevel=2<--调试级别;级别越高输出的信息越详细

logfile=/var/log/yum.log<--日志文件存放位置

exactarch=1<--下载的程序包的平台是否与当前平台一致

obsoletes=1是否检测包是否已被废弃

gpgcheck=1<--在安装前是否检查每一个程序包的来源合法性;包完整性

plugins=1<--是否支持插件

installonly_limit=5<--限制每一次安装的程序包个数

bugtracker_url=http://bugs.centos.org/set_project.php?project_id=19&ref=http://bugs.centos.org/bug_report_page.php?category=yum<--bug追踪的路径

distroverpkg=centos-release

 

….

#in /etc/yum.repos.d

7.  各仓库配置

[repositoryid]<--对于当前系统的yum来讲,此repositoryid用于惟一标识此repository指向,因此,其必须惟一;

name=<--当前仓库描述信息;

baseurl=url://path/to/repository/<--指明repository的访问路径;通常为一个文件服务器上输出的某repository;

url:

ftp服务: ftp://SERVER/PATH/TO/REPOSITORY

http服务: http://SERVER/PATH/TO/REPOSITORY

本地目录:: file:///PATH/TO/REPOSTIROY

 

enabled={1|0}此仓库是否可被使用:默认为1

gpgcheck={1|0} 是否对程序包做校验;默认为1

gpgkey=url://path/to/keyfile指明gpgkey文件路径;

cost=#指明当前repository的访问开销,默认为1000;

 

yum有内置变量,用于保存当前平台的相信息;

(1)$raleasever: 当前OS发行版的主版本号;

例如,对CentOS6.6 x86_64,主版本号为6;

(2)$arch:平台

i386,i486

(3)$basearch: 基础平台, 例如i686, i586,i486以及i386的基础平台同为i386;

(4)$YUM0-$YUM9

 

假如:

baseurl=http://repo.magedu.com/centos/$releasever/$basearch

当前系统为CentOS6.6 x86_64

http://repo.magedu.com/centos/6/x86_64

创建yum仓库

如何使用发行版光盘作为yumrepository

(1)挂载光盘到指定挂载点,如/media/cdrom

(2)新建yum repo配置文件,类似如下所示:

[dvdbase]

name=CentOS6.6 X86_64 on dvd1

baseurl=file:///media/cdrom

gpgcheck=0

enabled=1      

 

自己创建yum仓库:

(1)在基于发行版安装树安装 createrepo 程序包;

(2)确定repository输出方式:

    本地输出:提供一个放置rpm包的本地路径;

    网络输出:提供一个文件服务器,配置好ftp服务或http服务;

ftp服务:

安装程序包:vsftpd

文件存放路径:/var/ftp/pub

启动服务:servicevsftpd start

http服务:

安装程序包:httpd

文件存放路径:/var/www/html

启动服务:servicehttpd start

 

Note:确保防火墙服务关闭;

 

(3)在准备好的目录中放置rpm程序包文件

(4)对此目录运行createrepo命令即可

 

示例

[[email protected]]# cat centos6.6.repo

[base]

name=CentOS$releasever $basearch on local server 172.16.0.1

baseurl=http://172.16.0.1/cobbler/ks_mirror/CentOS-6.6-$basearch/

enabled=1

gpgcheck=0

 

[extra]

name=CentOS$releasever $basearch extras

baseurl=http://172.16.0.1/centos/$releasever/extras/$basearch/

enabled=1

gpgcheck=0

 

[epel]

name=FedoraEPEL for CentOS$releasever $basearch on local server 172.16.0.1

baseurl=http://172.16.0.1/fedora-epel/$releasever/$basearch/

enabled=1

gpgcheck=0

 

yum命令的使用格式

yum[options] [command] [package ...]

 

yum仓库管理:

repolist:列出已经配置的所有可用仓库   

repolist[all|enabled|disabled]

列出默认已经启用的仓库

[root@localhost ~]# yumrepolist  <--默认情况下会列出以启用的仓库 等同于yumrepolist enabled

已加载插件:fastestmirror,refresh-packagekit, security

Loading mirror speeds fromcached hostfile

仓库标识                         仓库名称                                                                         状态

base                             CentOS 6 x86_64 on localserver 172.16.0.1                                         4,184

epel                             Fedora EPEL forCentOS6 x86_64 on local server 172.16.0.1                         12,922

extra                            CentOS 6 x86_64 extras                                                            2,334

repolist: 19,440

列出全部的repo仓库

[root@localhost ~]# yumrepolist all

已加载插件:fastestmirror,refresh-packagekit, security

Loading mirror speeds fromcached hostfile

仓库标识                         仓库名称                                                                   状态

C6.0-base                        CentOS-6.0 - Base                                                         禁用

C6.0-centosplus                  CentOS-6.0 - CentOSPlus                                                    禁用

...

C6.5-extras                      CentOS-6.5 - Extras                                                        禁用

C6.5-updates                     CentOS-6.5 - Updates                                                       禁用

base                             CentOS 6 x86_64 onlocal server 172.16.0.1                                  启用:  4,184

base-debuginfo                   CentOS-6 - Debuginfo                                          禁用

c6-media                         CentOS-6 - Media                                                           禁用

epel                             Fedora EPEL forCentOS6 x86_64 on local server 172.16.0.1                   启用: 12,922

extra                            CentOS 6 x86_64extras                                                     启用:  2,334

fasttrack                        CentOS-6 -fasttrack                                                       禁用

repolist: 19,440

yum缓存管理:

clean:清理缓存

clean[ packages | metadata | expire-cache | rpmdb | plugins | all ]

示例

清空所有的缓存

[root@localhost~]# yum clean all

已加载插件:fastestmirror, refresh-packagekit, security

Cleaningrepos: base epel extra

清理一切

Cleaningup list of fastest mirrors

makecache: 缓存创建

自动连接至每一个可用仓库,下载其无数据,将其创建为缓存;

示例

[root@localhost ~]# yummakecache

已加载插件:fastestmirror,refresh-packagekit, security

Determining fastest mirrors

base                                                                                              |3.2 kB     00:00    

base/group_gz                                                                                    | 216 kB     00:00    

base/filelists_db                                                                                 | 4.3MB     00:00    

base/primary_db                                                                                  | 3.3 MB     00:00    

base/other_db                                                                                    | 1.8MB     00:00    

epel                                                                                             | 3.0 kB     00:00    

epel/filelists_db                                                                                |  10 MB    00:01    

epel/primary_db                                                                                  | 7.2 MB     00:00    

epel/other_db                                                                                    | 4.3 MB     00:01    

extra                                                                                            | 2.9 kB     00:00    

extra/filelists_db                                                                               | 1.7 MB     00:00    

extra/primary_db                                                                                 | 1.1 MB     00:00    

extra/other_db                                                                                   | 965 kB     00:00    

元数据缓存已建立

yum程序包查看:

yumlist [all | glob_exp1] [glob_exp2] [...]

 

yumlist {available|updates|installed|extras|obsoletes} [glob_exp1] [...]

yum list [all | glob_exp1] [glob_exp2][...]

yum list available [glob_exp1] [...]

yum list updates [glob_exp1] [...]

yum list installed [glob_exp1] [...]

yum list extras [glob_exp1] [...]

yumgrouplist [hidden] [groupwildcard] [...]

示例

显示当前系统所有已安装和在仓库中未安装的软件包

# yumlist all

显示与mysql相关的软件包

#yum list *mysql* <--可使用glob通配符 * ?

显示当前系统上已经安装的软件包

#yum list installed

显示当前系统中已安装但在仓库中不提供的包

#yum list extras

列出当前系统可用包组

#yum grouplist

 

yum程序包安装:

yuminstall package1 [package2] [...]

Note:

1. 安装时只需要提供包名;

2.如果某包有在不同仓库中有多个不同版本,默认会安装最新版本;如果要安装指定版本:install PACKAGE-VERSION ...

例如:安装mysql

#yum install mysql -y

重新安装软件包(覆盖安装)

reinstallpackage1 [package2] [...]

例如 :

#yum reinstall mysql -y

程序包升级:

update[package1] [package2] [...]

示例

#yum update mysql php

程序包降级:

downgradepackage1 [package2] [...]

例如

#yum downgrade mysql php -y

检查有哪些升级可用:

check-update

例如

#yum check-update

程序包卸载:

remove| erase package1 [package2] [...]

 

所有依赖于正卸载的程序包的程序包会被一并卸载;

例如

#yum remove mysql php

查询程序包

查询程序的相关简要信息:info PACKAGE ...

[root@localhost~]# yum info mysql

已加载插件:fastestmirror, refresh-packagekit, security

Loadingmirror speeds from cached hostfile

已安装的软件包

Name        : mysql

Arch        : x86_64

Version     : 5.1.73

Release     : 3.el6_5

Size        : 2.4 M

Repo        : installed

Fromrepo   : base

Summary     : MySQL client programs and sharedlibraries

URL         : http://www.mysql.com

License     : GPLv2 with exceptions

Description: MySQL is a multi-user, multi-threaded SQL database server. MySQL is a

            : client/server implementationconsisting of a server daemon (mysqld)

            : and many different clientprograms and libraries. The base package

            : contains the standard MySQLclient programs and generic MySQL files.

在包名和sumary信息中搜索指定的关键字:searchKEYWORD ...

[root@localhost~]# yum search mysql

已加载插件:fastestmirror, refresh-packagekit, security

Loadingmirror speeds from cached hostfile

==================================================N/S Matched: mysql ===================================================

MySQL-python.x86_64: An interface to MySQL

MySQL-zrm.noarch: MySQL backup manager

apr-util-mysql.x86_64: APR utility library MySQL DBD driver

asterisk-mysql.x86_64: Applications for Asterisk that use MySQL

bacula-director-mysql.x86_64: Bacula Director with MySQL database support

bacula-storage-mysql.x86_64: MySQL Bacula storage daemon files

bytefx-data-mysql.x86_64: MySQL database connectivity for Mono

collectd-mysql.x86_64: MySQL module for collectd

dmlite-plugins-mysql.x86_64: MySQL plugin for dmlite

dovecot-mysql.x86_64: MySQL back end for dovecot

dpm-copy-server-mysql.x86_64: DPM copy server with MySQL database back-end

查询指定文件由哪个程序包安装生成:provides|whatprovides /PATH/TO/SOMEFILE

例如

[root@localhost~]# yum provides /etc/fstab

已加载插件:fastestmirror, refresh-packagekit, security

Loadingmirror speeds from cached hostfile

setup-2.8.14-20.el6_4.1.noarch: A set of system configuration and setup files

Repo        : base

匹配来自于:

Filename    : /etc/fstab

 

 

 

setup-2.8.14-20.el6_4.1.noarch: A set of system configuration and setup files

Repo        : installed

匹配来自于:

Other       : 提供依赖满足:/etc/fstab

安装或升级本地的程序包文件

用于安装仓库中并不存在的程序包文件,这些程序包有可能又依赖于仓库中的某些程序包;

安装本地的程序包文件

localinstallrpmfile1 [rpmfile2] [...]

示例

#yum localinstall zsh-4.3.10-7.el6.x86_64.rpm

#yum install  zsh-4.3.10-7.el6.x86_64.rpm

升级本地的程序包文件

示例

localupdaterpmfile1 [rpmfile2] [...]

#yum localupdate zsh-4.3.10-9.el6.x86_64.rpm

包组管理:

列出所有包组:grouplist

显示指定包组详情:groupinfo group1 [...]

示例

[root@localhost~]# yum groupinfo '大系统性能'

已加载插件:fastestmirror, refresh-packagekit, security

设置组进程

Loadingmirror speeds from cached hostfile

 

组:大系统性能

 描述:大系统的性能支持工具

 必要的软件包:

   numactl

   numad

 默认的软件包:

   hwloc

   libcgroup

   libhugetlbfs

   libhugetlbfs-utils

   libtopology

 可选的软件包:

   ksm_preload

   numatop

   tuna

 

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

 

卸载包组:groupremove group1 [group2][...]

 

升级包组:groupupdate group1 [group2][...]

 

在centos 7 上: 可直接用install、remove或update来管理包组:

@GROUP_NAME

示例:

yuminstall @"Server Platform Development"

 

yum命令的可用选项:

-y:自动回答为“yes”

--disablerepo=:临时禁用在配置文件中配置并启用的某repository;

--enablerepo=:临时启用指定的某repo;

--nogpgcheck:禁止做包校验;