Contents

Linux - YUM 安裝及更新套件

RPM 做為套件軟體工具的管理,YUM 為更上一階工具,能解決相依性問題並請能指定套件倉庫地址做自動更新。

Note
YUM 本身並無取代 RPM ,另外 YUM 經過優化改良後以改為 DNF ,不過還是可以使用 YUM 指令(透過軟連結指向 DNF),詳細 RedHat YUM 訊息, 官方文件請點我.

YUM 使用方式

YUM 在 RHEL 8 之後改成 DNF ,不過對於習慣使用 YUM 指令的人不用擔心,此指令並沒有被棄用,還是可以用,只是實際運作改為 DNF


1. YUM 搜尋套件

1
yum search [keyword]

此搜尋套件並非查詢機器已經安裝過的套件,而是查看所有列在此機器的倉庫清單有哪些符合關鍵字的套件。

Note
YUM 倉庫清單存放於 /etc/yum.repo.d/ 內,並以 .repo 結尾
1
2
3
4
5
6
[root@serverb ~]# yum search zsh
Last metadata expiration check: 2:34:12 ago on Wed 02 Mar 2022 07:12:59 PM CST.
===================================================== Name Exactly Matched: zsh =====================================================
zsh.x86_64 : Powerful interactive shell
==================================================== Name & Summary Matched: zsh ====================================================
zsh-html.noarch : Zsh shell manual in html format

2. YUM 套件清單

1
yum list [option]

查看所有倉庫清單,另可以添加 –installed 選擇已經安裝過的套件並做篩選

1
2
3
[root@serverb ~]# yum list --installed | grep firewalld
firewalld.noarch                     0.9.3-7.el8                             @anaconda  
firewalld-filesystem.noarch          0.9.3-7.el8                             @anaconda 

3. YUM 套件詳細訊息

1
yum info [package]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
[root@serverb ~]# yum info firewalld
Last metadata expiration check: 2:39:42 ago on Wed 02 Mar 2022 07:12:59 PM CST.
Installed Packages
Name         : firewalld
Version      : 0.9.3
Release      : 7.el8
Architecture : noarch
Size         : 2.0 M
Source       : firewalld-0.9.3-7.el8.src.rpm
Repository   : @System
From repo    : anaconda
Summary      : A firewall daemon with D-Bus interface providing a dynamic firewall
URL          : http://www.firewalld.org
License      : GPLv2+
Description  : firewalld is a firewall service daemon that provides a dynamic customizable
             : firewall with a D-Bus interface.

4. YUM 查看路徑檔案套件為何

1
yum provides [path]

與 rpm -qf 功能一樣,可以反向查詢

1
2
3
4
5
6
[root@serverb ~]# yum provides /var/www/html
Last metadata expiration check: 2:50:01 ago on Wed 02 Mar 2022 07:12:59 PM CST.
httpd-filesystem-2.4.37-41.module+el8.5.0+695+1fa8055e.noarch : The basic directory layout for the Apache HTTP server
Repo        : RH294_App
Matched from:
Filename    : /var/www/html

5. YUM 安裝套件

1
yum install [package]

yum 在安裝套件時都會提示,此次安裝有多少個相依性套件,大小為多少,是否要同意繼續,如已經熟悉並瞭解此套件,可以於每次安裝套件時加上 -y 參數,就不會詢問直接進行安裝

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[root@serverb ~]# yum install php
Last metadata expiration check: 2:54:58 ago on Wed 02 Mar 2022 07:12:59 PM CST.
Dependencies resolved.
=====================================================================================================================================
 Package                       Architecture       Version                                               Repository              Size
=====================================================================================================================================
Installing:
 php                           x86_64             7.2.24-1.module+el8.4.0+413+c9202dda                  RH294_App              1.5 M
Installing dependencies:
 apr                           x86_64             1.6.3-12.el8                                          RH294_App              128 k
 apr-util                      x86_64             1.6.1-6.el8.1                                         RH294_App              104 k
 httpd                         x86_64             2.4.37-43.module+el8.5.0+727+743c5577.1               RH294_App              1.4 M
 httpd-filesystem              noarch             2.4.37-43.module+el8.5.0+727+743c5577.1               RH294_App               39 k
 httpd-tools                   x86_64             2.4.37-43.module+el8.5.0+727+743c5577.1               RH294_App              106 k
...

6. YUM 套件更新

1
yum update

更新套件會檢查本地所安裝的套件與倉庫清單內所有套件是否有新版本,如有新版本將會移除舊版本安裝新的;惟核心在更新後並不會移除舊的核心,更新完並會在開機選項可以查看新安裝的核心版本,以供使用者選擇

Note

如果只要單獨更新核心可以使用

1
yum update kernel
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
[root@serverb ~]# yum update
Last metadata expiration check: 3:12:17 ago on Wed 02 Mar 2022 07:12:59 PM CST.
Dependencies resolved.
=====================================================================================================================================
 Package                                  Architecture         Version                                Repository                Size
=====================================================================================================================================
Installing:
 kernel                                   x86_64               4.18.0-348.12.2.el8_5                  RH294_Base               7.0 M
Upgrading:
 cryptsetup-libs                          x86_64               2.3.3-4.el8_5.1                        RH294_Base               473 k
 cyrus-sasl-lib                           x86_64               2.1.27-6.el8_5                         RH294_Base               122 k
 dnf-plugins-core                         noarch               4.0.21-4.el8_5                         RH294_Base                69 k
...

7. YUM 移除套件

