您的位置:首页 > 运维架构 > Linux

linux程序包管理之yum

2015-10-30 18:28 429 查看
上章我们提到,rpm包之间存在依赖关系,有时这些依赖关系很复杂,使用rpm命令安装程序包时,手动解决起来太麻烦。而yum工具能够很好地解决这一让人头疼的问题

一、什么是yum
yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装

二、yum的工作机制
①yum是基于C/S架构
②yum仓库:
rpm包提供方提供多个rpm包,然后分析并记录与包相关的元数据,如包名、版本信息、各包所包含的文件列表、包之间的依赖关系、包分组信息等。存放这些rpm包和元数据文件的位置就被称为repository或yum库
元数据文件格式:centos5: xml, centos6,7: sqlite
createrepo: 生成yum仓库元数据文件的工具
③yum客户端:
第一步:当客户端需要安装软件时,会主动向yum仓库获取元数据,并缓存于本地(/var/cache/yum)
第二步:yum客户端程序在本地分析元数据文件,并结合本地RPM资料库做出要安装的程序包的决策
第三步:根据决策联系yum仓库,下载各程序包缓存于本地后,一并进行安装

三、使用yum工具前的准备工作
3.1、确保有yum repository可用

①yum库分类
base库:通常为系统发行版所提供的程序包
updates库:一些升级包

extra库:额外程序包
epel库:
默认使用顺序为updates库→base库→extra库→epel库。在客户端可指定多个yum库,并可定义各yum库的使用优先级
②yum库的来源
提供yum库的除了有本地光盘,还有线上的一些文件服务器,如ftp,ntp,http,nfs;另外我们也可以在本地或线上自建yum库。repodata目录所在的父目录就是一个可用仓库ftp://server/path/to/repository http://server/path/to/repository
nfs://server/nfs_path
file:///path/to/repository
file:///media/cdrom
如下图,该yum库的地址为http://mirrors.163.com/centos/6/os/x86_64/。repodata目录中存放的是元数据文件,Packages为rpm包目录


3.2、yum客户端

提供repos配置文件,指明仓库访问路径及各种属性信息

主配置文件(中心配置文件):/etc/yum.conf

[root@localhost ~]# cat /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever  #缓存目录
keepcache=0  #是否保留缓存
debuglevel=2  #调试级别
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
...

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
■一个或几个相关仓库的配置信息可保存为一个文件,以.repo为后缀,位于/etc/yum.repos.d目录中
在*.repo文件中指定yum仓库的格式
[REPOSITORY_ID] #必须是唯一的
name=Some name for this repository
baseurl=url://server1/path/to/reposity #仓库地址,必须指向repodata目录所在的父目录
url://server2/path/to/reposity #可指定多个仓库,注意多个仓库的书写格式

mirrorlist=URL to a file #mirrorlist指向一个包含baseurl列表的文件
enabled={0|1}:是否启用该仓库,默认为启用
gpgcheck={0|1}:是否检测包来源合法性
gpgkey=URL:密钥所在位置
cost=N:指定访问此仓库的开销,默认为1000。开销越小,越优先被使用。如果在所有的仓库配置文件中都没指定该项或者指定的值相同,则依照它们在/etc/yum.repos.d目录中的存放顺序使用
另外,也可使用priority=N(1-99,数字越小,优先级越高)定义yum仓库优先级,但前提是要安装插件yum-plugin-priorities

示例:配置本地挂载的光盘作为yum仓库
[root@localhost ~]# mount /dev/cdrom /media/cdrom
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# ls /media/cdrom
CentOS_BuildTag  EULA  images    Packages                  repodata              RPM-GPG-KEY-CentOS-Debug-6     RPM-GPG-KEY-CentOS-Testing-6
EFI              GPL   isolinux  RELEASE-NOTES-en-US.html  RPM-GPG-KEY-CentOS-6  RPM-GPG-KEY-CentOS-Security-6  TRANS.TBL
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# yum -y install yum-plugin-priorities
...
[root@localhost yum.repos.d]# vim dvd.repo
[local]
name=Centos6.5-x86_64-dvd
baseurl=file:///media/cdrom
gpgcheck=1
gpgkey=file:///media/cdrom/RPM-GPG-KEY-CentOS-6
priori=1  #将本地yum仓库的优先级设为最高
[root@localhost yum.repos.d]# ls
CentOS6-Base-163.repo  CentOS-Base.repo.backup  CentOS-Debuginfo.repo  CentOS-Media.repo  CentOS-Vault.repo  dvd.repo
[root@localhost yum.repos.d]# yum clean all  #指定新的yum仓库后一般应刷新缓存
[root@localhost yum.repos.d]# yum clean all
Loaded plugins: fastestmirror, priorities, refresh-packagekit, security
Cleaning repos: local base epel extras updates
Cleaning up Everything
Cleaning up list of fastest mirrors


