Linux系统之前端程序包管理器-yum
【前言】
当我们使用rpm命令安装程序时,时常会因为程序包之间的依赖关系而焦头烂额,使得很多程序的安装过程复杂而费力,那有没有一种工具可以帮我们解决这个问题呢?当然有啦!这就是我们今天的重点yum,让我们一块来学习总结下吧!
【什么是yum】
yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器。基於RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软体包,无须繁琐地一次次下载、安装。yum提供了查找、安装、删除某一个、一组甚至全部软件包的命令,而且命令简洁而又好记。
【yum的工作机制】
yum 的关键之处是要有可靠的yum仓库(yum repository),它可以是http 或ftp 站点,也可以是本地软件池,其必须包含两类数据:
1、数据:也就是各个rpm包;
2、元数据:其包含了rpm 包的各种信息,包括包名、描述,功能,提供的文件,依赖关系、包分组信息等。正是通过收集汇总这些元数据 并加以分析,才能自动化地完成余下的任务。在各版本系统下其的存储格式为:CentOS5: *.xml; centos6,7: *.sqlite
其实从架构的角度来看,yum的工作机制为C/S架构,如下图所示:
【yum的使用机制】
1、确保有yum repo(yum仓库的配置文件)可用;rpm包的文件服务器,repodata目录所在父目录就是一个可用仓库。
2、yum客户端需要指供repo配置文件,指明yum仓库访问路径及各种属性信息。有两种配置方法:
法一:修改主配置文件(中心配置文件):/etc/yum.conf;
法二:在/etc/yum.repos.d/下创建或修改*.repo文件(存储一个或几个相关仓库的配置信息)。
在.repo文件定义一个yum repo指向的格式:
[REPOID] #定义软件源的名称,该名称是可以自定义,同时在该服务器上所有repo文件中是唯一的。注意[]中不能有空格;
name=Some name for this repository #定义软件仓库的名称,支持像$releasever 、$basearch这样的变量;注意=两边不能有空格;
baseurl=url://server/path/to/repository #指定真正的仓库所在地,可以给定多个,下面的URL不能顶格书写,URL中也支持$releasever 、$basearch这样的变量;
http://mirrors.sohu.com/centos/$releasever/os/$basearch
enabled={0|1} #配置此仓库是否启用,默认为1生效;
gpgcheck={0|1} #配置是否验证安装包,0为不进行验证,1为进行验证;
gpgkey=URL #指定用于验证的秘钥文件
cost={1..n} #默认为1000,指定访问此仓库的开销(在客户端指定)
补充说明:
1、$basearch: 系统基本架构,如i686,i586等的基本架构为i386;
$releasever: 程序的版本,对yum而言指的是redhat-release版本;只替换为主版本号,如RedHat 6.5,则替换为6;
2 、baseurl种url支持的协议有:http://、ftp://、nfs://、file://、 rsync://;当需要指定多个URL且不想通过baseurl来指定时, 我们可以把多个URL存放在一个文件里,通过mirrorlist=存放URL的文件来指定yum仓库。
3、一个.repo文件可以定义多个yum仓库。
[root@CentOS6 yum.repos.d]# ls /etc/yum.repos.d/
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
[root@CentOS6 mnt]# cat /etc/yum.repos.d/CentOS-Media.repo
[c6-media]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
file:///media/cdrom/
file:///media/cdrecorder/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
[root@CentOS6 yum.repos.d]#
【yum客户端命令的使用】
1、列出所有可用repo
yum repolist {enabled|disabled|all} enabled:可用的;disabled:不可用的;all:所有的 ,不带参数默认为列出可用的
[root@CentOS6 yum.repos.d]# yum repolist enabled
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
repo id repo name status
c6-cd CentOS-6 - cd 4,802
repolist: 4,802
[root@CentOS6 yum.repos.d]# yum repolist disabled
Loaded plugins: fastestmirror, refresh-packagekit, security
repo id repo name
c6-media CentOS-6 - Media
repolist: 0
[root@CentOS6 yum.repos.d]# yum repolist all
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
repo id repo name status
c6-cd CentOS-6 - cd enabled: 4,802
c6-media CentOS-6 - Media disabled
repolist: 4,802
[root@CentOS6 yum.repos.d]#
2、列出rpm包
yum list {all|installed|available} all:所有的;installed: 已安装的;available:可以安装的;默认为列出所有的
yum list KEYWORD* 支持使用文件名通配,来获取含有关键字的rpm包
[root@CentOS6 yum.repos.d]# yum list http* #列出所有以http开头的rpm包
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Installed Packages #下面为已经安装的包
httpd-tools.i686 2.2.15-29.el6.centos @anaconda-CentOS-201311271240.i386/6.5
Available Packages #下面为可以安装的包
httpd.i686 2.2.15-29.el6.centos c6-cd
httpd-devel.i686 2.2.15-29.el6.centos c6-cd
httpd-manual.noarch 2.2.15-29.el6.centos c6-cd
3、列出包的描述信息(无论包有没有安装都可以列出)
yum info package_name
[root@CentOS6 yum.repos.d]# yum info httpd #列出http包的描述信息
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Available Packages
Name : httpd
Arch : i686
Version : 2.2.15
Release : 29.el6.centos
Size : 828 k
Repo : c6-cd
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.
4、列出所有的包组信息
yum grouplist
[root@CentOS6 yum.repos.d]# yum grouplist
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Group Process
Loading mirror speeds from cached hostfile
c6-cd/group_gz | 212 kB 00:00 ...
Installed Groups: #已安装的包组
Additional Development
Base
...
Installed Language Groups: #已安装的语言包组
Arabic Support [ar]
...
Available Groups: #可安装的包组
Backup Client
...
Available Language Groups: #可安装的语言包组
Afrikaans Support [af]
...
Done
5、显示包组的信息:列出组中包含的程序包列表(如果包组名中含有空格必须用“”引起来)
yum groupinfo "GROUP NAME"
[root@CentOS6 yum.repos.d]# yum groupinfo "Development Tools" #显示开发工具包组的信息
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Group Process
Loading mirror speeds from cached hostfile
Group: Development tools
Description: A basic development environment.
Mandatory Packages:
autoconf
...
Default Packages:
byacc
...
Optional Packages:
ElectricFence
...
CentOS6 跟开发相关的包组有: Development Tools、Server Platform Development、 Desktop Platform Development
6、清理缓存
yum clean {all|packages|metadata|expire-cache|rpmdb|plugins}
yum clean all 清除缓存目录(/var/cache/yum)下的软件包以及旧的元数据
[root@CentOS6 yum.repos.d]# yum clean all
Loaded plugins: fastestmirror, refresh-packagekit, security
Cleaning repos: c6-cd
Cleaning up Everything
Cleaning up list of fastest mirrors
7、安装程序包
yum install package_name {-y} -y:自动回答为yes,执行过程中无需等待用户再次输入
[root@CentOS6 yum.repos.d]# yum install httpd
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
c6-cd | 4.0 kB 00:00 ...
c6-cd/primary_db | 3.4 MB 00:00 ...
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package httpd.i686 0:2.2.15-29.el6.centos will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=================================================================================
Package Arch Version Repository Size
=================================================================================
Installing:
httpd i686 2.2.15-29.el6.centos c6-cd 828 k
Transaction Summary
================================================================================
Install 1 Package(s)
Total download size: 828 k
Installed size: 2.8 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
** Found 1 pre-existing rpmdb problem(s), 'yum check' output follows:
gnome-user-share-2.28.2-3.el6.i686 has missing requires of httpd >= ('0', '2.2.0', None)
Installing : httpd-2.2.15-29.el6.centos.i686 1/1
Verifying : httpd-2.2.15-29.el6.centos.i686 1/1
Installed:
httpd.i686 0:2.2.15-29.el6.centos
Complete!
重新安装:
yum reinstall package_name
8、升级
yum check-update: 检查可用的升级包
yum update package_name 升级,当不指定版本时,默认升级到最新版本
yum downgrade package_name 降价到指定版本
9、卸载(卸载时为解决该包安装的依赖关系而安装的包也会一同卸载)
yum remove|erase package_name {-y} -y:自动回答为yes,执行过程中无需等待用户再次输入
10、查询某文件是由哪个包安装生成的
yum whatprovides|provides /path/to/somefile
[root@CentOS6 yum.repos.d]# yum provides /usr/bin/zip #查询出zip文件是由那个包安装生成;
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
zip-3.0-1.el6.i686 : A file compression and packaging utility compatible with PKZIP
Repo : c6-cd
Matched from:
Filename : /usr/bin/zip
zip-3.0-1.el6.i686 : A file compression and packaging utility compatible with PKZIP
Repo : installed
Matched from:
Other : Provides-match: /usr/bin/zip
11、安装包组
yum groupinstall "GROUP NAME"
12、卸载包组
yum groupremove "GROUP NAME"
【自建yum仓库】
下面通过自建一个http服务器的yum仓库为例说明:
第一步:安装httpd程序并启动服务;
yum -y install httpd
service httpd start
chkconfig httpd on
12345678910111213141516171819202122232425262728293031323334353637 [root@CentOS6 yum.repos.d]# yum -y install httpd
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package httpd.i686 0:2.2.15-29.el6.centos will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
httpd i686 2.2.15-29.el6.centos c6-cd 828 k
Transaction Summary
================================================================================
Install 1 Package(s)
Total download size: 828 k
Installed size: 2.8 M
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : httpd-2.2.15-29.el6.centos.i686 1/1
Verifying : httpd-2.2.15-29.el6.centos.i686 1/1
Installed:
httpd.i686 0:2.2.15-29.el6.centos
Complete!
[root@CentOS6 yum.repos.d]# service httpd start
Starting httpd: [ OK ]
[root@CentOS6 yum.repos.d]# chkconfig httpd on
[root@CentOS6 yum.repos.d]# chkconfig |grep httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
第二步:创建子目录,存放某相关的所有rpm包,httpd的文档根目录为/var/www/html;为仓库生成元数据文件(使用createrepo),以使能够作为仓库使用。
[root@CentOS6 html]# cd /var/www/html
[root@CentOS6 html]# mkdir test #创建test目录用来测试;
[root@CentOS6 html]# cp /mnt/cd/Packages/zsh-4.3.10-7.el6.i686.rpm ./test #复制zsh这个安装包到test目录;
[root@CentOS6 Packages]# yum install createrepo #安装createrepo,如提前已安装则不用再装;
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package createrepo.noarch 0:0.9.9-18.el6 will be installed
--> Processing Dependency: Python-deltarpm for package: createrepo-0.9.9-18.el6.noarch
--> Running transaction check
---> Package python-deltarpm.i686 0:3.5-0.5.20090913git.el6 will be installed
--> Processing Dependency: deltarpm = 3.5-0.5.20090913git.el6 for package: python-deltarpm-3.5-0.5.20090913git.el6.i686
--> Running transaction check
---> Package deltarpm.i686 0:3.5-0.5.20090913git.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================
Package Arch Version Repository Size
======================================================================
Installing:
createrepo noarch 0.9.9-18.el6 c6-cd 94 k
Installing for dependencies:
deltarpm i686 3.5-0.5.20090913git.el6 c6-cd 73 k
python-deltarpm i686 3.5-0.5.20090913git.el6 c6-cd 27 k
Transaction Summary
==========================================================================
Install 3 Package(s)
Total download size: 195 k
Installed size: 522 k
Is this ok [y/N]: y
Downloading Packages:
-----------------------------------------------------------------------------------------
Total 3.3 MB/s | 195 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : deltarpm-3.5-0.5.20090913git.el6.i686 1/3
Installing : python-deltarpm-3.5-0.5.20090913git.el6.i686 2/3
Installing : createrepo-0.9.9-18.el6.noarch 3/3
Verifying : createrepo-0.9.9-18.el6.noarch 1/3
Verifying : python-deltarpm-3.5-0.5.20090913git.el6.i686 2/3
Verifying : deltarpm-3.5-0.5.20090913git.el6.i686 3/3
Installed:
createrepo.noarch 0:0.9.9-18.el6
Dependency Installed:
deltarpm.i686 0:3.5-0.5.20090913git.el6
python-deltarpm.i686 0:3.5-0.5.20090913git.el6
Complete! #createrepo安装完成
[root@CentOS6 Packages]# createrepo /var/www/html/test/ #创建元数据文件;
Spawning worker 0 with 1 pkgs
Workers Finished
Gathering worker results
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@CentOS6 Packages]# ls /var/www/html/test/
repodata zsh-4.3.10-7.el6.i686.rpm #repodata即为刚刚创建出来存放元数据文件的目录;
[root@CentOS6 test]# ls /var/www/html/test/repodata/ #查看repodata下的元数据文件;
105b962a2624ebddd589ec03c1e4fdcd7a3511ea1e47fd80eeda364b41d48cdd-primary.sqlite.bz2
7762e79a7341d1b6a929e893fd732302dd27499742abf4549130601465760426-filelists.xml.gz
78f72a947bd046641a5ea0574872212cfe829f1525ff74f1d13f4da3c4d73cde-filelists.sqlite.bz2
7e8d7b7938dbd28a823ebe27420f3c2d088f9a1933f37319e925295e24417009-other.sqlite.bz2
885867e505b55bc3f09fb4eec3bc7cf8a5352cce29c307e7b37afaa38e3d33cc-primary.xml.gz
e0eb2191ed5de3b3975bbb005a00e07a044d5724b87373d4a6cdf399ff99bf3f-other.xml.gz
#通过以上几步一个简单的yum仓库就搭建好了!
第三步:配置yum客户端使用此仓库即可;
[root@CentOS6 yum.repos.d]# vim test.repo #编辑指向yum库的repo文件
[c6-test]
name=CentOS-$releasever - test
baseurl=file:///var/www/html/test
gpgcheck=0 #此处只为实验,没有复制密钥,到yum库中,所以不启用校验;
enabled=1 #启用
[root@CentOS6 yum.repos.d]# yum clean all #清除缓存;
Loaded plugins: fastestmirror, refresh-packagekit, security
Cleaning repos: c6-test
Cleaning up Everything
Cleaning up list of fastest mirrors
[root@CentOS6 yum.repos.d]# yum repolist #显示可用的repo;
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
c6-test | 2.9 kB 00:00 ...
c6-test/primary_db | 3.2 kB 00:00 ...
repo id repo name status
c6-test CentOS-6 - test 1
repolist: 1
[root@CentOS6 yum.repos.d]# yum -y install zsh #安装zsh;
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package zsh.i686 0:4.3.10-7.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================
Package Arch Version Repository Size
==========================================================================
Installing:
zsh i686 4.3.10-7.el6 c6-test 2.1 M
Transaction Summary
==========================================================================
Install 1 Package(s)
Total download size: 2.1 M
Installed size: 4.6 M
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : zsh-4.3.10-7.el6.i686 1/1
Verifying : zsh-4.3.10-7.el6.i686 1/1
Installed:
zsh.i686 0:4.3.10-7.el6
Complete!
#安装成功!实验完成!
好啦!可能描写的比较啰嗦,如有总结说明不妥的地方,还请各位见谅!谢谢!