1
yum remove [package]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[root@serverb ~]# yum remove zsh
Dependencies resolved.
=====================================================================================================================================
 Package                  Architecture                Version                                 Repository                        Size
=====================================================================================================================================
Removing:
 zsh                      x86_64                      5.5.1-6.el8_1.2                         @RH294_Base                      6.9 M

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

8. YUM Group 套件列表

1
yum group list

YUM 套件安裝固然方便,不過如果想要的功能需要很多套件組合而成呢?一個一個安裝不是好方式,故就有群組概念,將常使用的功能加以整理,把所有相關套件(含依賴性套件)一口氣全部安裝方式。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@serverb ~]# yum group list
Last metadata expiration check: 3:21:45 ago on Wed 02 Mar 2022 07:12:59 PM CST.
Available Environment Groups:
   Server with GUI
   Server
   Workstation
   Virtualization Host
   Custom Operating System
Installed Environment Groups:
   Minimal Install
Available Groups:
   Container Management
   .NET Core Development
   RPM Development Tools
   Development Tools
   Graphical Administration Tools
   Headless Management
   Legacy UNIX Compatibility
   Network Servers
   Scientific Support
   Security Tools
   Smart Card Support
   System Tools
Note

其安裝方式與一般雷同,例如

1
yum group install "Server with GUI"

9. YUM Module Streams

1
yum module list

這個方式為 RHEL 8 系列之後的新方式,主要在於傳統的倉庫清單被拆成 BaseOS 及 AppStream 兩個倉庫,此作法可以讓快速迭代的應用程式可以選擇版本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[root@serverb ~]# yum module list  postgresql
Last metadata expiration check: 3:46:46 ago on Wed 02 Mar 2022 07:08:59 PM CST.
rh294 AppStream
Name                     Stream                  Profiles                            Summary                                         
postgresql               9.6                     client, server [d]                  PostgreSQL server and client module             
postgresql               10 [d][e]               client, server [d] [i]              PostgreSQL server and client module             
postgresql               12                      client, server [d]                  PostgreSQL server and client module             
postgresql               13                      client, server [d]                  PostgreSQL server and client module             

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

d : 表示預設會進行安裝版本
i : 表示已經安裝的版本
e : 後續移除重新安裝都會使用該版本
x : 此本版不進行安裝

如上述,目前已經安裝 10 版本的 postgresql ,如因業務需要要改用 9.6 版本的可以執行如下步驟

1
2
3
4
5
6
7
8
# 先移除套件
yum module remove postgresql

# 取消 10 版本限制
yum module reset postgresql

# 改安裝 9.6 版本
yum module install postgresql:9.6

上述步驟執行完後再次查看 postgresql 套件模組

1
2
3
4
5
6
7
8
yum module list  postgresql
Last metadata expiration check: 3:45:01 ago on Wed 02 Mar 2022 07:12:59 PM CST.
rh294 AppStream
Name                     Stream                Profiles                             Summary                                          
postgresql               9.6 [e]               client, server [d] [i]               PostgreSQL server and client module              
postgresql               10 [d]                client, server [d]                   PostgreSQL server and client module              
postgresql               12                    client, server [d]                   PostgreSQL server and client module              
postgresql               13                    client, server [d]                   PostgreSQL server and client module  

10. YUM history 套件歷史紀錄

1
yum history [options]

查看安裝或移除記錄,並且可以使用 undo 指定 ID 回上一步驟

1
2
3
4
5
6
7
[root@serverb ~]# yum history 
ID     | Command line                                                                   | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------------------------------------------------------------
    17 | install httpd php                                                              | 2022-03-02 23:08 | Install        |   15   
    16 | remove zsh                                                                     | 2022-03-02 23:08 | Removed        |    1   
    15 | module remove postgresql                                                       | 2022-03-02 23:08 | Removed        |    1   
    14 | module install postgresql                                                      | 2022-03-02 22:59 | I, U           |    2 EE
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
[root@serverb ~]# yum history info 17
Transaction ID : 17
Begin time     : Wed 02 Mar 2022 11:08:50 PM CST
Begin rpmdb    : 411:91b538447070dcfdd1846b0dcd7de68609cd9b5a
End time       : Wed 02 Mar 2022 11:08:53 PM CST (3 seconds)
End rpmdb      : 426:b58bc73689caa304fb3a31c72e92d9f2dde3f862
User           : root <root>
Return-Code    : Success
Releasever     : 8
Command Line   : install httpd php
Comment        : 
Packages Altered:
    Install apr-1.6.3-12.el8.x86_64                                         @RH294_App
    Install apr-util-1.6.1-6.el8.1.x86_64                                   @RH294_App
    Install apr-util-bdb-1.6.1-6.el8.1.x86_64                               @RH294_App
    Install apr-util-openssl-1.6.1-6.el8.1.x86_64                           @RH294_App
    Install httpd-2.4.37-43.module+el8.5.0+727+743c5577.1.x86_64            @RH294_App
...

YUM 小結

YUM 真的是一的十分棒的管理工具,不過還是要記得一件事情,RHEL 8 以後其實是改成 DNF 了喔,如下顯示

1
2
[root@serverb ~]# ls -l $(which yum)
lrwxrwxrwx. 1 root root 5 Nov  9 22:43 /usr/bin/yum -> dnf-3


如果你還沒有註冊 Like Coin,你可以在文章最下方看到 Like 的按鈕,點下去後即可申請帳號,透過申請帳號後可以幫我的文章按下 Like,而 Like 最多可以點五次,而你不用付出任何一塊錢,就能給我寫這篇文章的最大的回饋!