【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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM