【Linux笔记】CentOS&RHEL YUM基础知识

以下内容收集自网络,以作参考。

一、YUM是什么

YUM = Yellow dog Updater, Modified。

主要功能是更方便的添加/删除/更新RPM包。

它能自动解决包的倚赖性问题。

它能便于管理大量系统的更新问题。

二、 YUM的特点

可以同时配置多个资源库(Repository)。

简洁的配置文件(/etc/yum.conf,/etc/yum.repos.d 下的文件)。

自动解决增加或删除rpm包时遇到的倚赖性问题。

使用方便。

保持与RPM数据库的一致性。

三、 YUM安装

CentOS/RHEL:[root@localhost ~]# rpm -ivh yum-3.2.29-30.el6.centos.noarch

四、YUM配置

1、/etc/yum.conf配置文件

yum的配置信息储存在/etc/yum.conf的配置文件中,如下(该配置文件为CentOS-6.3-x64-yum 3.2.29版本,不同版本的配置文件略有不同。):

[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
exclude=*.i?86 kernel kernel-xen kernel-debug
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

#  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

[main]部份是yum的全局配置项,各项的详解如下:

cachedir:yum缓存的目录,yum在此存储下载的rpm包和数据库,一般是/var/cache/yum。

keepcache:是否保持yum缓存,默认为0。

debuglevel:除错级别,0-10,默认是2。

logfile:yum的日志文件,默认是/var/log/yum.log。

exactarch:有两个选项1和0,代表是否只升级和你安装软件包cpu体系一致的包,如果设为1,则如你安装了一个i386的rpm,则yum不会用1686的包来升级。

gpgchkeck: 有1和0两个选择,分别代表是否是否进行gpg校验,如果没有这一项,默认好像也是检查的。

plugins:是否允许使用插件,默认是0不允许。

exclude:排除某些软件在升级名单之外,可以用通配符,列表中各个项目要用空格隔开,这个对于安装了诸如美化包,中文补丁的朋友特别有用。

installonly_limit:允许保留多少个内核包。

distroverpkg:指定一个软件包,yum会根据这个包判断你的发行版本,默认是redhat-release,也可以是安装的任何针对自己发行版的rpm包。

pkgpolicy:包的策略。一共有两个选项,newest和last,这个作用是如果你设置了多个repository,而同一软件在不同的repository中同时存在,yum应该安装哪一个,如果是newest,则yum会安装最新的那个版本。如果是last,则yum会将服务器id以字母表排序,并选择最后的那个服务器上的软件安装。一般都是选newest。

retries:网络连接发生错误后的重试次数,如果设为0,则会无限重试。

tolerent:也有1和0两个选项,表示yum是否容忍命令行发生与软件包有关的错误,比如你要安装1,2,3三个包,而其中3此前已经安装了,如果你设为1,则yum不会出现错误信息。默认是0。

2、/etc/yum.repos.d/*.repo

该目录下的所有*.repo文件是系统中yum源(软件仓库)的配置文件,通常一个repo文件定义了一个或者多个软件仓库的细节内容,例如我们将从哪里下载需要安装或者升级的软件包,repo文件中的设置内容将被yum读取和应用。

以/etc/yum.repos.d/atomic.repo文件为实例,如下:

# Name: Atomic Rocket Turtle RPM Repository for CentOS / Red Hat Enterprise Linux 6 - 
# URL: http://www.atomicrocketturtle.com/
# Note: This isn't covered by ASL support. -Scott
[atomic] #方框号里的是软件源的名称,将被yum取得并识别。 
name = CentOS / Red Hat Enterprise Linux $releasever - atomicrocketturtle.com  #name定义了软件 仓库的名称,通常是为了方便阅读配置文件,一般没什么作用,$releasever变量定义了发行版本,通常是8,9,10等数字,$basearch变 量定义了系统的架构,可以是i386、x86_64、ppc等值,这两个变量根据当前系统的版本架构不同而有不同的取值,这可以方便yum升级的时候选择 适合当前系统的软件包,以下相同。
mirrorlist = http://www.atomicorp.com/mirrorlist/atomic/centos-6-$basearch  #mirrorlist指定一个镜像服务器的地址列表,通常是开启的,我们可以试试,将$releasever和$basearch替换成自己对应的版本和架构,例如10和i386,在浏览器中打开,我们就能看到一长串镜可用的镜像服务器地址列表。
#mirrorlist = http://www.atomicorp.com/channels/atomic/centos/6/mirrors-atomic
enabled = 1 #这个选项表示这个repo中定义的源是启用的,0为禁用
priority = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY.art.txt  #定义用于校验的gpg密钥
gpgcheck = 1 #这个选项表示这个repo中下载的rpm将进行gpg的校验,已确定rpm包的来源是有效和安全的

# Almost Stable, release candidates for [atomic]
[atomic-testing]
name = CentOS / Red Hat Enterprise Linux $releasever - atomicrocketturtle.com - (Testing)
mirrorlist = http://www.atomicorp.com/mirrorlist/atomic-testing/centos-6-$basearch
enabled = 0
priority = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY.art.txt
gpgcheck = 1

# Untested, Unstable, known buggy, and incomplete packages. 
[atomic-bleeding]
name = CentOS / Red Hat Enterprise Linux $releasever - atomicrocketturtle.com - (Bleeding)
baseurl = http://www.atomicorp.com/channels/atomic-bleeding/centos/$releasever/$basearch/
enabled = 0
priority = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY.art.txt
gpgcheck = 1

 五、扩展RPM包

1、rpmforge

Centos/RHEL默认的yum软件仓库非常有限,仅仅限于发行版本那几张盘里面的常规包和一些软件包的更新,利用rpmforge,可以增加非常多的第三方rpm软件包。

获取

http://dag.wieers.com/rpm/packages/rpmforge-release/

找到与系统平台架构匹配的rpmforge包安装之即可。安装完后.默认会在系统的/etc/yum.repos.d/下新增两个文件,mirrors-rpmforge和rpmforge.repo

安装

[root@localhost ~]# rpm -ivh rpmforge-release-0.3.6-1.el4.rf.i386.rpm

[root@localhost ~]# rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt

2、加速yum更新速度

安装yum的fast mirror插件,可以加快 CentOS/RHEL yum的速度和提高稳定性,效果显著。

[root@localhost ~]# yum -y install yum-fastestmirror

六、YUM使用

注:当第一次使用yum或yum资源库有更新时,yum会自动下载所有所需的headers放置于/var/cache/yum目录下,所需时间可能较长.

■ yum更新:

系统更新(更新所有可以升级的rpm包,包括kernel)

[root@localhost ~]# yum -y update 

每天定期执行系统更新

[root@localhost ~]# chkconfig yum on

[root@localhost ~]# service yum start 

rpm包的更新,检查可更新的rpm包

[root@localhost ~]# yum check-update

更新所有的rpm包

[root@localhost ~]# yum update

更新指定的rpm包,如更新kernel和kernel source

[root@localhost ~]# yum update kernel kernel-source

大规模的版本升级,与yum update不同的是,连旧的淘汰的包也升级

[root@localhost ~]# yum upgrade

■ yum安装和删除:

安装rpm包,如xmms-mp3

[root@localhost ~]# yum install xmms-mp3

删除rpm包,包括与该包有倚赖性的包

[root@localhost ~]# yum remove licq

注:同时会提示删除licq-gnome,licq-qt,licq-text

■ yum暂存(/var/cache/yum/)的相关参数:

清除暂存中rpm包文件

[root@localhost ~]# yum clean packages

清除暂存中rpm头文件

[root@localhost ~]# yum clean headers

清除暂存中旧的rpm头文件

[root@localhost ~]# yum clean oldheaders

清除暂存中旧的rpm头文件和包文件

[root@localhost ~]# yum clean 或#yum clean all

注:相当于yum clean packages + yum clean oldheaders

■ rpm包列表:

列出资源库中所有可以安装或更新的rpm包

[root@localhost ~]# yum list 

列出资源库中特定的可以安装或更新以及已经安装的rpm包

[root@localhost ~]# yum list mozilla

[root@localhost ~]# yum list mozilla*

注:可以在rpm包名中使用匹配符,如列出所有以mozilla开头的rpm包

列出资源库中所有可以更新的rpm包

[root@localhost ~]# yum list updates

列出已经安装的所有的rpm包

[root@localhost ~]# yum list installed

列出已经安装的但是不包含在资源库中的rpm包

[root@localhost ~]# yum list extras

注:通过其它网站下载安装的rpm包

■ rpm包信息显示(info参数同list):

列出资源库中所有可以安装或更新的rpm包的信息

[root@localhost ~]# yum info 

列出资源库中特定的可以安装或更新以及已经安装的rpm包的信息

[root@localhost ~]# yum info mozilla

[root@localhost ~]# yum info mozilla*

注:可以在rpm包名中使用匹配符,如列出所有以mozilla开头的rpm包的信息

列出资源库中所有可以更新的rpm包的信息

[root@localhost ~]# yum info updates

列出已经安装的所有的rpm包的信息

[root@localhost ~]# yum info installed

列出已经安装的但是不包含在资源库中的rpm包的信息

[root@localhost ~]# yum info extras

注:通过其它网站下载安装的rpm包的信息 

■ 搜索rpm包

搜索匹配特定字符的rpm包

[root@localhost ~]# yum search mozilla

注:在rpm包名,包描述等中搜索

搜索有包含特定文件名的rpm包

[root@localhost ~]# yum provides realplay

七、YUM常用问题解决

1、如果网速慢的话可以通过增加yum的超时时间,这样就不会总是因为超时而退出。

#vi /etc/yum.conf

#加上这么一句

timeout=120

2、yum Existing lock错误的解决办法

如果系统启动的时候, yum出现Existing lock /var/run/yum.pid: another copy is running as pid 3380. Aborting.可以用下面的办法解决:

方法一:etc/init.d/yum-updatesd stop

方法二:rm -f /var/run/yum.pid

主要原因就是yum在自动更新,只要关掉它就可以了。

八、更改YUM源实例

1、yum来源地址

网易:http://mirrors.163.com/.help/CentOS6-Base-163.repo  

搜狐:wget http://mirrors.sohu.com/help/CentOS-Base-sohu.repo

2、更改yum源

[root@localhost ~]#  cd /etc/yum.repos.d/ 

[root@localhost ~]#  wget http://mirrors.163.com/.help/CentOS6-Base-163.repo #yum来源地址

[root@localhost ~]#  vi CentOS6-Base-163.repo #编辑文件,把文件里面的$releasever全部替换为版本号,即6(我当前系统为CeotOS6.3),最后保存!

3、清理yum缓存

[root@localhost ~]# yum clean all

[root@localhost ~]# yum makecache #将服务器上的软件包信息缓存到本地,以提高搜索安装软件的速度

[root@localhost ~]# yum install vim* #测试新yum源是否可用

至此,CentOS/RHEL可以使用yum源在线安装软件了!

4、 安装插件

最后,可以考虑装个插件,yum install yum-fastestmirror -y,这个会自动选择快的那个源。

5、结语

如果我在那个目录里放了很多源,怎么知道当前用的是那个呢?如果你没有改yum.conf,那么看/var/cache/yum/文件夹里的一个time开头的文件,就知道了。

附:链接来源

■ http://blog.csdn.net/yuandianlws/article/details/7468287

■ http://www.cnblogs.com/shuaixf/archive/2011/11/30/2268496.html

■ http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/10/09/2203916.html

原文地址:https://www.cnblogs.com/lyongde/p/3187328.html