首页 > 代码库 > Linux下程序包管理之YUM
Linux下程序包管理之YUM
实验环境:
CentOS release 6.6 (Final) 一台
IP地址:172.16.249.230
附带:CentOS release 6.6光盘镜像文件
CentOS-6.6-x86_64-bin-DVD1.iso
CentOS-6.6-x86_64-bin-DVD2.iso
YUM(Yellow dog Updater, Modified) 由Duke University团队,修改Yellow Dog Linux的Yellow Dog Updater开发而成,
是一个基于 RPM 包管理的字符前端软件包管理器。能够从指定的服务器自动下载 RPM 包并且安装,可以处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装。被Yellow Dog Linux本身,以及Fedora、Red Hat Enterprise Linux、CentOS、SUSE采用。
一.YUM仓库和客户端简介
YUM repository:yum仓库
存储了众多rpm包,以及包的相关元数据的文件服务器
文件服务器:
HTTP:http://ip/centos/$releasever/os/$basearch/
FTP:ftp://ip/centos/$releasever/os/$basearch/
NFS:网络文件系统,nfs://ip/centos/$releasever/os/$basearch/
FILE:本地,file:///centos/$releasever/os/$basearch/
YUM Client :客户端
配置文件:指向仓库的位置以及各种配置信息,每个yum客户端可以有多个仓库。
缓存文件:/var/cache/yum/
二.YUM的配置文件详解
yum的配置一般有两种方式,一种是直接配置/etc目录下的yum.conf文件,另外一种是在/etc/yum.repos.d目录下增加.repo文件。
(一).关于yum.conf文件详解
#cat /etc/yum.conf [main] cachedir=/var/cache/yum/$basearch/$releasever #yum下载的RPM包的缓存目录 keepcache=0 #缓存是否保存,1保存,0不保存。 debuglevel=2 #调试级别(0-10),默认为2(具体调试级别的应用,我也不了解)。 logfile=/var/log/yum.log #yum的日志文件所在的位置 exactarch=1 #在更新的时候,是否允许更新不同版本的RPM包,比如是否在i386上更新i686的RPM包。 obsoletes=1 #这是一个update的参数,具体请参阅yum(8),简单的说就是相当于upgrade,允许更新陈旧的RPM包。 gpgcheck=1 #是否检查GPG(GNU Private Guard),一种密钥方式签名。 plugins=1 #是否允许使用插件,默认是0不允许,但是我们一般会用yum-fastestmirror这个插件。 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 distroverpkg=centos-release # This is the default, if you make this bigger yum won‘t see if the metadata # is newer on the remote and so you‘ll "gain" the bandwidth of not having to # download the new metadata and "pay" for it by yum not having correct # information. # It is esp. important, to have correct metadata, for distributions like # Fedora which don‘t keep old packages around. If you don‘t like this checking # interupting your command line usage, it‘s much better to have something # manually check the metadata once an hour (yum-updatesd will do this). # metadata_expire=90m # PUT YOUR REPOS HERE OR IN separate files named file.repo # in /etc/yum.repos.d
(二).关于/etc/yum.repos.d/*.repo文件详解
什么是repo文件?
repo文件是CentOS中yum源(软件仓库)的配置文件,通常一个repo文件定义了一个或者多个软件仓库的细节内容,例如我们将从哪里下载需要安装或者升级的软件包,repo文件中的设置内容将被yum读取和应用!
我们以一份系统自带的repo文件做为实例来探讨(#号后面是我加的注释):
[base] name=CentOS-$releasever - Base #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
[base] #方括号里面的是软件源的名称,将被yum取得并识别 name=CentOS-$releasever - Base #这里也定义了软件 仓库的名称,通常是为了方便阅读配置文件,一般没什么作用, $releasever变量定义了发行版本,通常是5,6,7等数字, 这个变量根据当前系统的版本架构不同而有不同的取值, 这可以方便yum升级的时候选择 适合当前系统的软件包,以下同…… #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/ #上面的一行baseurl第一个字符是‘#‘表示该行已经被注释,将不会被读取, $basearch变量定义了系统的架构,可以是i386、x86_64、ppc等值 这一行的意思是指定一个baseurl(源的镜像服务器地址) enabled=1 #这个选项表示这个repo中定义的源是启用的,0为禁用 gpgcheck=1 # 这个选项表示这个repo中下载的rpm将进行gpg的校验,已确定rpm包的来源是有效和安全的 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 #定义用于校验的gpg密钥
三.构建YUM仓库
简要步骤说明
(一).确认文件共享方式
(二).把rpm包放置于能够被各yum客户端访问到位置
(三).创建yum仓库的元数据
createrepo命令
#yum -y install createrepo
为yum仓库创建元数据文件
#createrepo 软件包文件目录
实验案例:
在本文中我创建一个本地光盘yum来构建一个本地YUM仓库。
(一)、挂载本地光盘
#mkdir /mnt/cdrom
#新建挂载目录
#mount -t iso9660 /dev/cdrom /mnt/cdrom
#挂载光盘到/mnt/cdrom 目录
#ls /mnt/cdrom
#查看/mnt/cdrom 目录内容
[root@LinuxHost yum.repos.d]# mkdir /mnt/cdrom [root@LinuxHost yum.repos.d]# mount -t iso9660 /dev/cdrom /mnt/cdrom mount: block device /dev/sr0 is write-protected, mounting read-only [root@LinuxHost yum.repos.d]# ls /mnt/cdrom CentOS_BuildTag isolinux RPM-GPG-KEY-CentOS-Debug-6 EFI Packages RPM-GPG-KEY-CentOS-Security-6 EULA RELEASE-NOTES-en-US.html RPM-GPG-KEY-CentOS-Testing-6 GPL repodata TRANS.TBL images RPM-GPG-KEY-CentOS-6
(二)、配置本地YUM源
#cd /etc/yum.repos.d/
#进入/etc/yum.repos.d 目录
#ls
#查看目录内容
#mv CentOS-Base.repo CentOS-Base.repo.bak #mv CentOS-Debuginfo.repo CentOS-Debuginfo.repo.bak
#重命名上面2个文件
#vi CentOS-Media.repo
#对CentOS-Media.repo 进行编辑
(三)、修改CentOS-Media.repo内容
[c6-media]
name=CentOS-$releaserver - Media
#自定义Yum源名称
baseurl=file:///mnt/cdrom/
#本地光盘挂载路径
gpgcheck=1
#检查GPG-KEY
enabled=1
#启用Yum源
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-6
#GPG-KEY路径
[root@LinuxHost yum.repos.d]# cat CentOS-Media.repo [C6-Media] name=CentOS-$releaserver-Media baseurl=file:///mnt/cdrom gpcheck=1 enabled=1 gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-6
检查列出可用yum仓库元数据
[root@LinuxHost yum.repos.d]# yum repolist Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile repo id repo name status C6-Media CentOS-$releaserver-Media 6,518 repolist: 6,518
至此,本地YUM源配置完成,我们就可以使用yum命令来安装管理程序包了。
四.YUM命令的使用
语法: yum [options] [command] [package ...]
其中的[options]是可选的,选项包括-h(帮助),-y(当安装过程提示选择全部为"yes"),-q(不显示安装的过程)等等。[command]为所要进行的操作,[package ...]是操作的对象
1 程序包管理:安装,更新,卸载,查看信息
直接安装包(可指定版本号) yum install packages -y
列出所有程序包(本地已安装和仓库中的程序包)
yum list
实例:
[root@LinuxHost yum.repos.d]# yum list |more Loaded plugins: fastestmirror, refresh-packagekit, security Determining fastest mirrors Installed Packages ConsoleKit.x86_64 0.4.1-3.el6 @anaconda-Cen tOS-201410241409.x86_64/6.6 ConsoleKit-libs.x86_64 0.4.1-3.el6 @anaconda-Cen tOS-201410241409.x86_64/6.6 ConsoleKit-x11.x86_64 0.4.1-3.el6 @anaconda-Cen tOS-201410241409.x86_64/6.6 DeviceKit-power.x86_64 014-3.el6 @anaconda-Cen tOS-201410241409.x86_64/6.6 GConf2.x86_64 2.28.0-6.el6 @anaconda-Cen tOS-201410241409.x86_64/6.6
列出可用yum仓库元数据
yum repolist
实例:
[root@LinuxHost yum.repos.d]# yum repolist Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile repo id repo name status C6-Media CentOS-$releaserver-Media 6,518 repolist: 6,518
--disablerepo=name repolist禁用某个仓库,优先级高于配置文件定义的 --enablerepo=name repolist启用某个仓库,优先级高于配置文件定义的 yum list {all|installed|available}显示选项后可以使用globbing通配符
清除本地所有缓存
yum clean all
headers, packages, metadata, dbcache, plugins, expire-cache, rpmdb, all
清除缓存的选项
实例:
[root@LinuxHost yum.repos.d]# yum clean all Loaded plugins: fastestmirror, refresh-packagekit, security Cleaning repos: C6-Media Cleaning up Everything Cleaning up list of fastest mirrors
手动生成yum缓存,有些慢就不帖了。
yum makecache
升级安装包(可指定版本号),没更新的包了!就不升级了!
yum update packages
降级安装包,
yum downgrade packages
我先升级zsh到zsh-4.3.10-8.el6_5.x86_64.rpm,然后执行降级。
实例:
[root@LinuxHost dvd]# rpm -Uvh zsh-4.3.10-8.el6_5.x86_64.rpm Preparing... ########################################### [100%] 1:zsh ########################################### [100%] [root@LinuxHost dvd]# yum downgrade zsh Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Downgrade Process Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package zsh.x86_64 0:4.3.10-7.el6 will be a downgrade ---> Package zsh.x86_64 0:4.3.10-8.el6_5 will be erased --> Finished Dependency Resolution Dependencies Resolved ======================================================================================== Package Arch Version Repository Size ======================================================================================== Downgrading: zsh x86_64 4.3.10-7.el6 C6-Media 2.1 M Transaction Summary ======================================================================================== Downgrade 1 Package(s) Total download size: 2.1 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. Installing : zsh-4.3.10-7.el6.x86_64 1/2 Cleanup : zsh-4.3.10-8.el6_5.x86_64 2/2 Verifying : zsh-4.3.10-7.el6.x86_64 1/2 Verifying : zsh-4.3.10-8.el6_5.x86_64 2/2 Removed: zsh.x86_64 0:4.3.10-8.el6_5 Installed: zsh.x86_64 0:4.3.10-7.el6 Complete! [root@LinuxHost dvd]# rpm -qa zsh zsh-4.3.10-7.el6.x86_64
降级成功!
卸载程序包(会一并卸载依赖包)
yum remove|erease packages
实例:
[root@LinuxHost dvd]# yum remove zsh Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Remove Process Resolving Dependencies --> Running transaction check ---> Package zsh.x86_64 0:4.3.10-7.el6 will be erased --> Finished Dependency Resolution Dependencies Resolved ======================================================================================== Package Arch Version Repository Size ======================================================================================== Removing: zsh x86_64 4.3.10-7.el6 @C6-Media 4.8 M Transaction Summary ======================================================================================== Remove 1 Package(s) Installed size: 4.8 M Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Erasing : zsh-4.3.10-7.el6.x86_64 1/1 Verifying : zsh-4.3.10-7.el6.x86_64 1/1 Removed: zsh.x86_64 0:4.3.10-7.el6 Complete! [root@LinuxHost dvd]# rpm -qa zsh
显示已安装或者未安装的指定包的信息(没有rpm -qi详细)
yum info packages
实例:
[root@LinuxHost dvd]# yum info zsh Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile Available Packages Name : zsh Arch : x86_64 Version : 4.3.10 Release : 7.el6 Size : 2.1 M Repo : C6-Media Summary : A powerful interactive shell URL : http://zsh.sunsite.dk/ License : BSD Description : The zsh shell is a command interpreter usable as an interactive login : shell and as a shell script command processor. Zsh resembles the ksh : shell (the Korn shell), but includes many enhancements. Zsh supports : command line editing, built-in spelling correction, programmable : command completion, shell functions (with autoloading), a history : mechanism, and more.
根据关键字模糊查询包名或包的summary信息包含此KEYWORD的相关列表
yum search KEYWORD
实例:
[root@LinuxHost dvd]# yum search zsh Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile =================================== N/S Matched: zsh =================================== python-twisted-core-zsh.x86_64 : Tab completion for Zsh and Twisted Core zsh-html.x86_64 : Zsh shell manual in html format zsh.x86_64 : A powerful interactive shell Name and summary matches only, use "search all" for everything.
查询文件谁提供了程序包安装的
yum provides|whatprovides /path/file
实例:
[root@LinuxHost dvd]# yum provides /usr/bin/yum Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile yum-3.2.29-60.el6.centos.noarch : RPM package installer/updater/manager Repo : C6-Media Matched from: Filename : /usr/bin/yum yum-3.2.29-60.el6.centos.noarch : RPM package installer/updater/manager Repo : installed Matched from: Other : Provides-match: /usr/bin/yum
2.程序包组管理 group
列出所有包组
yum grouplist
实例:
[root@LinuxHost dvd]# yum grouplist |more Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Group Process Loading mirror speeds from cached hostfile Installed Groups: Additional Development Base Console internet tools Desktop Development tools Dial-up Networking Support Directory Client E-mail server Fonts General Purpose Desktop Graphical Administration Tools Hardware monitoring utilities Input Methods Legacy UNIX compatibility Legacy X Window System compatibility NFS file server Network Infrastructure Server Network file system client Networking Tools Performance Tools Perl Support Security Tools System administration tools Web Server X Window System Installed Language Groups: --More--
显示某包组的信息(强制安装的包,默认安装包,可选包)
yum groupinfo “group_name"
实例:
[root@LinuxHost dvd]# yum groupinfo "Development tools" |more 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 --More-- 信息太多省略
CentOS6/7都支持安装程序组的命令
yum groupinstall "group_name" yum install @group_name
实例:
[root@LinuxHost dvd]# yum groupinstall "Server Platform Development" Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Group Process Loading mirror speeds from cached hostfile Package zlib-devel-1.2.3-29.el6.x86_64 already installed and latest version Package glibc-devel-2.12-1.149.el6.x86_64 already installed and latest version Package libstdc++-devel-4.4.7-11.el6.x86_64 already installed and latest version Package ncurses-devel-5.7-3.20090208.el6.x86_64 already installed and latest version Package 1:dbus-devel-1.2.24-7.el6_3.x86_64 already installed and latest version Package db4-devel-4.7.25-18.el6_4.x86_64 already installed and latest version Resolving Dependencies --> Running transaction check ---> Package krb5-devel.x86_64 0:1.10.3-33.el6 will be installed 省略。。。。。。。
CentOS6/7都支持移除组的命令
yum groupremove "group_name" yum remove @group_name
实例:
[root@LinuxHost dvd]# yum groupremove "Server Platform Development" Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Group Process Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package db4-devel.x86_64 0:4.7.25-18.el6_4 will be erased --> Processing Dependency: db4-devel for package: 4:perl-devel-5.10.1-136.el6.x86_64 ---> Package dbus-devel.x86_64 1:1.2.24-7.el6_3 will be erased ---> Package glibc-devel.x86_64 0:2.12-1.149.el6 will be erased --> Processing Dependency: glibc-devel >= 2.2.90-12 for package: gcc-4.4.7-11.el6.x86_64 ---> Package libstdc++-devel.x86_64 0:4.4.7-11.el6 will be erased --> Processing Dependency: libstdc++-devel = 4.4.7-11.el6 for package: gcc-c++-4.4.7-11.el6.x86_64 省略。。。。。。
直接升级程序包组
yum groupupdate "group_name"
实例:
[root@LinuxHost dvd]# yum groupupdate "Server Platform Development" Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Group Process Loading mirror speeds from cached hostfile Package zlib-devel-1.2.3-29.el6.x86_64 already installed and latest version Package glibc-devel-2.12-1.149.el6.x86_64 already installed and latest version Package libstdc++-devel-4.4.7-11.el6.x86_64 already installed and latest version Package ncurses-devel-5.7-3.20090208.el6.x86_64 already installed and latest version Package 1:dbus-devel-1.2.24-7.el6_3.x86_64 already installed and latest version Package db4-devel-4.7.25-18.el6_4.x86_64 already installed and latest version Resolving Dependencies --> Running transaction check ---> Package krb5-devel.x86_64 0:1.10.3-33.el6 will be installed --> Processing Dependency: libselinux-devel for package: krb5-devel-1.10.3-33.el6.x86_64 省略
3.其他程序管理命令
安装本地的rpm包文件,自动通过仓库解决依赖关系,还会检查包来源合法性和完整性
yum localinstall packages
实例:
[root@LinuxHost dvd]# yum localinstall zsh-4.3.10-8.el6_5.x86_64.rpm Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Local Package Process Examining zsh-4.3.10-8.el6_5.x86_64.rpm: zsh-4.3.10-8.el6_5.x86_64 Marking zsh-4.3.10-8.el6_5.x86_64.rpm to be installed Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package zsh.x86_64 0:4.3.10-8.el6_5 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================== Package Arch Version Repository Size ======================================================================================== Installing: zsh x86_64 4.3.10-8.el6_5 /zsh-4.3.10-8.el6_5.x86_64 4.8 M Transaction Summary ======================================================================================== Install 1 Package(s) Total size: 4.8 M Installed size: 4.8 M Is this ok [y/N]: 省略信息。。。。。。
本地安装忽略合法性和完整性
yum localinstall packages --nogpgcheck
检查可用升级
yum check-update
实例:
[root@LinuxHost dvd]# yum check-update Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile
重装程序包
yum reinstall packages
列出程序包依赖关系的信息
yum deplist packages
实例:
[root@LinuxHost dvd]# yum deplist python Loaded plugins: fastestmirror, refresh-packagekit, security Finding dependencies: Loading mirror speeds from cached hostfile package: python.x86_64 2.6.6-52.el6 dependency: python-libs(x86-64) = 2.6.6-52.el6 provider: python-libs.x86_64 2.6.6-52.el6 dependency: libutil.so.1()(64bit) provider: glibc.x86_64 2.12-1.149.el6 dependency: libdl.so.2()(64bit) provider: glibc.x86_64 2.12-1.149.el6 dependency: rtld(GNU_HASH) provider: glibc.x86_64 2.12-1.149.el6 provider: glibc.i686 2.12-1.149.el6 dependency: libm.so.6()(64bit) provider: glibc.x86_64 2.12-1.149.el6 dependency: libpython2.6.so.1.0()(64bit) provider: python-libs.x86_64 2.6.6-52.el6 dependency: libpthread.so.0()(64bit) provider: glibc.x86_64 2.12-1.149.el6 dependency: libc.so.6(GLIBC_2.2.5)(64bit) provider: glibc.x86_64 2.12-1.149.el6
yum此前执行的命令历史
yum history
实例:
[root@LinuxHost dvd]# yum history Loaded plugins: fastestmirror, refresh-packagekit, security ID | Login user | Date and time | Action(s) | Altered ------------------------------------------------------------------------------- 19 | root <root> | 2014-11-26 02:51 | Erase | 1 18 | root <root> | 2014-11-26 02:49 | Downgrade | 1 < 17 | root <root> | 2014-11-25 07:08 | Install | 1 > 16 | root <root> | 2014-11-25 06:46 | Install | 3 15 | root <root> | 2014-11-25 04:02 | Install | 1 14 | root <root> | 2014-11-24 13:35 | Install | 3 13 | root <root> | 2014-11-24 13:14 | Install | 1 12 | root <root> | 2014-11-24 12:13 | Install | 1 11 | root <root> | 2014-11-24 12:12 | Install | 10 10 | root <root> | 2014-11-24 12:10 | Install | 34 9 | root <root> | 2014-11-24 11:43 | Install | 1 8 | root <root> | 2014-11-24 11:12 | Install | 15 7 | root <root> | 2014-11-24 11:11 | Install | 1 6 | root <root> | 2014-11-24 08:58 | Install | 33 < 5 | root <root> | 2014-11-21 13:14 | Install | 1 > 4 | root <root> | 2014-11-12 20:21 | Install | 1 3 | root <root> | 2014-11-12 19:25 | Install | 1 2 | root <root> | 2014-11-12 11:14 | Install | 5 1 | System <unset> | 2014-11-12 09:42 | Install | 988 history list
检查本地yum状态
yum check
实例:
[root@LinuxHost dvd]# yum check Loaded plugins: fastestmirror, refresh-packagekit, security check all
检查需要很长时间。
至此程序包管理工具介绍完结!!!谢谢浏览!!!!
本文出自 “龙之守护” 博客,请务必保留此出处http://sohudrgon.blog.51cto.com/3088108/1582757
Linux下程序包管理之YUM