1. 程式人生 > >Linux 包管理工具之Yum

Linux 包管理工具之Yum

linux yum

Yum(全稱為 Yellow dog Updater, Modified)是基於RPM包的包管理工具,能夠從指定的服務器自動下載RPM包並且安裝,可以自動處理依賴性關系,並且一次安裝所有依賴的軟件包,無須繁瑣地一次次下載、安裝。至於RPM的介紹,請參考 http://blog.51cto.com/marvin89/2107904

yum簡單流程圖:
技術分享圖片

yum配置文件查看

[root@localhost Packages]# rpm -q yum
yum-3.4.3-154.el7.centos.noarch
[root@localhost Packages]# rpm -qc yum
/etc/logrotate.d/yum
/etc/yum.conf
/etc/yum/version-groups.conf

配置文件分2端

[root@localhost Packages]# man yum.conf

#主配置
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0    #
debuglevel=2
logfile=/var/log/yum.log
exactarch=1    #centos6  centos7 之類是否精確匹配
obsoletes=1
gpgcheck=1    #來源合法性
plugins=1    #支持插件機制
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release   

#倉庫配置
[repositoryID]
name=Some name for this repository  #介紹
baseurl=url://path/to/repository/      #必須跟repodata同一個目錄
enabled={1|0}
gpgcheck={1|0}
gpgkey=URL  #公鑰 
enablegroups={1|0} #用組的方式管理程序包
failovermethod={roundrobin|priority}
    默認為:roundrobin,意為隨機挑選; baseurl
cost=默認為1000  權重越高優先使用

配置以centos光盤為倉庫的yum

準備yum源 cd盤?

[root@localhost rpm]# cd /mnt/dvd/
[root@localhost dvd]# ls
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

配置倉庫

[root@localhost yum.repos.d]# vim /etc/yum.repos.d/local.repo
[base]
name=Base repo from cd
baseurl=file:///mnt/dvd/
gpgcheck=1
gpgkey=file:///mnt/dvd/RPM-GPG-KEY-CentOS-7

1、顯示倉庫列表:

 #repolist [all|enabled|disabled]