yum配置文件中可用的四个宏
$releasever: 程序的版本,对yum而言指的是redhat-release版本;只替换为主版本号,如RedHat 6.5,则替换为6;
$arch: 系统架构
$basearch: 系统基本架构,如i686,i586等的基本架构为i386;
$YUM0-9: 在系统中定义的环境变量,可以在yum中使用;
当我们在yum仓库配置文件中使用宏时,一个配置文件可适用于不同的平台和系统版本。例如http://mirrors.163.com/centos/$releasever/os/$basearch/,在i386平台的Centos5.5系统上会被替换为http://mirrors.163.com/centos/5/os/i386/;在x86_64平台的Centos6.5上会被替换为http://mirrors.163.com/centos/6/os/x86_64/
获取当前系统相应变量替换结果的办法:
# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import yum,pprint
>>> yb = yum.YumBase()
>>> pprint.pprint(yb.conf.yumvar,width=1)
Loaded plugins: fastestmirror, refresh-packagekit
{'arch': 'ia32e',
'basearch': 'x86_64',
'releasever': '6',
'uuid': 'ea249181-2f82-4c40-8c42-d51b3fce319a'}
>>> quit()
[root@localhost yum.repos.d]# cat CentOS6-Base-163.repo
...
[base]
name=CentOS-$releasever - Base - 163.com
baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#released updates
[updates]
name=CentOS-$releasever - Updates - 163.com
baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
...


自建yum仓库
(1)自建基于光盘镜像的yum库,见上方示例
(2)在中心服务器上提供yum库,以http为例
①安装httpd程序,并启动服务
rpm -ivh httpd-XX或者yum install httpd
service httpd start
chkconfig httpd on
②httpd的文档根目录为/var/www/html:
创建子目录,存放某些rpm包

③为仓库生成元数据文件,以使能够作为仓库使用
rpm -ivh createrepo-XX或者yum install createrepo
createrepo /path/to/rpm_repo/
④ 配置yum客户端使用此仓库即可
[root@localhost ~]# yum -y install httpd createrepo
...
Complete!
[root@localhost ~]# service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[  OK  ]
[root@localhost ~]# chkconfig httpd on
[root@localhost ~]# ss -tnl
...
LISTEN      0      128           :::80          :::*
...
[root@localhost ~]# cp -r zabbix-2.4 /var/www/html/
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# mv zabbix-2.4 zabbix
[root@localhost html]# ls zabbix
zabbix-2.4.0-1.el6.x86_64.rpm               zabbix-proxy-pgsql-2.4.0-1.el6.x86_64.rpm    zabbix-server-pgsql-2.4.0-1.el6.x86_64.rpm
...
[root@localhost html]# createrepo zabbix
Spawning worker 0 with 17 pkgs
Workers Finished
Gathering worker results

Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@localhost html]# ls zabbix
repodata                                    zabbix-proxy-mysql-2.4.0-1.el6.x86_64.rpm    zabbix-server-mysql-2.4.0-1.el6.x86_64.rpm
[root@localhost html]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# vim testhttp.repo
[zabbix]
name=zabbix repo
base=http://192.168.30.20/zabbix
gpgcheck=0
[root@localhost yum.repos.d]# yum clean all
...
[root@localhost yum.repos.d]# yum repolist
Loaded plugins: fastestmirror, priorities, refresh-packagekit, security
Determining fastest mirrors
repo id                                                                 repo name                                                                                    status
base                                                                    CentOS-6 - Base - 163.com                                                                    0
extras                                                                  CentOS-6 - Extras - 163.com                                                                  0
local                                                                   Centos6.5-x86_64-dvd                                                                         0
updates                                                                 CentOS-6 - Updates - 163.com                                                                 0
zabbix                                                                  zabbix repo                                                                                  0
repolist: 0
[root@localhost yum.repos.d]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT  #开放80端口





四、yum命令的使用
常用选项:
-y:对所有提问都回答yes
-q:静默模式
-v:详细模式

--disablerepo=[repo]:安装时禁用某些yum仓库
--skip-broken:忽略依赖问题
--nogpgcheck:不对包做验证

