一、什么是yum
我們在Linux系統上安裝處理軟件,一般是使用RPM,它是通過預先編譯完成并且把軟件打包為RPM文件格式后,再加以安裝的一種方式,使用者只要拿到這個打包好的軟件,然后將里頭的文件放置到應該擺放的目錄,這樣就完成了安裝。但是,由于有些軟件是有依賴于其他軟件的,當你要安裝某個RPM類型的軟件時,RPM會檢驗RPM軟件數據庫,它所依賴的相關軟件包是否都已存在,如果沒有檢索到,那么這個RPM文件默認就不能安裝。甚至是有些包之間還會存在循環依賴,這時RPM就不能快速有效的進行軟件安裝了。
對于RPM的上述局限性,yum的出現就解決了包之間的依賴性的問題。文章前面提到了,RPM把軟件依賴關系儲存在本地數據庫里,那我們在安裝軟件的時候,如果先到數據庫里找到所有依賴包的列表,再檢索哪些已經安裝到本地,然后把剩下沒安裝的一起安裝,這樣就可以解決包依賴性的問題了,這就是yum機制的由來
二、yum的運作流程
各版本發行商都會釋放出軟件并放置于yum服務器上,所以yum服務器儲存有我們各種所需的軟件。參考下圖,yum服務器不僅存儲了各種RPM包,還有包的相關的元數據文件(放置于特定目錄repodata下),前面提到的包的依賴性關系就儲存在元數據文件中。這些文件與RPM軟件包所在的本地或網絡位置就被稱為yum倉庫(yum repo)。當用戶端有軟件安裝或升級的需求時,用戶端會訪問yum服務器下載或更新RPM軟件列表并存在本機緩存列表中,然后通過緩存列表與本地RPM數據庫相比對,篩選出缺少哪些RPM軟件包并根據yum倉庫儲存的路徑下載(可以是本地,也可以是網絡),最后通過RPM機制一并進行安裝。
三、如何配置yum客戶端
yum本身的配置文件,主要指向倉庫的位置以及相關的各種配置信息。
1.主配置文件–> /etc/yum.conf:為所有倉庫提供公共配置
2.各倉庫指向的定義–> /etc/yum.repos.d/*.repo:為倉庫的指向提供配置
* [repositoryID]
#倉庫的名字,具有唯一性,標識repo的指向
* name=Some name for this repository
#倉庫描述信息
* baseurl=url://path/to/repository/
#指明repo的訪問路徑,通常為一個文件服務器上輸出的某repo
#文件服務器類型
http://SEVER/PATH/TO/REPOSITORY
https://SEVER/PATH/TO/REPOSITORY
ftp://SEVER/PATH/TO/REPOSITORY
file:///PATH/TO/REPOSITORY
* enabled={1|0}
#此倉庫是否可被使用
* gpgcheck={1|0}
#是否對RPM包做檢驗
* gpgkey=url://path/to/keyfile/
#指明gpgkey文件路徑
* enablegroups={1|0}
#是否啟用包組
* failovermethod={roundrobin|priority}
#設置baseurl有多個時的優先級
roundrobin:隨機挑選,默認值
priority:按順序訪問
* cost=
#指明倉庫的訪問開銷,默認為1000
3. 創建新的配置文件
按照以上方式,我們就在/etc/yum.repos.d/目錄下創建newbase.repo的配置文件(這里要注意目錄下的配置文件只要有一個有問題就會影響yum的使用)
[root@centos7 ~]#vi /etc/yum.repos.d/newbase.repo [NewBase] name=NewBase baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/ #使用阿里云CentOS系統yum源 file:///misc/cd/ #使用本地光盤作為yum源 enable=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 #使用本地文件,也可以使用文件服務器上的,https://mirrors.aliyun.com/centos/7/os/x86_64/RPM-GPG-KEY-CentOS-7 failovermethod={priority} #按順序訪問baseurl路徑
對于CentOS來說,也支持由Fedora基金會發展的外加軟件計劃(Extra Packages for Enterprise Linux, EPEL),包含了很多第三方軟件。我們可以使用阿里云的Epel的yum源:https://mirrors.aliyun.com/epel/7/x86_64,只需在newbase.repo配置文件中創建另一個yum倉庫:
[NewEpel]
name=NewEpel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/rpm-gpg-key-epel-7
4.yum倉庫的測試
yum repolist: 列出已經配置的所有yum倉庫
用法:yum repolist [all|enabled|disabled]
[root@centos7 ~]#yum repolist all Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * NewBase: repo id repo name status !Centos7 Centos7 disabled NewBase NewBase enabled: 9,363 NewEpel NewEpel enabled: 11,787 !epel7 epel7 disabled repolist: 21,150 # NewBase和NewEpel兩個yum倉庫的status都為enabled,說明已經成功激活
四、yum命令的用法
yum [options] [command] [package …]
Command:
1.顯示服務器提供的程序包:
yum list
yum list [all | glob_exp1] [glob_exp2] […] #默認為all,顯示全部RPM包,也支持glob通配符
yum list {available|installed|updates} [glob_exp1][…]
[root@centos7 ~]#yum list|less Installed Packages #已安裝軟件 GConf2.x86_64 3.2.6-8.el7 @anaconda #隨安裝向導安裝的 apr.x86_64 1.4.8-3.el7 @Centos7 #通過Centos7yum倉庫安裝 .....中間省略....... Available Packages #還可以安裝的軟件 0ad.x86_64 0.0.21-1.el7 NewEpel #位置 389-ds-base.x86_64 1.3.5.10-11.el7 NewBase #位置 .....(底下省略).....
yum list updates 目前服務器上可供本機進行升級的軟件有哪些,與yum check-update 類似
-.yum install package1 [package2] […] (包名)
注意:1.如果一個包在不同倉庫中有多個版本,默認會安裝最新版本
2.如果要安裝制定版本:install PACKAGE-VERSION…
-.yum reinstall package1 [package2] […] (重新安裝)
3.升級程序包:
yum update [package1] [package2] […]
yum downgrade package1 [package2] […] (降級)
4.卸載程序包:
yum remove | erase package1 [package2] […]
此命令默認是不會卸載所依賴的包,但是依賴于正卸載的程序包的程序包會被一并卸載
5.查看程序包information:
yum info […]
6.查看指定的特性(可以是某文件)是由哪個程序包所提供,與RPM -qf 類似
yum provides | whatprovides feature1 [feature2][…]
[root@centos7 ~]#yum provides /usr/bin/bash Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * NewBase: bash-4.2.46-20.el7_2.x86_64 : The GNU Bourne Again shell Repo : NewBase Matched from: Filename : /usr/bin/bash #bash的可執行程序文件是由bash-4.2.46-20.el7_2.x86_64這個RPM包提供的
7.緩存管理
-.清理本地緩存:
在yum運作流程中我們提到了客戶端存有緩存列表,這些文件默認是存在/var/cache/yum/目錄下
[root@centos7 ~]#ls /var/cache/yum/x86_64/7/ #本機yum的緩存位置 base Centos7 epel7 extras NewBase NewEpel timedhosts timedhosts.txt updates
yum clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]
[root@centos7 ~]#yum clean all Loaded plugins: fastestmirror, langpacks Cleaning repos: NewBase NewEpel Cleaning up everything Cleaning up list of fastest mirrors
-.構建緩存:
yum makecache #自動連接至每一個可用倉庫,下載數據,創建為緩存
[root@centos7 ~]#yum makecache Loaded plugins: fastestmirror, langpacks NewBase | 3.6 kB 00:00:00 NewEpel | 4.3 kB 00:00:00 (1/4): NewBase/other_db | 2.4 MB 00:00:00 (2/4): NewBase/filelists_db | 6.6 MB 00:00:00 (3/4): NewEpel/other_db | 2.2 MB 00:00:02 (4/4): NewEpel/filelists_db | 8.0 MB 00:00:23 Loading mirror speeds from cached hostfile * NewBase: Metadata Cache Created
8.搜索:yum search string1 [string2] […]
搜索與以指定的關鍵字程序包名有關的軟件列表
9.查看yum事務歷史:
yum history [info|list|packages- list|packages- info|summary|addon- info|redo|undo|rollback|new|sync|stats]
#yum history列出yum的詳細操作歷史 [root@centos7 ~]#yum history Loaded plugins: fastestmirror, langpacks ID | Login user | Date and time | Action(s) | Altered ------------------------------------------------------------------------------- 6 | root <root> | 2017-06-11 19:39 | Install | 1 P< 5 | root <root> | 2017-06-11 18:10 | Install | 1 >< 4 | root <root> | 2017-06-11 17:40 | Install | 1 >< 3 | root <root> | 2017-06-10 09:28 | Install | 53 >< 2 | System <unset> | 2017-05-19 14:53 | Install | 1 > 1 | System <unset> | 2017-05-17 12:55 | Install | 1319 history list
#yum history info 6 查看第6條歷史的詳細操作,從Conmand Lind可以看出是安裝了tree軟件包 [root@centos7 ~]#yum history info 6 Loaded plugins: fastestmirror, langpacks Transaction ID : 6 Begin time : Sun Jun 11 19:39:02 2017 Begin rpmdb : 1376:18487d64bcef03c85f0f14dca43701acce15328a End time : (0 seconds) End rpmdb : 1377:8e94811fa592da9c619827c4eb3a265af705bb45 User : root <root> Return-Code : Success Command Line : install tree Transaction performed with: Installed rpm-4.11.3-21.el7.x86_64 @anaconda Installed yum-3.4.3-150.el7.centos.noarch @anaconda Installed yum-plugin-fastestmirror-1.1.31-40.el7.noarch @anaconda Packages Altered: Install tree-1.6.0-10.el7.x86_64 @NewBase Rpmdb Problems: conflicts: ipa-client-4.4.0-12.el7.centos.x86_64 has installed conflicts freeipa-client: ipa-client-4.4.0-12.el7.centos.x86_64 Installed ipa-client-4.4.0-12.el7.centos.x86_64 @anaconda conflicts: ipa-client-common-4.4.0-12.el7.centos.noarch has installed conflicts freeipa-client-common: : ipa-client-common-4.4.0-12.el7.centos.noarch Installed ipa-client-common-4.4.0-12.el7.centos.noarch @anaconda conflicts: ipa-common-4.4.0-12.el7.centos.noarch has installed conflicts freeipa-common: ipa-common-4.4.0-12.el7.centos.noarch Installed ipa-common-4.4.0-12.el7.centos.noarch @anaconda history info
#yum history undo 6 撤消第6條的操作,即卸載tree包 [root@centos7 ~]#yum history undo 6 Loaded plugins: fastestmirror, langpacks Undoing transaction 6, from Sun Jun 11 19:39:02 2017 Install tree-1.6.0-10.el7.x86_64 @NewBase Resolving Dependencies --> Running transaction check ---> Package tree.x86_64 0:1.6.0-10.el7 will be erased --> Finished Dependency Resolution Dependencies Resolved ==================================================================================================================================== Package Arch Version Repository Size ==================================================================================================================================== Removing: tree x86_64 1.6.0-10.el7 @NewBase 87 k Transaction Summary ==================================================================================================================================== Remove 1 Package Installed size: 87 k Is this ok [y/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Erasing : tree-1.6.0-10.el7.x86_64 1/1 Verifying : tree-1.6.0-10.el7.x86_64 1/1 Removed: tree.x86_64 0:1.6.0-10.el7 Complete!
這里要注意,在第四個卸載命令里提到了,remove命令是不會把其依賴的包卸載,而通過undo 命令是可以全部卸載的
10.包組管理的相關命令:
-.yum grouplist
列出所有可使用的軟件群組,主要分為以下四類:
Installed environment groups: 已經安裝的系統環境包組
Available environment groups: 還可以安裝的系統環境包組
Installed groups: 已經安裝的包組
Available Groups: 還能額外安裝的包組
-.yum groupinstall group1 [group2] […]
安裝包組
-.yum groupupdate group1 [group2] […]
升級包組
-.yum groupremove group1 [group2] […]
卸載包組
-.yum groupinfo group1 […]
顯示指定包組信息
11.yum的命令行選項:
-y: 自動回答為“ yes”
-q:靜默模式
— nogpgcheck:禁止進行gpg check
— disablerepo=repoidglob:臨時禁用此處指定的repo
— enablerepo=repoidglob:臨時啟用此處指定的repo
— noplugins:禁用所有插件
yum命令的用法就簡單介紹這么多,同時希望本文能讓你對yum的使用有所幫助。關于如何把我們的本地RPM包像yum sever一樣通過http或ftp網絡方式分享出去,請參考另外一篇博客http://www.www58058.com/78133
原創文章,作者:ffu,如若轉載,請注明出處:http://www.www58058.com/77910