標籤:
一、yum 簡介
yum,是Yellow dog Updater, Modified 的簡稱,是杜克大學為了提高RPM 軟體包安裝性而開發的一種軟體包管理器。起初是由yellow dog 這一發行版的開發人員Terra Soft 研發,用python 寫成,那時還叫做yup(yellow dog updater),後經杜克大學的[email protected] Team Dev進行改進,遂有此名。yum 的宗旨是自動化地升級,安裝/移除rpm 包,收集rpm 包的相關資訊,檢查依賴性並自動提示使用者解決。yum 的關鍵之處是要有可靠的repository,顧名思義,這是軟體的倉庫,它可以是http 或ftp 網站,也可以是本地軟體池,但必須包含rpm 的header,header 包括了rpm 包的各種資訊,包括描述,功能,提供的檔案,依賴性等。正是收集了這些header 並加以分析,才能自動化地完成餘下的任務。
yum 的理念是使用一個中心倉庫(repository)管理一部分甚至一個distribution 的應用程式相互關係,根據計算出來的軟體依賴關係進行相關的升級、安裝、刪除等等操作,減少了Linux 使用者一直頭痛的dependencies 的問題。這一點上,yum 和apt 相同。apt 原為debian 的deb 類型軟體管理所使用,但是現在也能用到RedHat 門下的rpm 了。
yum 主要功能是更方便的添加/刪除/更新RPM 包,自動解決包的倚賴性問題,便於管理大量系統的更新問題。
yum 可以同時配置多個資產庫(Repository),簡潔的設定檔(/etc/yum.conf),自動解決增加或刪除rpm 包時遇到的依賴性問題,保持與RPM 資料庫的一致性。
二、yum 安裝
CentOS 預設已經安裝了yum,不需要另外安裝,這裡為了實驗目的,先將yum 卸載再重新安裝。
1、查看系統預設安裝的yum
# rpm -qa|grep yum
2、卸載yum
# rpm -e yum-fastestmirror-1.1.16-14.el5.centos.1 yum-metadata-parser-1.1.2-3.el5.centos yum-3.2.22-33.el5.centos
3、重新安裝yum
這裡可以通過wget 從網上下載相關包安裝,也可以掛載系統安裝光碟片進行安裝,這裡選擇掛載系統安裝光碟片進行安裝。
# mount /dev/cdrom /mnt/cdrom/
# rpm -ivh yum-3.2.22-33.el5.centos.noarch.rpm yum-fastestmirror-1.1.16-14.el5.centos.1.noarch.rpm yum-metadata-parser-1.1.2-3.el5.centos.i386.rpm
# yum -v
yum 的基礎安裝包包括:
yum //RPM installer/updater
yum-fastestmirror //Yum plugin which chooses fastest repository from a mirrorlist
yum-metadata-parser //A fast metadata parser for yum
其他安裝包根據自己需要安裝。
三、yum 配置
yum 的設定檔分為兩部分:main 和repository
main 部分定義了全域配置選項,整個yum 設定檔應該只有一個main。常位於/etc/yum.conf 中。
repository 部分定義了每個源/伺服器的具體配置,可以有一到多個。常位於/etc/yum.repo.d 目錄下的各檔案中。
yum.conf 檔案一般位於/etc目錄下,一般其中只包含main部分的配置選項。
# cat /etc/yum.conf
[main]
cachedir=/var/cache/yum
//yum 緩衝的目錄,yum 在此儲存下載的rpm 包和資料庫,預設設定為/var/cache/yum
keepcache=0
//安裝完成後是否保留軟體包,0為不保留(預設為0),1為保留
debuglevel=2
//Debug 資訊輸出等級,範圍為0-10,預設為2
logfile=/var/log/yum.log
//yum 記錄檔位置。使用者可以到/var/log/yum.log 檔案去查詢過去所做的更新。
pkgpolicy=newest
//包的策略。一共有兩個選項,newest 和last,這個作用是如果你設定了多個repository,而同一軟體在不同的repository 中同時存在,yum 應該安裝哪一個,如果是newest,則yum 會安裝最新的那個版本。如果是last,則yum 會將伺服器id 以字母表排序,並選擇最後的那個伺服器上的軟體安裝。一般都是選newest。
distroverpkg=redhat-release
//指定一個軟體包,yum 會根據這個包判斷你的發行版本,預設是redhat-release,也可以是安裝的任何針對自己發行版的rpm 包。
tolerant=1
//有1和0兩個選項,表示yum 是否容忍命令列發生與軟體包有關的錯誤,比如你要安裝1,2,3三個包,而其中3此前已經安裝了,如果你設為1,則yum 不會出現錯誤資訊。預設是0。
exactarch=1
//有1和0兩個選項,設定為1,則yum 只會安裝和系統架構匹配的軟體包,例如,yum 不會將i686的軟體包安裝在適合i386的系統中。預設為1。
retries=6
//網路連接發生錯誤後的重試次數,如果設為0,則會無限重試。預設值為6.
obsoletes=1
//這是一個update 的參數,具體請參閱yum(8),簡單的說就是相當於upgrade,允許更新陳舊的RPM包。
plugins=1
//是否啟用外掛程式,預設1為允許,0表示不允許。我們一般會用yum-fastestmirror這個外掛程式。
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=16&ref=http://bugs.centos.org/bug_report_page.php?category=yum
# Note: yum-RHN-plugin doesn‘t honor this.
metadata_expire=1h
installonly_limit = 5
# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
除了上述之外,還有一些可以添加的選項,如:
exclude=selinux* // 排除某些軟體在升級名單之外,可以用萬用字元,列表中各個項目要用空格隔開,這個對於安裝了諸如美化包,中文補丁的朋友特別有用。
gpgcheck=1 // 有1和0兩個選擇,分別代表是否是否進行gpg(GNU Private Guard) 校正,以確定rpm 包的來源是有效和安全的。這個選項如果設定在[main]部分,則對每個repository 都有效。預設值為0。
四、配置本地yum源
1、掛載系統安裝光碟片
# mount /dev/cdrom /mnt/cdrom/
2、配置本地yum源
# cd /etc/yum.repos.d/
# ls
會看到四個repo 檔案
CentOS-Base.repo 是yum 網路源的設定檔
CentOS-Media.repo 是yum 本地源的設定檔
修改CentOS-Media.repo
# cat CentOS-Media.repo
# CentOS-Media.repo
#
# This repo is used to mount the default locations for a CDROM / DVD on
# CentOS-5. You can use this repo and yum to install items directly off the
# DVD ISO that we release.
#
# To use this repo, put in your DVD and use it with the other repos too:
# yum --enablerepo=c5-media [command]
#
# or for ONLY the media repo, do this:
#
# yum --disablerepo=\* --enablerepo=c5-media [command]
[c5-media]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
file:///mnt/cdrom/
file:///media/cdrecorder/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
在baseurl 中修改第2個路徑為/mnt/cdrom(即為光碟片掛載點)
將enabled=0改為1
3、禁用預設的yum 網路源
將yum 網路源設定檔改名為CentOS-Base.repo.bak,否則會先在網路源中尋找適合的包,改名之後直接從本地源讀取。
4、執行yum 命令
# yum install postgresql
關於repo 檔案的格式
所有repository 伺服器設定都應該遵循如下格式:
[serverid]
name=Some name for this server
baseurl=url://path/to/repository/
serverid 是用於區別各個不同的repository,必須有一個獨一無二的名稱;
name 是對repository 的描述,支援像$releasever $basearch這樣的變數;
baseurl 是伺服器設定中最重要的部分,只有設定正確,才能從上面擷取軟體。它的格式是:
baseurl=url://server1/path/to/repository/
url://server2/path/to/repository/
url://server3/path/to/repository/
其中url 支援的協議有 http:// ftp:// file:// 三種。baseurl 後可以跟多個url,你可以自己改為速度比較快的鏡像站,但baseurl 只能有一個,也就是說不能像如下格式:
baseurl=url://server1/path/to/repository/
baseurl=url://server2/path/to/repository/
baseurl=url://server3/path/to/repository/
其中url 指向的目錄必須是這個repository header 目錄的上一級,它也支援$releasever $basearch 這樣的變數。
url 之後可以加上多個選項,如gpgcheck、exclude、failovermethod 等,比如:
[updates-released]
name=Fedora Core $releasever - $basearch - Released Updates
baseurl=http://download.atrpms.net/mirrors/fedoracore/updates/$releasever/$basearch
http://redhat.linux.ee/pub/fedora/linux/core/updates/$releasever/$basearch
http://fr2.rpmfind.net/linux/fedora/core/updates/$releasever/$basearch
gpgcheck=1
exclude=gaim
failovermethod=priority
其中gpgcheck,exclude 的含義和[main] 部分相同,但只對此伺服器起作用,failovermethode 有兩個選項roundrobin 和priority,意思分別是有多個url可供選擇時,yum 選擇的次序,roundrobin 是隨機播放,如果串連失敗則使用下一個,依次迴圈,priority 則根據url 的次序從第一個開始。如果不指明,預設是roundrobin。
五、配置國內yum源
系統預設的yum 源速度往往不盡人意,為了達到快速安裝的目的,在這裡修改yum源為國內源。
上海交通大學yum源
a. 修改/etc/yum.repos.d/CentOS-Base.repo為:
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://ftp.sjtu.edu.cn/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
#released updates
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
baseurl=http://ftp.sjtu.edu.cn/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
baseurl=http://ftp.sjtu.edu.cn/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
baseurl=http://ftp.sjtu.edu.cn/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
baseurl=http://ftp.sjtu.edu.cn/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
關於變數
$releasever:代表發行版的版本,從[main]部分的distroverpkg擷取,如果沒有,則根據redhat-release包進行判斷。
$arch:cpu體系,如i686,athlon等
$basearch:cpu的基本體系組,如i686和athlon同屬i386,alpha和alphaev6同屬alpha。
b. 匯入GPG KEY
yum 可以使用gpg 對包進行校正,確保下載包的完整性,所以我們先要到各個repository 網站找到gpg key,一般都會放在首頁的醒目位置,一些名字諸如RPM-GPG-KEY-CentOS-5 之類的純文字檔案,把它們下載下來,然後用rpm --import RPM-GPG-KEY-CentOS-5 命令將key 匯入。
c. 執行yum 命令
其他國內yum源列表如下:
1. 企業貢獻:
搜狐開源鏡像站:http://mirrors.sohu.com/
網易開源鏡像站:http://mirrors.163.com/
2. 大學教學:
北京理工大學:
http://mirror.bit.edu.cn (IPv4 only)
http://mirror.bit6.edu.cn (IPv6 only)
北京交通大學:
http://mirror.bjtu.edu.cn (IPv4 only)
http://mirror6.bjtu.edu.cn (IPv6 only)
http://debian.bjtu.edu.cn (IPv4+IPv6)
蘭州大學:http://mirror.lzu.edu.cn/
廈門大學:http://mirrors.xmu.edu.cn/
清華大學:
http://mirrors.tuna.tsinghua.edu.cn/ (IPv4+IPv6)
http://mirrors.6.tuna.tsinghua.edu.cn/ (IPv6 only)
http://mirrors.4.tuna.tsinghua.edu.cn/ (IPv4 only)
天津大學:http://mirror.tju.edu.cn/
中國科學技術大學:
http://mirrors.ustc.edu.cn/ (IPv4+IPv6)
http://mirrors4.ustc.edu.cn/
http://mirrors6.ustc.edu.cn/
東北大學:
http://mirror.neu.edu.cn/ (IPv4 only)
http://mirror.neu6.edu.cn/ (IPv6 only)
電子科技大學:http://ubuntu.uestc.edu.cn/
六、使用第三方軟體庫
Centos/RHEL預設的yum軟體倉庫非常有限,僅僅限於發行版本那幾張盤裡面的常規包和一些軟體包的更新,利用RpmForge,可以增加非常多的第三方rpm軟體包。RpmForge庫現在已經擁有超過10000種的CentOS的軟體包,被CentOS社區認為是最安全也是最穩定的一個第三方軟體庫。
1、安裝yum-priorities外掛程式
這個外掛程式是用來設定yum在調用軟體源時的順序的。因為官方提供的軟體源,都是比較穩定和被推薦使用的。因此,官方源的順序要高於第三方源的順序。如何保證這個順序,就需要安裝yum-priorities這外掛程式了。
# yum -y install yum-priorities
2、安裝完yum-priorities外掛程式後需要設定/etc/yum.repos.d/ 目錄下的.repo相關檔案(如CentOS-Base.repo),在這些檔案中插入順序指令:priority=N (N為1到99的正整數,數值越小越優先)
一般配置[base], [addons], [updates], [extras] 的priority=1,[CentOSplus], [contrib] 的priority=2,其他第三的軟體源為:priority=N (推薦N>10)
以CentOS-Base.repo 為例:
[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://ftp.sjtu.edu.cn/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
priority=1
3、下載與安裝相應rpmforge的rpm檔案包
# wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
4、安裝DAG的PGP Key
# rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
5、驗證rpmforge的rpm檔案包
# rpm -K rpmforge-release-0.5.2-2.el5.rf.*.rpm
6、安裝rpmforge的rpm檔案包
# rpm -i rpmforge-release-0.5.2-2.el5.rf.i386.rpm
7、設定/etc/yum.repos.d/rpmforge.repo檔案中源的層級
[[email protected] yum.repos.d]# cat rpmforge.repo
### Name: RPMforge RPM Repository for RHEL 5 - dag
### URL: http://rpmforge.net/
[rpmforge]
name = RHEL $releasever - RPMforge.net - dag
baseurl = http://apt.sw.be/redhat/el5/en/$basearch/rpmforge
mirrorlist = http://apt.sw.be/redhat/el5/en/mirrors-rpmforge
#mirrorlist = file:///etc/yum.repos.d/mirrors-rpmforge
enabled = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
gpgcheck = 1
priority=12
8、測試安裝
# yum install htop
參考 http://wiki.centos.org/AdditionalResources/Repositories/RPMForge#head-5aabf02717d5b6b12d47edbc5811404998926a1b
其他第三方軟體庫如 EPEL(Extra Packages for Enterprise Linux ) 和 RPMFusion 的安裝與使用和RPMForge類似,可自行尋找資料安裝
http://www.cnblogs.com/mchina/archive/2013/01/04/2842275.html
CentOS yum 源的配置與使用