1、列出repository
yum repolist {enabled|disabled|all},默认为enabled
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror, refresh-packagekit, security
Repository base is listed more than once in the configuration
Loading mirror speeds from cached hostfile
repo id                                                                 repo name                                                                                    status
base                                                                    CentOS-6 - Base - 163.com                                                                    6,575
extras                                                                  CentOS-6 - Extras - 163.com                                                                     44
updates                                                                 CentOS-6 - Updates - 163.com                                                                   470
repolist: 7,089
[root@localhost ~]# yum repolist disabled
Loaded plugins: fastestmirror, refresh-packagekit, security
Repository base is listed more than once in the configuration
repo id                                                                        repo name
C6.0-base                                                                      CentOS-6.0 - Base
C6.0-centosplus                                                                CentOS-6.0 - CentOSPlus
...


2、包管理

①列出rpm包
yum list {all|installed|available},默认为all
yum list {all|installed|available} | grep 'PATTERN'
yum list KEYWORD*
[root@localhost ~]# yum list httpd*
Loaded plugins: fastestmirror, refresh-packagekit, security
Repository base is listed more than once in the configuration
Loading mirror speeds from cached hostfile
Installed Packages
httpd.x86_64                                                                     2.2.15-47.el6.centos                                                              @updates
httpd-tools.x86_64                                                               2.2.15-47.el6.centos                                                              @updates
Available Packages
httpd-devel.i686                                                                 2.2.15-47.el6.centos                                                              updates
httpd-devel.x86_64                                                               2.2.15-47.el6.centos                                                              updates
httpd-manual.noarch                                                              2.2.15-47.el6.centos                                                              updates


②显示包的描述信息(安装或未安装的包都可显示)
yum info package_name #相当于rpm -qi pkg_name,但rpm命令不能查看未安装的包的描述信息
[root@localhost ~]# yum info ftp
Loaded plugins: fastestmirror, refresh-packagekit, security
Repository base is listed more than once in the configuration
Loading mirror speeds from cached hostfile
Available Packages
Name        : ftp
Arch        : x86_64
Version     : 0.17
Release     : 54.el6
Size        : 58 k
Repo        : base
Summary     : The standard UNIX FTP (File Transfer Protocol) client
URL         : ftp://ftp.uk.linux.org/pub/linux/Networking/netkit License     : BSD with advertising
Description : The ftp package provides the standard UNIX command-line FTP (File
: Transfer Protocol) client.  FTP is a widely used protocol for
: transferring files over the Internet and for archiving files.
:
: If your system is on a network, you should install ftp in order to do
: file transfers.


③安装程序包
yum install package_name #会自动解决依赖关系
yum reinstall package_name #重新安装

[root@localhost ~]# yum -y install php
...
Installed:
php.x86_64 0:5.3.3-46.el6_6
Dependency Installed:
php-cli.x86_64 0:5.3.3-46.el6_6                     php-common.x86_64 0:5.3.3-46.el6_6

Complete!


④升级/降级
yum check-update: 检查可用的升级包
yum update [package_name] #升级所有或指定的包

yum downgrade package_name
#可指定升/降级到指定的版本,如yum update httpd-2.2.15
[root@localhost ~]# rpm -q mysql
mysql-5.1.71-1.el6.x86_64
[root@localhost ~]# yum -y update mysql
...
Updated:
mysql.x86_64 0:5.1.73-5.el6_6
Complete!
[root@localhost ~]# rpm -q mysql
mysql-5.1.73-5.el6_6.x86_64
[root@localhost ~]# yum -y downgrade mysql
...
Removed:
mysql.x86_64 0:5.1.73-5.el6_6

Installed:
mysql.x86_64 0:5.1.71-1.el6

Complete!
[root@localhost ~]# rpm -q mysql
mysql-5.1.71-1.el6.x86_64


⑤卸载包
yum remove|erase package_name #会将被依赖的包一并卸载
[root@localhost ~]# yum remove mysql-libs
...
Removed:
mysql-libs.x86_64 0:5.1.71-1.el6      mysql-libs.x86_64 0:5.1.73-5.el6_6

Dependency Removed:
mysql.x86_64 0:5.1.71-1.el6  #将依赖此包的mysql包卸载

Complete!


⑥查询某文件是由哪个包安装生成的
yum whatprovides|provides /path/to/somefile
[root@localhost ~]# yum provides /var/www/html
...
httpd-2.2.15-45.el6.centos.x86_64 : Apache HTTP Server
Repo        : base
Matched from:
Filename    : /var/www/html

httpd-2.2.15-47.el6.centos.x86_64 : Apache HTTP Server
Repo        : installed
Matched from:
Other       : Provides-match: /var/www/html


