歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux系統之前端程序包管理器-yum

Linux系統之前端程序包管理器-yum

日期:2017/2/28 14:01:42   编辑:Linux教程

【前言】
當我們使用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

Copyright © Linux教程網 All Rights Reserved