2021-05-12 14:32:11
Linux系統之前端程式包管理器-yum
【前言】
當我們使用rpm命令安裝程式時,時常會因為程式包之間的依賴關係而焦頭爛額,使得很多程式的安裝過程複雜而費力,那有沒有一種工具可以幫我們解決這個問題呢?當然有啦!這就是我們今天的重點yum,讓我們一塊來學習總結下吧!
【什麼是yum】
yum(全稱為 Yellow dog Updater, Modified)是一個在Fedora和RedHat以及SUSE中的Shell前端軟體包管理器。基於RPM包管理,能夠從指定的伺服器自動下載RPM包並且安裝,可以自動處理依賴性關係,並且一次安裝所有依賴的軟體包,無須繁瑣地一次次下載、安裝。yum提供了查詢、安裝、刪除某一個、一組甚至全部軟體包的命令,而且命令簡潔而又好記。
【yum的工作機制】
yum 的關鍵之處是要有可靠的yum倉庫(yum repository),它可以是http 或ftp 站點,也可以是本地軟體池,其必須包含兩類資料:
1、資料:也就是各個rpm包;
2、後設資料:其包含了rpm 包的各種資訊,包括包名、描述,功能,提供的檔案,依賴關係、包分組資訊等。正是通過收集彙總這些後設資料 並加以分析,才能自動化地完成餘下的任務。在各版本系統下其的儲存格式為:CentOS5: *.xml; centos6,7: *.sqlite
其實從架構的角度來看,yum的工作機制為C/S架構,如下圖所示:
【yum的使用機制】
1、確保有yum repo(yum倉庫的組態檔)可用;rpm包的檔案伺服器,repodata目錄所在父目錄就是一個可用倉庫。
2、yum用戶端需要指供repo組態檔,指明yum倉庫存取路徑及各種屬性資訊。有兩種設定方法:
法一:修改主組態檔(中心組態檔):/etc/yum.conf;
法二:在/etc/yum.repos.d/下建立或修改*.repo檔案(儲存一個或幾個相關倉庫的設定資訊)。
在.repo檔案定義一個yum repo指向的格式:
[REPOID] #定義軟體源的名稱,該名稱是可以自定義,同時在該伺服器上所有repo檔案中是唯一的。注意[]中不能有空格;
name=Some name for this repository #定義軟體倉庫的名稱,支援像$releasever 、$basearch這樣的變數;注意=兩邊不能有空格;
baseurl=url://server/path/to/repository #指定真正的倉庫所在地,可以給定多個,下面的URL不能頂格書寫,URL中也支援$releasever 、$basearch這樣的變數;
http://mirrors.sohu.com/centos/$releasever/os/$basearch
enabled={0|1} #設定此倉庫是否啟用,預設為1生效;
gpgcheck={0|1} #設定是否驗證安裝包,0為不進行驗證,1為進行驗證;
gpgkey=URL #指定用於驗證的秘鑰檔案
cost={1..n} #預設為1000,指定存取此倉庫的開銷(在用戶端指定)
補充說明:
1、$basearch: 系統基本架構,如i686,i586等的基本架構為i386;
$releasever: 程式的版本,對yum而言指的是redhat-release版本;只替換為主版本號,如RedHat 6.5,則替換為6;
2 、baseurl種url支援的協定有:http://、ftp://、nfs://、file://、 rsync://;當需要指定多個URL且不想通過baseurl來指定時, 我們可以把多個URL存放在一個檔案裡,通過mirrorlist=存放URL的檔案來指定yum倉庫。
3、一個.repo檔案可以定義多個yum倉庫。
[root@CentOS6 yum.repos.d]# ls /etc/yum.repos.d/
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
[root@CentOS6 mnt]# cat /etc/yum.repos.d/CentOS-Media.repo
[c6-media]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
file:///media/cdrom/
file:///media/cdrecorder/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
[root@CentOS6 yum.repos.d]#
【yum用戶端命令的使用】
1、列出所有可用repo
yum repolist {enabled|disabled|all} enabled:可用的;disabled:不可用的;all:所有的 ,不帶引數預設為列出可用的
[root@CentOS6 yum.repos.d]# yum repolist enabled
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
repo id repo name status
c6-cd CentOS-6 - cd 4,802
repolist: 4,802
[root@CentOS6 yum.repos.d]# yum repolist disabled
Loaded plugins: fastestmirror, refresh-packagekit, security
repo id repo name
c6-media CentOS-6 - Media
repolist: 0
[root@CentOS6 yum.repos.d]# yum repolist all
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
repo id repo name status
c6-cd CentOS-6 - cd enabled: 4,802
c6-media CentOS-6 - Media disabled
repolist: 4,802
[root@CentOS6 yum.repos.d]#
2、列出rpm包
yum list {all|installed|available} all:所有的;installed: 已安裝的;available:可以安裝的;預設為列出所有的
yum list KEYWORD* 支援使用檔名通配,來獲取含有關鍵字的rpm包
[root@CentOS6 yum.repos.d]# yum list http* #列出所有以http開頭的rpm包
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Installed Packages #下面為已經安裝的包
httpd-tools.i686 2.2.15-29.el6.centos @anaconda-CentOS-201311271240.i386/6.5
Available Packages #下面為可以安裝的包
httpd.i686 2.2.15-29.el6.centos c6-cd
httpd-devel.i686 2.2.15-29.el6.centos c6-cd
httpd-manual.noarch 2.2.15-29.el6.centos c6-cd
3、列出包的描述資訊(無論包有沒有安裝都可以列出)
yum info package_name
[root@CentOS6 yum.repos.d]# yum info httpd #列出http包的描述資訊
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Available Packages
Name : httpd
Arch : i686
Version : 2.2.15
Release : 29.el6.centos
Size : 828 k
Repo : c6-cd
Summary : Apache HTTP Server
URL : http://httpd.apache.org/
License : ASL 2.0
Description : The Apache HTTP Server is a powerful, efficient, and extensible
: web server.
4、列出所有的包組資訊
yum grouplist
[root@CentOS6 yum.repos.d]# yum grouplist
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Group Process
Loading mirror speeds from cached hostfile
c6-cd/group_gz | 212 kB 00:00 ...
Installed Groups: #已安裝的包組
Additional Development
Base
...
Installed Language Groups: #已安裝的語言套件組
Arabic Support [ar]
...
Available Groups: #可安裝的包組
Backup Client
...
Available Language Groups: #可安裝的語言套件組
Afrikaans Support [af]
...
Done
5、顯示包組的資訊:列出組中包含的程式包列表(如果包組名中含有空格必須用“”引起來)
yum groupinfo "GROUP NAME"
[root@CentOS6 yum.repos.d]# yum groupinfo "Development Tools" #顯示開發工具包組的資訊
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
...
Default Packages:
byacc
...
Optional Packages:
ElectricFence
...
CentOS6 跟開發相關的包組有: Development Tools、Server Platform Development、 Desktop Platform Development
6、清除快取
yum clean {all|packages|metadata|expire-cache|rpmdb|plugins}
yum clean all 清除快取目錄(/var/cache/yum)下的軟體包以及舊的後設資料
[root@CentOS6 yum.repos.d]# yum clean all
Loaded plugins: fastestmirror, refresh-packagekit, security
Cleaning repos: c6-cd
Cleaning up Everything
Cleaning up list of fastest mirrors
7、安裝程式包
yum install package_name {-y} -y:自動回答為yes,執行過程中無需等待使用者再次輸入
[root@CentOS6 yum.repos.d]# yum install httpd
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
c6-cd | 4.0 kB 00:00 ...
c6-cd/primary_db | 3.4 MB 00:00 ...
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package httpd.i686 0:2.2.15-29.el6.centos will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=================================================================================
Package Arch Version Repository Size
=================================================================================
Installing:
httpd i686 2.2.15-29.el6.centos c6-cd 828 k
Transaction Summary
================================================================================
Install 1 Package(s)
Total download size: 828 k
Installed size: 2.8 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.
** Found 1 pre-existing rpmdb problem(s), 'yum check' output follows:
gnome-user-share-2.28.2-3.el6.i686 has missing requires of httpd >= ('0', '2.2.0', None)
Installing : httpd-2.2.15-29.el6.centos.i686 1/1
Verifying : httpd-2.2.15-29.el6.centos.i686 1/1
Installed:
httpd.i686 0:2.2.15-29.el6.centos
Complete!
重新安裝:
yum reinstall package_name
8、升級
yum check-update: 檢查可用的升級包
yum update package_name 升級,當不指定版本時,預設升級到最新版本
yum downgrade package_name 降價到指定版本
9、解除安裝(解除安裝時為解決該包安裝的依賴關係而安裝的包也會一同解除安裝)
yum remove|erase package_name {-y} -y:自動回答為yes,執行過程中無需等待使用者再次輸入
10、查詢某檔案是由哪個包安裝生成的
yum whatprovides|provides /path/to/somefile
[root@CentOS6 yum.repos.d]# yum provides /usr/bin/zip #查詢出zip檔案是由那個包安裝生成;
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
zip-3.0-1.el6.i686 : A file compression and packaging utility compatible with PKZIP
Repo : c6-cd
Matched from:
Filename : /usr/bin/zip
zip-3.0-1.el6.i686 : A file compression and packaging utility compatible with PKZIP
Repo : installed
Matched from:
Other : Provides-match: /usr/bin/zip
11、安裝包組
yum groupinstall "GROUP NAME"
12、解除安裝包組
yum groupremove "GROUP NAME"
【自建yum倉庫】
下面通過自建一個http伺服器的yum倉庫為例說明:
第一步:安裝httpd程式並啟動服務;
yum -y install httpd
service httpd start
chkconfig httpd on
12345678910111213141516171819202122232425262728293031323334353637 [root@CentOS6 yum.repos.d]# yum -y install httpd
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package httpd.i686 0:2.2.15-29.el6.centos will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
httpd i686 2.2.15-29.el6.centos c6-cd 828 k
Transaction Summary
================================================================================
Install 1 Package(s)
Total download size: 828 k
Installed size: 2.8 M
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : httpd-2.2.15-29.el6.centos.i686 1/1
Verifying : httpd-2.2.15-29.el6.centos.i686 1/1
Installed:
httpd.i686 0:2.2.15-29.el6.centos
Complete!
[root@CentOS6 yum.repos.d]# service httpd start
Starting httpd: [ OK ]
[root@CentOS6 yum.repos.d]# chkconfig httpd on
[root@CentOS6 yum.repos.d]# chkconfig |grep httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
第二步:建立子目錄,存放某相關的所有rpm包,httpd的文件根目錄為/var/www/html;為倉庫生成後設資料檔案(使用createrepo),以使能夠作為倉庫使用。
[root@CentOS6 html]# cd /var/www/html
[root@CentOS6 html]# mkdir test #建立test目錄用來測試;
[root@CentOS6 html]# cp /mnt/cd/Packages/zsh-4.3.10-7.el6.i686.rpm ./test #複製zsh這個安裝包到test目錄;
[root@CentOS6 Packages]# yum install createrepo #安裝createrepo,如提前已安裝則不用再裝;
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package createrepo.noarch 0:0.9.9-18.el6 will be installed
--> Processing Dependency: Python-deltarpm for package: createrepo-0.9.9-18.el6.noarch
--> Running transaction check
---> Package python-deltarpm.i686 0:3.5-0.5.20090913git.el6 will be installed
--> Processing Dependency: deltarpm = 3.5-0.5.20090913git.el6 for package: python-deltarpm-3.5-0.5.20090913git.el6.i686
--> Running transaction check
---> Package deltarpm.i686 0:3.5-0.5.20090913git.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================
Package Arch Version Repository Size
======================================================================
Installing:
createrepo noarch 0.9.9-18.el6 c6-cd 94 k
Installing for dependencies:
deltarpm i686 3.5-0.5.20090913git.el6 c6-cd 73 k
python-deltarpm i686 3.5-0.5.20090913git.el6 c6-cd 27 k
Transaction Summary
==========================================================================
Install 3 Package(s)
Total download size: 195 k
Installed size: 522 k
Is this ok [y/N]: y
Downloading Packages:
-----------------------------------------------------------------------------------------
Total 3.3 MB/s | 195 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : deltarpm-3.5-0.5.20090913git.el6.i686 1/3
Installing : python-deltarpm-3.5-0.5.20090913git.el6.i686 2/3
Installing : createrepo-0.9.9-18.el6.noarch 3/3
Verifying : createrepo-0.9.9-18.el6.noarch 1/3
Verifying : python-deltarpm-3.5-0.5.20090913git.el6.i686 2/3
Verifying : deltarpm-3.5-0.5.20090913git.el6.i686 3/3
Installed:
createrepo.noarch 0:0.9.9-18.el6
Dependency Installed:
deltarpm.i686 0:3.5-0.5.20090913git.el6
python-deltarpm.i686 0:3.5-0.5.20090913git.el6
Complete! #createrepo安裝完成
[root@CentOS6 Packages]# createrepo /var/www/html/test/ #建立後設資料檔案;
Spawning worker 0 with 1 pkgs
Workers Finished
Gathering worker results
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@CentOS6 Packages]# ls /var/www/html/test/
repodata zsh-4.3.10-7.el6.i686.rpm #repodata即為剛剛建立出來存放後設資料檔案的目錄;
[root@CentOS6 test]# ls /var/www/html/test/repodata/ #檢視repodata下的後設資料檔案;
105b962a2624ebddd589ec03c1e4fdcd7a3511ea1e47fd80eeda364b41d48cdd-primary.sqlite.bz2
7762e79a7341d1b6a929e893fd732302dd27499742abf4549130601465760426-filelists.xml.gz
78f72a947bd046641a5ea0574872212cfe829f1525ff74f1d13f4da3c4d73cde-filelists.sqlite.bz2
7e8d7b7938dbd28a823ebe27420f3c2d088f9a1933f37319e925295e24417009-other.sqlite.bz2
885867e505b55bc3f09fb4eec3bc7cf8a5352cce29c307e7b37afaa38e3d33cc-primary.xml.gz
e0eb2191ed5de3b3975bbb005a00e07a044d5724b87373d4a6cdf399ff99bf3f-other.xml.gz
#通過以上幾步一個簡單的yum倉庫就搭建好了!
第三步:設定yum用戶端使用此倉庫即可;
[root@CentOS6 yum.repos.d]# vim test.repo #編輯指向yum庫的repo檔案
[c6-test]
name=CentOS-$releasever - test
baseurl=file:///var/www/html/test
gpgcheck=0 #此處只為實驗,沒有複製金鑰,到yum庫中,所以不啟用校驗;
enabled=1 #啟用
[root@CentOS6 yum.repos.d]# yum clean all #清除快取;
Loaded plugins: fastestmirror, refresh-packagekit, security
Cleaning repos: c6-test
Cleaning up Everything
Cleaning up list of fastest mirrors
[root@CentOS6 yum.repos.d]# yum repolist #顯示可用的repo;
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
c6-test | 2.9 kB 00:00 ...
c6-test/primary_db | 3.2 kB 00:00 ...
repo id repo name status
c6-test CentOS-6 - test 1
repolist: 1
[root@CentOS6 yum.repos.d]# yum -y install zsh #安裝zsh;
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package zsh.i686 0:4.3.10-7.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================
Package Arch Version Repository Size
==========================================================================
Installing:
zsh i686 4.3.10-7.el6 c6-test 2.1 M
Transaction Summary
==========================================================================
Install 1 Package(s)
Total download size: 2.1 M
Installed size: 4.6 M
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : zsh-4.3.10-7.el6.i686 1/1
Verifying : zsh-4.3.10-7.el6.i686 1/1
Installed:
zsh.i686 0:4.3.10-7.el6
Complete!
#安裝成功!實驗完成!
好啦!可能描寫的比較囉嗦,如有總結說明不妥的地方,還請各位見諒!謝謝!
RedHat 6.2 Linux修改yum源免費使用CentOS源 http://www.linuxidc.com/Linux/2013-07/87383.htm
設定EPEL YUM源 http://www.linuxidc.com/Linux/2012-10/71850.htm
Redhat 本地yum源設定 http://www.linuxidc.com/Linux/2012-11/75127.htm
yum的組態檔說明 http://www.linuxidc.com/Linux/2013-04/83298.htm
RedHat 6.1下安裝yum(圖文) http://www.linuxidc.com/Linux/2013-06/86535.htm
YUM 安裝及清理 http://www.linuxidc.com/Linux/2013-07/87163.htm
CentOS 6.4上搭建yum本地源 http://www.linuxidc.com/Linux/2014-07/104533.htm
本文永久更新連結地址:http://www.linuxidc.com/Linux/2015-05/117261.htm
相關文章