⑦特殊场景:从其它处获得一个rpm包,如果此包依赖于其它包(在仓库中),如何安装?
无需从其它库中下载依赖的包手动解决依赖关系,可使用yum命令直接安装
yum install /path/to/pkg_file
[root@localhost ~]# yum install mysql-5.1.73-5.el6_6.x86_64.rpm
...
Installing:
mysql               x86_64               5.1.73-5.el6_6               /mysql-5.1.73-5.el6_6.x86_64               2.4 M
Installing for dependencies:
mysql-libs          x86_64               5.1.73-5.el6_6               base                                       1.2 M
...
Complete!


yumdownloader pkg_name:只下载包不安装

3、包组管理
①列出所有包组
yum grouplist
[root@localhost ~]# yum grouplist
...
Installed Groups:
Additional Development
Debugging Tools
...
Installed Language Groups:
Arabic Support [ar]
Armenian Support [hy]
...
Available Groups:
Backup Client
Backup Server
...
Available Language Groups:
Afrikaans Support [af]
Albanian Support [sq]
...


②显示包组的信息:例如组中包含的程序包列表
yum groupinfo "GROUP NAME"

CentOS6 跟开发相关的包组:
Development Tools
Server Platform Development
Desktop Platform Development
[root@localhost ~]# yum groupinfo "Development tools"
...
Group: Development tools
Description: A basic development environment.
Mandatory Packages:
autoconf
automake
...
Default Packages:
byacc
cscope
...
Optional Packages:
ElectricFence
PyYAML
...


③安装包组
yum groupinstall "GROUP_NAME"
[root@localhost ~]# yum groupinstall "Development tools"
Installed:
autoconf.noarch 0:2.63-5.1.el6           automake.noarch 0:1.11.1-4.el6      bison.x86_64 0:2.4.1-5.el6                          byacc.x86_64 0:1.9.20070509-7.el6
...
Dependency Installed:
cloog-ppl.x86_64 0:0.15.7-1.2.el6                        cpp.x86_64 0:4.4.7-16.el6                            gettext-devel.x86_64 0:0.17-18.el6
...


④升级包组
yum groupupdate "group_name"

⑤卸载包组
yum groupremove "GROUP_NAME"

4、清理缓存

yum clean {all|packages|metadata|expire-cache|rpmdb|plugins}
[root@localhost ~]# yum clean all
Loaded plugins: fastestmirror, refresh-packagekit, security
Repository base is listed more than once in the configuration
Cleaning repos: base extras updates
Cleaning up Everything
Cleaning up list of fastest mirrors
[root@localhost ~]# tree /var/cache/yum  #可看到/var/cache/yum各子目录中的内容都被清空
/var/cache/yum
├── timedhosts.txt
└── x86_64
└── 6
├── base
│   ├── gen
│   └── packages
├── extras
│   └── packages
├── local
│   ├── gen
│   └── packages
└── updates
└── packages

12 directories, 1 file


四、配置EPEL源
1、EPEL是什么?

EPEL (Extra Packages for Enterprise Linux) 是Fedora社区维护的一个软件仓库项目,为RHEL及衍生发行版如CentOS、Scientific Linux等提供他们不提供的高质量的软件包。
EPEL官网:https://fedoraproject.org/wiki/EPEL
2、配置EPEL源

①确认当前系统版本
cat /etc/redhat-release
②从EPEL官网上下载对应的EPEL的rpm包和公钥
以Centos 6.5为例:
wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm wget http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6 ③导入公钥并安装EPEL包
rpm --import /path/to/key_file
rpm -ivh /path/to/pkg_name
④查看EPEL是否已添加到仓库列表
yum repolist
[root@localhost ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@localhost ~]# wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm ...
2015-10-29 16:04:21 (13.0 KB/s) - “epel-release-latest-6.noarch.rpm” saved [14540/14540]
[root@localhost ~]# wget http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6 ...
[root@localhost ~]# rpm --import RPM-GPG-KEY-EPEL-6
[root@localhost ~]# rpm -ivh epel-release-latest-6.noarch.rpm
Preparing...                ########################################### [100%]
1:epel-release           ########################################### [100%]
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror, priorities, refresh-packagekit, security
Determining fastest mirrors
epel/metalink                                                                                                                                       | 4.2 kB     00:00
epel                                                                                                                                                | 4.3 kB     00:00
epel/primary_db                                                                                                                                     | 5.8 MB     00:19
repo id                                                        repo name                                                                                             status
base                                                           CentOS-6 - Base - 163.com                                                                             0
epel                                                           Extra Packages for Enterprise Linux 6 - x86_64                                                        0
extras                                                         CentOS-6 - Extras - 163.com                                                                           0
local                                                          Centos6.5-x86_64-dvd                                                                                  0
updates                                                        CentOS-6 - Updates - 163.com                                                                          0
zabbix                                                         zabbix repo                                                                                           0
repolist: 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  yum EPEL createrepo