[root@localhost yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                               repo name                                        status
!base                                 Base repo from cd                                9,591
repolist: 9,591

2、安裝

 [root@localhost yum.repos.d]# yum install zsh-html   [#-y]    默認安裝最新版本,安裝老版本需要手動指定

3、升級、檢查升級 (也可以是本地程序包)

#升級
[root@localhost yum.repos.d]# yum update  zsh-html
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
No packages marked for update
-----------------------------------------------------------
#檢查升級
[root@localhost yum.repos.d]# yum check-update zsh-html
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile

4、卸載 只會卸載當前包和依賴當前包的包
不會刪除安裝時候依賴ImageMagick的包 ?因為這些包依賴ImageMagick,而不是被依賴

[root@localhost yum.repos.d]# yum install ImageMagick
Dependencies Resolved

=============================================================================================
 Package                        Arch          Version                      Repository   Size
=============================================================================================
Installing:
 ImageMagick                    x86_64        6.7.8.9-15.el7_2             base        2.1 M
Installing for dependencies:
 OpenEXR-libs                   x86_64        1.7.1-7.el7                  base        217 k
 avahi-libs                     x86_64        0.6.31-17.el7                base         61 k
 cairo                          x86_64        1.14.8-2.el7                 base        713 k
 cups-libs                      x86_64        1:1.6.3-29.el7               base        356 k
 fontconfig                     x86_64        2.10.95-11.el7               base        229 k
 fontpackages-filesystem        noarch        1.44-8.el7                   base        9.9 k
 gdk-pixbuf2                    x86_64        2.36.5-1.el7                 base        567 k
 ghostscript                    x86_64        9.07-28.el7                  base        4.3 M
 ghostscript-fonts              noarch        5.50-32.el7                  base        324 k
 graphite2                      x86_64        1.3.6-1.el7_2                base        112 k
 harfbuzz                       x86_64        1.3.2-1.el7                  base        177 k
 ilmbase                        x86_64        1.0.3-7.el7                  base        100 k
 jasper-libs                    x86_64        1.900.1-31.el7               base        150 k
.....

[root@localhost yum.repos.d]# yum remove ImageMagick
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package ImageMagick.x86_64 0:6.7.8.9-15.el7_2 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================
 Package                Arch              Version                     Repository        Size
=============================================================================================
Removing:
 ImageMagick            x86_64            6.7.8.9-15.el7_2            @base            7.6 M

Transaction Summary
=============================================================================================
Remove  1 Package

卸載ImageMagick的依賴包OpenEXR-libs,會把ImageMagick也卸載

[root@localhost yum.repos.d]# yum remove  OpenEXR-libs
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package OpenEXR-libs.x86_64 0:1.7.1-7.el7 will be erased
--> Processing Dependency: libIlmImf.so.7()(64bit) for package: ImageMagick-6.7.8.9-15.el7_2.x86_64
--> Running transaction check
---> Package ImageMagick.x86_64 0:6.7.8.9-15.el7_2 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================
 Package                Arch             Version                       Repository       Size
=============================================================================================
Removing:
 OpenEXR-libs           x86_64           1.7.1-7.el7                   @base           844 k
Removing for dependencies:
 ImageMagick            x86_64           6.7.8.9-15.el7_2              @base           7.6 M

個人覺得這個卸載方式不是很好,會殘留很多無用的包

?
5、顯示程序包:

# yum list [all | glob_exp1] [glob_exp2] [...]
# yum list {available|installed|updates} [glob_exp1] [...]

[root@localhost yum.repos.d]# yum list
Installed Packages
GeoIP.x86_64                                1.5.0-11.el7                           @anaconda #@表示安裝過的。anaconda表示操作系統

....
zziplib-devel.i686                              0.13.62-5.el7                       base   #還未安裝的
zziplib-devel.x86_64                            0.13.62-5.el7                       base
zsh-html.x86_64                             5.0.2-28.el7                          @base      #base源安裝的

6、具體包信息查看

[root@localhost yum.repos.d]# yum info ImageMagick
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
Name        : ImageMagick
Arch        : x86_64
Version     : 6.7.8.9
Release     : 15.el7_2
Size        : 7.6 M
Repo        : installed
From repo   : base
Summary     : An X application for displaying and manipulating images
....

7、查看指定的特性(可以是某文件)是由哪個程序包所提供:相當於rpm -qf

# provides | whatprovides feature1 [feature2] [...]

[root@localhost yum.repos.d]# yum provides /etc/passwd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
setup-2.8.71-7.el7.noarch : A set of system configuration and setup files
Repo        : base
Matched from:
Filename    : /etc/passwd

setup-2.8.71-7.el7.noarch : A set of system configuration and setup files
Repo        : @anaconda
Matched from:
Filename    : /etc/passwd

8、查看 以指定的關鍵字搜索程序包名及summary信息;

[root@localhost yum.repos.d]# yum search zsh
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
===================================== N/S matched: zsh ======================================
zsh-html.x86_64 : Zsh shell manual in html format
zsh.x86_64 : Powerful interactive shell

  Name and summary matches only, use "search all" for everything.

9、依賴包查看

[root@localhost yum.repos.d]# yum deplist bash
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
package: bash.x86_64 4.2.46-28.el7
  dependency: libc.so.6(GLIBC_2.15)(64bit)
   provider: glibc.x86_64 2.17-196.el7
  dependency: libdl.so.2()(64bit)
......

10、查看yum事務歷史:

#history [info|list|packages-list|packages-info|summary|addon-info|redo|undo|rollback|new|sync|stats]
[root@localhost yum.repos.d]# yum history
Loaded plugins: fastestmirror
ID     | Login user               | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
    17 | root <root>              | 2018-04-25 21:53 | Install        |    1
    16 | root <root>              | 2018-04-25 21:49 | Erase          |    1
    15 | root <root>              | 2018-04-25 21:49 | Install        |   48

11、包組查看

[root@localhost yum.repos.d]# yum grouplist
Installed Groups:
   .....
Available Groups:
   Compatibility Libraries
   Console Internet Tools
   Graphical Administration Tools
   Development Tools
    .....

12、安裝包組

[root@localhost yum.repos.d]# yum groupinstall "Development Tools"
# 升級 groupupdate group1 [group2] [...]

13、移除包組

[root@localhost yum.repos.d]# yum groupremove  "Development Tools"

14、包組信息查看

[root@localhost yum.repos.d]# yum groupinfo  "Development Tools"

建立自己的yum源

1、添加幾個rpm包,為yum源

[root@localhost yum.repos.d]# mkdir -p /yum/repo
[root@localhost yum.repos.d]# cp /mnt/dvd/Packages/z* /yum/repo/

2、centos7需要安裝createrepo

[root@localhost repo]# yum install createrepo

3、初始化倉庫

[root@localhost repo]# pwd
/yum/repo

[root@localhost repo]# createrepo ../   #在上級目錄下創建repodata
Spawning worker 0 with 5 pkgs
Spawning worker 1 with 5 pkgs
Spawning worker 2 with 5 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

[root@localhost repo]# cd ..
[root@localhost yum]# ls repodata
447ec135b1c7f6b6270eefc3c48487b7111af058b6a8e69d2752dff8fe692f10-filelists.sqlite.bz2
5bf5f6ef05024c6fa8323255156d043213eac62f765d4106d71e57e133e16990-other.xml.gz
6c2ee7607b3b821acc6907604067e85d101fb4eecc6e10baa45086f26e0343f8-primary.sqlite.bz2
c881c0f911ca54a1ade1d96280a04b8224a5d782522d555f7c6d1b528c2ddd7f-other.sqlite.bz2
e018c8c524d20f6f30d26b42c909757eb2966a55c2f1a90d543602848db03384-filelists.xml.gz
ed266c67e84620208b4124883f03e6a8eaf0ba1d98a0dddd85c5a57beb8efbc4-primary.xml.gz
repomd.xml

4、編輯配置文件

[myrepo]
name=test repo
baseurl=file:///yum
gpgcheck=0

5、使用自己的yum源

[root@localhost repodata]# yum remove zsh
[root@localhost repodata]# yum install zsh
Loaded plugins: fastestmirror
myrepo                                                  | 2.9 kB     00:00
myrepo/primary_db                                         | 9.2 kB   00:00
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package zsh.x86_64 0:5.0.2-28.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================
 Package       Arch             Version                 Repository        Size
===============================================================================
Installing:
 zsh           x86_64           5.0.2-28.el7            myrepo           2.4 M

Transaction Summary
===============================================================================
Install  1 Package

Total download size: 2.4 M
Installed size: 5.6 M
Is this ok [y/d/N]:

Linux 包管理工具之Yum