Linux软件包的管理--YUM
在上一篇博客《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。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 在这里定义一些默认配置属性和全局配置属性 [main] cachedir= /var/cache/yum/ $basearch/$releasever
keepcache=0 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=16&ref=http: //bugs .centos.org /bug_report_page .php?category=yum
distroverpkg=centos-release |
一个后几个相关的仓库的配置保存成一个文件,文件名都以.repo结尾,保存在/etc/yum.repo.d目录中。
下面介绍一下文件格式(以163镜像为例):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[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通常只需一个,我们一般配置baseurl gpgcheck=1 # 是否进行gpg检测(包的完整性和合法性)
gpgkey= # gpg检测是的秘钥文件
# 还有其他选型,读者在配置时,可自行选择 # 常用的还有: # enabled={0|1} 是否启用该仓库 # cost=N 配置仓库的”花费“,等价于优先级。N的值越小,优先级越高。 |
配置好这些准备工作,就可以使用yum工具了。
三、yum工具的使用
这里以互联网163镜像为例演示。
1、列出所有可用repo
yum repolist {enabled|disabled|all}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# yum repolist {enabled|disabled|all} # enabled 列出可用的 # disabled 不可用的 # all所有的 [[email protected] yum.repos.d] # yum repolist
Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile base | 3.7 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 repo id repo name status
base CentOS-6 - Base - 163.com 6,367 extras CentOS-6 - Extras - 163.com 14 updates CentOS-6 - Updates - 163.com 1,153 repolist: 7,534 |
2、列出rpm包
yum list {all|installed|available}
1
2
3
4
5
6
7
8
9
|
# yum list {all|installed|available} # all 列出所有的包 等价于 yum list # installed 列出已安装的包 # available 列出可用的包(不包括已安装的) # 一般情况下,会这样使用 [[email protected] yum.repos.d] # yum list installed | grep "http"
httpd.x86_64 2.2.15-26.el6.centos httpd-tools.x86_64 2.2.15-26.el6.centos |
3、包的描述信息
yum info package_name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
Loading mirror speeds from cached hostfile Installed Packages Name : httpd Arch : x86_64 Version : 2.2.15 Release : 26.el6.centos Size : 2.9 M Repo : installed From repo : anaconda-CentOS-201303050102.x86_64 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.
Available Packages Name : httpd Arch : x86_64 Version : 2.2.15 Release : 30.el6.centos Size : 821 k Repo : updates 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
1
2
3
4
5
6
|
# 经常情况下我们会是这样来使用 [[email protected] yum.repos.d] # yum grouplist | grep "Deve"
Additional Development
Desktop Platform Development
Development tools
Server Platform Development
|
5、显示包组的信息:例如组中包含的程序包列表
yum groupinfo "GROUP NAME"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
[[email protected] 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
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}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# yum clean {all|packages|metadata|expire-cache|rpmdb|plugins} # all 所有缓存 # packages 清除包缓存 # metedata 清除元数据缓存 # expire-cache 清除过期缓存 # rpmdb 清除RPM数据库缓存 # plufins 清除插件缓存 # 最常用的就是 yum clean all [[email protected] yum.repos.d] # yum clean all
Loaded plugins: fastestmirror, refresh-packagekit, security Cleaning repos: base extras updates Cleaning up Everything Cleaning up list of fastest mirrors |
7、安装程序包
yum install package_name
1
|
yum install httpd -y # 常用选项是 -y 意思是不需要确认,直接安装
|
重新安装:
yum reinstall package_name
1
|
yum reinstall httpd -y |
8、升级
yum check-update: 检查可用的升级包
yum update package_name 升级软件
1
2
|
[[email protected] yum.repos.d] # rpm -q ntp
ntp-4.2.6p5-1.el6.centos.x86_64 |
yum downgrade package_name 降级使用的,退回原来的版本
1
2
3
4
5
6
7
|
# yum downgrade ntp -y 降级第一次可能会出现错误,解决办法之一是:yum upgrade -y 升级yum工具 [[email protected] yum.repos.d] # yum downgrade ntp -y
Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Downgrade Process Loading mirror speeds from cached hostfile Only Upgrade available on package: ntp-4.2.6p5-1.el6.centos.x86_64 Nothing to do
|
9、卸载
yum remove|erase package_name
1
|
yum remove httpd -y |
10、查询某文件是由哪个包安装生成的
yum whatprovides|provides /path/to/somefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# 会列出所有的提供此配置文件的包 [[email protected] yum.repos.d] # yum provides /etc/httpd/conf/httpd.conf
Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile httpd-2.2.15-30.el6.centos.x86_64 : Apache HTTP Server Repo : updates Matched from: Filename : /etc/httpd/conf/httpd .conf
httpd-2.2.15-29.el6.centos.x86_64 : Apache HTTP Server Repo : base Matched from: Filename : /etc/httpd/conf/httpd .conf
httpd-2.2.15-30.el6.centos.x86_64 : Apache HTTP Server Repo : installed Matched from: Other : Provides-match: /etc/httpd/conf/httpd .conf
|
11、安装包组
yum groupinstall "GROUP NAME"
1
2
3
4
5
|
# 常用的包有: # Desktop Platform Development 开发平台 # Development tools 开发所用的包 # 例如:yum groupinstall "Development tools" -y |
12、卸载包组
yum groupremove "GROUP NAME"
四、自制yum仓库
1、如果作为个人使用的话,最简单的办法是,挂载光盘镜像使用。
以VMware虚拟机为例:
先关联光盘镜像,挂载光盘镜像就可以使用。
其中:.repo中的baseurl使用file就可。例如:baseurl=file:///media/centos
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服务端的步骤。