跳至内容

软件管理

概述

在 Linux 系统上,可以通过两种方式安装软件

  • 使用安装包;
  • 从源代码文件编译。

注意

这里不涵盖从源代码安装。通常,您应该使用包方法,除非您想要的软件无法通过包管理器获得。原因是依赖关系通常由包系统管理,而对于源代码,您需要手动管理依赖关系。

:这是一个包含安装程序所需所有数据的单个文件。它可以直接在系统中的软件仓库中执行。

源代码文件:一些软件不是以现成的包形式提供的,而是通过包含源代码文件的存档提供的。由管理员负责准备这些文件并编译它们来安装程序。

RPM:RedHat 包管理器

RPM (RedHat Package Manager) 是一个软件管理系统。可以安装、卸载、更新或检查包中包含的软件。

RPM 是所有基于 RedHat 的发行版(RockyLinux、Fedora、CentOS、SuSe、Mandriva 等)使用的格式。在 Debian 世界中,它相当于 DPKG (Debian Package)。

RPM 包的名称遵循特定的命名规则

Illustration of a package name

rpm 命令

rpm 命令允许您安装一个包。

rpm [-i][-U] package.rpm [-e] package

示例(对于名为 'package' 的包)

rpm -ivh package.rpm
选项 描述
-i package.rpm 安装包。
-U package.rpm 更新已安装的包。
-e package.rpm 卸载包。
-h 显示进度条。
-v 通知操作的进度。
--test 测试命令而不执行它。

rpm 命令还允许您通过添加 -q 选项来查询系统包数据库。

可以执行多种类型的查询以获取有关已安装包的不同信息。RPM 数据库位于 /var/lib/rpm 目录中。

示例

rpm -qa

此命令查询系统中安装的所有包。

rpm -q [-a][-i][-l] package [-f] file

示例

rpm -qil package
rpm -qf /path/to/file
选项 描述
-a 列出系统中安装的所有包。
-i __package__ 显示包信息。
-l __package__ 列出包中包含的文件。
-f 显示包含指定文件的包的名称。
--last 包列表按安装日期给出(最近安装的包先出现)。

警告

-q 选项之后,包名称必须精确。不支持元字符(通配符)。

技巧

但是,可以通过列出所有已安装的包并使用 grep 命令进行过滤。

示例:列出最近安装的包

sudo rpm -qa --last | head
NetworkManager-config-server-1.26.0-13.el8.noarch Mon 24 May 2021 02:34:00 PM CEST
iwl2030-firmware-18.168.6.1-101.el8.1.noarch  Mon 24 May 2021 02:34:00 PM CEST
iwl2000-firmware-18.168.6.1-101.el8.1.noarch  Mon 24 May 2021 02:34:00 PM CEST
iwl135-firmware-18.168.6.1-101.el8.1.noarch   Mon 24 May 2021 02:34:00 PM CEST
iwl105-firmware-18.168.6.1-101.el8.1.noarch   Mon 24 May 2021 02:34:00 PM CEST
iwl100-firmware-39.31.5.1-101.el8.1.noarch    Mon 24 May 2021 02:34:00 PM CEST
iwl1000-firmware-39.31.5.1-101.el8.1.noarch   Mon 24 May 2021 02:34:00 PM CEST
alsa-sof-firmware-1.5-2.el8.noarch            Mon 24 May 2021 02:34:00 PM CEST
iwl7260-firmware-25.30.13.0-101.el8.1.noarch  Mon 24 May 2021 02:33:59 PM CEST
iwl6050-firmware-41.28.5.1-101.el8.1.noarch   Mon 24 May 2021 02:33:59 PM CEST

示例:列出内核的安装历史记录

sudo rpm -qa --last kernel
kernel-4.18.0-305.el8.x86_64                  Tue 25 May 2021 06:04:56 AM CEST
kernel-4.18.0-240.22.1.el8.x86_64             Mon 24 May 2021 02:33:35 PM CEST

示例:使用 grep 列出所有具有特定名称的已安装包

sudo dnf list installed | grep httpd
centos-logos-httpd.noarch           80.5-2.el8                              @baseos
httpd.x86_64                        2.4.37-30.module_el8.3.0+561+97fdbbcc   @appstream
httpd-filesystem.noarch             2.4.37-30.module_el8.3.0+561+97fdbbcc   @appstream
httpd-tools.x86_64                  2.4.37-30.module_el8.3.0+561+97fdbbcc   @appstream

DNF:Dandified Yum

DNF (Dandified Yum) 是一个软件包管理器,是 YUM (Yellow dog Updater Modified) 的后继者。它使用存储在本地或远程仓库(用于存储包的目录)中的 RPM 包。对于大多数常用命令,其用法与 yum 相同。

dnf 命令通过比较系统中已安装的包与服务器上定义的仓库中的包来管理包。它还会自动安装依赖项,前提是它们也存在于仓库中。

dnf 是许多基于 RedHat 的发行版(RockyLinux、Fedora、CentOS 等)使用的管理器。在 Debian 世界中,它相当于 APT (Advanced Packaging Tool)。

dnf 命令

dnf 命令允许您通过仅指定简短名称来安装包。

dnf [install][remove][list all][search][info] package

示例

dnf install tree

仅需要包的简短名称。

选项 描述
install 安装包。
删除 卸载包。
list all 列出仓库中已有的包。
search 在仓库中搜索包。
provides */command_name 搜索命令。
信息 显示包信息。
autoremove 删除所有作为依赖项安装但不再需要的包。

dnf install 命令允许您安装所需的包,而无需担心其依赖项,这些依赖项将直接由 dnf 本身解析。

dnf install nginx
Last metadata expiration check: 3:13:41 ago on Wed 23 Mar 2022 07:19:24 AM CET.
Dependencies resolved.
============================================================================================================================
 Package                             Architecture    Version                                        Repository         Size
============================================================================================================================
Installing:
 nginx                               aarch64         1:1.14.1-9.module+el8.4.0+542+81547229         appstream         543 k
Installing dependencies:
 nginx-all-modules                   noarch          1:1.14.1-9.module+el8.4.0+542+81547229         appstream          22 k
 nginx-mod-http-image-filter         aarch64         1:1.14.1-9.module+el8.4.0+542+81547229         appstream          33 k
 nginx-mod-http-perl                 aarch64         1:1.14.1-9.module+el8.4.0+542+81547229         appstream          44 k
 nginx-mod-http-xslt-filter          aarch64         1:1.14.1-9.module+el8.4.0+542+81547229         appstream          32 k
 nginx-mod-mail                      aarch64         1:1.14.1-9.module+el8.4.0+542+81547229         appstream          60 k
 nginx-mod-stream                    aarch64         1:1.14.1-9.module+el8.4.0+542+81547229         appstream          82 k

Transaction Summary
============================================================================================================================
Install  7 Packages

Total download size: 816 k
Installed size: 2.2 M
Is this ok [y/N]:

如果您不记得包的确切名称,可以使用 dnf search name 命令进行搜索。如您所见,有一个部分包含确切的名称,另一个部分包含包的对应项,所有这些都突出显示以便于搜索。

dnf search nginx
Last metadata expiration check: 0:20:55 ago on Wed 23 Mar 2022 10:40:43 AM CET.
=============================================== Name Exactly Matched: nginx ================================================
nginx.aarch64 : A high performance web server and reverse proxy server
============================================== Name & Summary Matched: nginx ===============================================
collectd-nginx.aarch64 : Nginx plugin for collectd
munin-nginx.noarch : NGINX support for Munin resource monitoring
nginx-all-modules.noarch : A meta package that installs all available Nginx modules
nginx-filesystem.noarch : The basic directory layout for the Nginx server
nginx-mod-http-image-filter.aarch64 : Nginx HTTP image filter module
nginx-mod-http-perl.aarch64 : Nginx HTTP perl module
nginx-mod-http-xslt-filter.aarch64 : Nginx XSLT module
nginx-mod-mail.aarch64 : Nginx mail modules
nginx-mod-stream.aarch64 : Nginx stream modules
pagure-web-nginx.noarch : Nginx configuration for Pagure
pcp-pmda-nginx.aarch64 : Performance Co-Pilot (PCP) metrics for the Nginx Webserver
python3-certbot-nginx.noarch : The nginx plugin for certbot

通过在附加搜索键中输入来搜索包的另一种方法是将 dnf 命令的结果通过管道传输到带有所需键的 grep 命令。

dnf search nginx | grep mod
Last metadata expiration check: 3:44:49 ago on Wed 23 Mar 2022 06:16:47 PM CET.
nginx-all-modules.noarch : A meta package that installs all available Nginx modules
nginx-mod-http-image-filter.aarch64 : Nginx HTTP image filter module
nginx-mod-http-perl.aarch64 : Nginx HTTP perl module
nginx-mod-http-xslt-filter.aarch64 : Nginx XSLT module
nginx-mod-mail.aarch64 : Nginx mail modules
nginx-mod-stream.aarch64 : Nginx stream modules

dnf remove 命令会从系统中移除一个包及其依赖项。下面是 dnf remove httpd 命令的摘录。

dnf remove httpd
Dependencies resolved.
============================================================================================================================
 Package                        Architecture    Version                                            Repository          Size
============================================================================================================================
Removing:
 httpd                          aarch64         2.4.37-43.module+el8.5.0+727+743c5577.1            @appstream         8.9 M
Removing dependent packages:
 mod_ssl                        aarch64         1:2.4.37-43.module+el8.5.0+727+743c5577.1          @appstream         274 k
 php                            aarch64         7.4.19-1.module+el8.5.0+696+61e7c9ba               @appstream         4.4 M
 python3-certbot-apache         noarch          1.22.0-1.el8                                       @epel              539 k
Removing unused dependencies:
 apr                            aarch64         1.6.3-12.el8                                       @appstream         299 k
 apr-util                       aarch64         1.6.1-6.el8.1                                      @appstream         224 k
 apr-util-bdb                   aarch64         1.6.1-6.el8.1                                      @appstream          67 k
 apr-util-openssl               aarch64         1.6.1-6.el8.1                                      @appstream          68 k
 augeas-libs                    aarch64         1.12.0-6.el8                                       @baseos            1.4 M
 httpd-filesystem               noarch          2.4.37-43.module+el8.5.0+727+743c5577.1            @appstream         400
 httpd-tools                    aarch64         2.4.37-43.module+el8.5.0+727+743c5577.1
...

dnf list 命令列出系统中已安装并在仓库中存在的包。它接受多个参数

参数 描述
all 列出已安装的包,然后列出仓库中可用的包。
available 仅列出可供安装的包。
updates 列出可以升级的包。
obsoletes 列出被更高版本取代的包。
recent 列出最近添加到仓库的包。

正如您所料,dnf info 命令提供有关包的详细信息

dnf info firewalld
Last metadata expiration check: 15:47:27 ago on Tue 22 Mar 2022 05:49:42 PM CET.
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    : baseos
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.

Available Packages
Name         : firewalld
Version      : 0.9.3
Release      : 7.el8_5.1
Architecture : noarch
Size         : 501 k
Source       : firewalld-0.9.3-7.el8_5.1.src.rpm
Repository   : baseos
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.

有时您只知道要使用的可执行文件,但不知道它所在的包,在这种情况下,您可以使用 dnf provides */package_name 命令,它将在数据库中为您搜索所需的匹配项。

搜索 semanage 命令的示例

dnf provides */semanage
Last metadata expiration check: 1:12:29 ago on Wed 23 Mar 2022 10:40:43 AM CET.
libsemanage-devel-2.9-6.el8.aarch64 : Header files and libraries used to build policy manipulation tools
Repo        : powertools
Matched from:
Filename    : /usr/include/semanage

policycoreutils-python-utils-2.9-16.el8.noarch : SELinux policy core python utilities
Repo        : baseos
Matched from:
Filename    : /usr/sbin/semanage
Filename    : /usr/share/bash-completion/completions/semanage

dnf autoremove 命令不需要任何参数。Dnf 会自行查找要删除的候选包。

dnf autoremove
Last metadata expiration check: 0:24:40 ago on Wed 23 Mar 2022 06:16:47 PM CET.
Dependencies resolved.
Nothing to do.
Complete!

其他有用的 dnf 选项

选项 描述
repolist 列出系统中配置的仓库。
grouplist 列出可用的包集合。
clean 删除临时文件。

dnf repolist 命令列出系统中配置的仓库。默认情况下,它只列出启用的仓库,但可以使用以下参数

参数 描述
--all 列出所有仓库。
--enabled 默认
--disabled 仅列出禁用的仓库。

示例

dnf repolist
repo id                                                  repo name
appstream                                                Rocky Linux 8 - AppStream
baseos                                                   Rocky Linux 8 - BaseOS
epel                                                     Extra Packages for Enterprise Linux 8 - aarch64
epel-modular                                             Extra Packages for Enterprise Linux Modular 8 - aarch64
extras                                                   Rocky Linux 8 - Extras
powertools                                               Rocky Linux 8 - PowerTools
rockyrpi                                                 Rocky Linux 8 - Rasperry Pi

以及带有 --all 标志的命令的摘录。

dnf repolist --all

...
repo id                                             repo name                                                                                       status
appstream                                           Rocky Linux 8 - AppStream                                                                       enabled
appstream-debug                                     Rocky Linux 8 - AppStream - Source                                                              disabled
appstream-source                                    Rocky Linux 8 - AppStream - Source                                                              disabled
baseos                                              Rocky Linux 8 - BaseOS                                                                          enabled
baseos-debug                                        Rocky Linux 8 - BaseOS - Source                                                                 disabled
baseos-source                                       Rocky Linux 8 - BaseOS - Source                                                                 disabled
devel                                               Rocky Linux 8 - Devel WARNING! FOR BUILDROOT AND KOJI USE                                       disabled
epel                                                Extra Packages for Enterprise Linux 8 - aarch64                                                 enabled
epel-debuginfo                                      Extra Packages for Enterprise Linux 8 - aarch64 - Debug                                         disabled
epel-modular                                        Extra Packages for Enterprise Linux Modular 8 - aarch64                                         enabled
epel-modular-debuginfo                              Extra Packages for Enterprise Linux Modular 8 - aarch64 - Debug                                 disabled
epel-modular-source                                 Extra Packages for Enterprise Linux Modular 8 - aarch64 - Source
...

下面是已禁用仓库列表的摘录。

dnf repolist --disabled
repo id                                 repo name
appstream-debug                         Rocky Linux 8 - AppStream - Source
appstream-source                        Rocky Linux 8 - AppStream - Source
baseos-debug                            Rocky Linux 8 - BaseOS - Source
baseos-source                           Rocky Linux 8 - BaseOS - Source
devel                                   Rocky Linux 8 - Devel WARNING! FOR BUILDROOT AND KOJI USE
epel-debuginfo                          Extra Packages for Enterprise Linux 8 - aarch64 - Debug
epel-modular-debuginfo                  Extra Packages for Enterprise Linux Modular 8 - aarch64 - Debug
epel-modular-source                     Extra Packages for Enterprise Linux Modular 8 - aarch64 - Source
epel-source                             Extra Packages for Enterprise Linux 8 - aarch64 - Source
epel-testing                            Extra Packages for Enterprise Linux 8 - Testing - aarch64
...

使用 -v 选项可以增强列表,并提供大量附加信息。下面可以看到命令结果的一部分。

dnf repolist -v

...
Repo-id            : powertools
Repo-name          : Rocky Linux 8 - PowerTools
Repo-revision      : 8.5
Repo-distro-tags      : [cpe:/o:rocky:rocky:8]:  ,  , 8, L, R, c, i, k, n, o, u, x, y
Repo-updated       : Wed 16 Mar 2022 10:07:49 PM CET
Repo-pkgs          : 1,650
Repo-available-pkgs: 1,107
Repo-size          : 6.4 G
Repo-mirrors       : https://mirrors.rockylinux.org/mirrorlist?arch=aarch64&repo=PowerTools-8
Repo-baseurl       : https://example.com/pub/rocky/8.8/PowerTools/x86_64/os/ (30 more)
Repo-expire        : 172,800 second(s) (last: Tue 22 Mar 2022 05:49:24 PM CET)
Repo-filename      : /etc/yum.repos.d/Rocky-PowerTools.repo
...

使用组

组是包集合(您可以将其视为虚拟包),它们在逻辑上将一组应用程序分组以完成某个目的(桌面环境、服务器、开发工具等)。

dnf grouplist 命令列出所有可用的组。

dnf grouplist
Last metadata expiration check: 1:52:00 ago on Wed 23 Mar 2022 02:11:43 PM CET.
Available Environment Groups:
   Server with GUI
   Server
   Minimal Install
   KDE Plasma Workspaces
   Custom Operating System
Available Groups:
   Container Management
   .NET Core Development
   RPM Development Tools
   Development Tools
   Headless Management
   Legacy UNIX Compatibility
   Network Servers
   Scientific Support
   Security Tools
   Smart Card Support
   System Tools
   Fedora Packager
   Xfce

dnf groupinstall 命令允许您安装这些组之一。

dnf groupinstall "Network Servers"
Last metadata expiration check: 2:33:26 ago on Wed 23 Mar 2022 02:11:43 PM CET.
Dependencies resolved.
================================================================================
 Package           Architecture     Version             Repository         Size
================================================================================
Installing Groups:
 Network Servers

Transaction Summary
================================================================================

Is this ok [y/N]:

请注意,用双引号括起组名是一个好习惯,因为如果没有双引号,只有当组名不包含空格时,命令才会正确执行。

因此,dnf groupinstall Network Servers 会产生以下错误。

dnf groupinstall Network Servers
Last metadata expiration check: 3:05:45 ago on Wed 23 Mar 2022 02:11:43 PM CET.
Module or Group 'Network' is not available.
Module or Group 'Servers' is not available.
Error: Nothing to do.

移除组的相应命令是 dnf groupremove "group name"

dnf clean 命令清除 dnf 创建的所有缓存和临时文件。可以使用以下参数。

参数 描述
all 删除为已启用仓库创建的所有临时文件。
dbcache 删除仓库元数据的缓存文件。
expire-cache 删除本地 Cookie 文件。
metadata 删除所有仓库的元数据。
packages 删除任何缓存的包。

DNF 的工作原理

DNF 管理器依赖一个或多个配置文件来定位包含 RPM 包的仓库。

这些文件位于 /etc/yum.repos.d/ 并且必须以 .repo 结尾才能被 DNF 使用。

示例

/etc/yum.repos.d/Rocky-BaseOS.repo

每个 .repo 文件至少包含以下信息,每行一个指令。

示例

[baseos] # Short name of the repository
name=Rocky Linux $releasever - BaseOS # Short name of the repository #Detailed name
mirrorlist=http://mirrors.rockylinux.org/mirrorlist?arch=$basearch&repo=BaseOS-$releasever # http address of a list or mirror
#baseurl=http://dl.rockylinux.org/$contentdir/$releasever/BaseOS/$basearch/os/ # http address for direct access
gpgcheck=1 # Repository requiring a signature
enabled=1 # Activated =1, or not activated =0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial # GPG public key path

默认情况下,enabled 指令不存在,这意味着仓库已启用。要禁用一个仓库,必须指定 enabled=0 指令。

DNF 模块

模块是在 Rocky Linux 8 中由上游引入的。要使用模块,必须存在 AppStream 仓库并启用它。

包的混淆

AppStream 仓库中模块流的创建引起了很多人的困惑。由于模块被打包在流中(请参阅下面的示例),因此某个包会出现在我们的 RPM 中,但如果尝试在未启用模块的情况下安装它,则不会发生任何事情。请记住,如果您尝试安装一个包但找不到它,请查看模块。

什么是模块

模块来自 AppStream 仓库,包含流和配置文件。它们可以描述如下:

  • 模块流: 模块流可以被认为是 AppStream 仓库中的一个独立仓库,其中包含不同的应用程序版本。这些模块仓库包含该特定流的应用程序 RPM、依赖项和文档。Rocky Linux 8 中模块流的一个例子是 postgresql。如果您使用标准的 sudo dnf install postgresql 安装 postgresql,您将获得版本 10。但是,使用模块,您可以安装版本 9.6、12 或 13。

  • 模块配置文件: 模块配置文件在安装包时会考虑模块流的用例。应用配置文件会调整包 RPM、依赖项和文档以适应模块的使用。以我们相同的 postgresql 流为例,您可以应用“服务器”或“客户端”的配置文件。显然,如果您只是将 postgresql 用作访问服务器的客户端,您不需要在系统上安装相同的包。

列出模块

您可以通过执行以下命令获取所有模块的列表

dnf module list

这将为您提供可用模块及其配置文件的长列表。问题是,您可能已经知道您感兴趣的包,因此要找出是否有特定包的模块,请在“list”后面添加包名。我们将再次使用我们的 postgresql 包示例

dnf module list postgresql

这将为您提供类似以下的输出

Rocky Linux 8 - AppStream
Name                       Stream                 Profiles                           Summary                                            
postgresql                 9.6                    client, server [d]                 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

请注意列表中的“[d]”。这意味着它是默认的。它显示默认版本是 10,并且无论您选择哪个版本,如果您不指定配置文件,那么服务器配置文件将是使用的配置文件,因为这也是默认的。

启用模块

使用我们的 postgresql 包示例,假设我们要启用 12 版本。为此,您只需使用以下命令:

dnf module enable postgresql:12

此处,enable 命令需要模块名称,后跟“:”和流名称。

要验证您已启用 postgresql 模块流版本 12,请再次使用您的 list 命令,它应该显示以下输出:

Rocky Linux 8 - AppStream
Name                       Stream                 Profiles                           Summary                                            
postgresql                 9.6                    client, server [d]                 PostgreSQL server and client module                
postgresql                 10 [d]                 client, server [d]                 PostgreSQL server and client module                
postgresql                 12 [e]                 client, server [d]                 PostgreSQL server and client module                
postgresql                 13                     client, server [d]                 PostgreSQL server and client module

在这里,我们可以在流 12 旁边看到“[e]”表示“已启用”,所以我们知道版本 12 已启用。

从模块流安装软件包

现在我们的模块流已启用,下一步是安装 postgresql,它是 postgresql 服务器的客户端应用程序。这可以通过运行以下命令来完成:

dnf install postgresql

这应该会给您此输出:

========================================================================================================================================
 Package                    Architecture           Version                                              Repository                 Size
========================================================================================================================================
Installing group/module packages:
 postgresql                 x86_64                 12.12-1.module+el8.6.0+1049+f8fc4c36                 appstream                 1.5 M
Installing dependencies:
 libpq                      x86_64                 13.5-1.el8                                           appstream                 197 k

Transaction Summary
========================================================================================================================================
Install  2 Packages
Total download size: 1.7 M
Installed size: 6.1 M
Is this ok [y/N]:

在输入“y”确认后,您就安装了应用程序。

从模块流配置文件安装软件包

甚至可以直接安装包,而无需启用模块流!在此示例中,假设我们只想将客户端配置文件应用于我们的安装。为此,我们只需输入此命令:

dnf install postgresql:12/client

这应该会给您此输出:

========================================================================================================================================
 Package                    Architecture           Version                                              Repository                 Size
========================================================================================================================================
Installing group/module packages:
 postgresql                 x86_64                 12.12-1.module+el8.6.0+1049+f8fc4c36                 appstream                 1.5 M
Installing dependencies:
 libpq                      x86_64                 13.5-1.el8                                           appstream                 197 k
Installing module profiles:
 postgresql/client
Enabling module streams:
 postgresql                                        12

Transaction Summary
========================================================================================================================================
Install  2 Packages

Total download size: 1.7 M
Installed size: 6.1 M
Is this ok [y/N]:

在提示时回答“y”将安装使用 postgresql 版本 12 作为客户端所需的一切。

模块移除和重置或切换到

安装后,您可能会决定出于任何原因需要不同版本的流。第一步是移除您的包。再次使用我们的 postgresql 包示例,我们将这样做:

dnf remove postgresql

这将显示与上面安装过程类似的输出,但它将移除包及其所有依赖项。在提示时回答“y”并按 Enter 键以卸载 postgresql

完成此步骤后,您可以使用以下命令重置模块:

dnf module reset postgresql

这将产生类似以下的输出:

Dependencies resolved.
========================================================================================================================================
 Package                         Architecture                   Version                           Repository                       Size
========================================================================================================================================
Disabling module profiles:
 postgresql/client                                                                                                                     
Resetting modules:
 postgresql                                                                                                                            

Transaction Summary
========================================================================================================================================

Is this ok [y/N]:

在提示时回答“y”将把 postgresql 重置为默认流,并且我们之前启用的流(在本例中为 12)将不再启用。

Rocky Linux 8 - AppStream
Name                       Stream                 Profiles                           Summary                                            
postgresql                 9.6                    client, server [d]                 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

现在您可以使用默认值。

您还可以使用 switch-to 子命令从一个已启用的流切换到另一个。使用此方法不仅切换到新流,还安装所需的包(降级或升级),而无需单独的步骤。要使用此方法启用 postgresql 流版本 13 并使用“client”配置文件,您将使用:

dnf module switch-to postgresql:13/client

禁用模块流

有时您可能希望禁用从模块流安装包的功能。以我们的 postgresql 示例为例,这可能是因为您想直接从 PostgreSQL 使用仓库,以便您可以使用更新的版本(在撰写本文时,该仓库提供 14 和 15 版本)。禁用模块流将使安装其中任何包成为不可能,除非先重新启用它们。

要禁用 postgresql 的模块流,只需执行:

dnf module disable postgresql

如果您再次列出 postgresql 模块,您将看到以下显示所有 postgresql 模块版本均已禁用:

Rocky Linux 8 - AppStream
Name                       Stream                   Profiles                          Summary                                           
postgresql                 9.6 [x]                  client, server [d]                PostgreSQL server and client module               
postgresql                 10 [d][x]                client, server [d]                PostgreSQL server and client module               
postgresql                 12 [x]                   client, server [d]                PostgreSQL server and client module               
postgresql                 13 [x]                   client, server [d]                PostgreSQL server and client module

EPEL 仓库

什么是 EPEL 以及如何使用它?

EPEL (Extra Packages for Enterprise Linux) 是一个开源且免费的社区驱动的仓库,由 EPEL Fedora Special Interest Group 维护,它为 RHEL(以及 CentOS、Rocky Linux 等)提供来自 Fedora 源的附加包。

它提供了官方 RHEL 仓库未包含的包。这些包未包含在内,因为它们不被认为是企业环境必需的,或者超出了 RHEL 的范围。我们不能忘记 RHEL 是一个企业级发行版,桌面实用程序或其他专用软件可能不是企业项目的优先事项。

安装

安装所需的文件的过程可以很容易地通过 Rocky Linux 默认提供的包来完成。

如果您在互联网代理后面

export http_proxy=http://172.16.1.10:8080

然后

dnf install epel-release

安装完成后,您可以使用 dnf info 命令检查包是否已正确安装。

dnf info epel-release
Last metadata expiration check: 1:30:29 ago on Thu 24 Mar 2022 09:36:42 AM CET.
Installed Packages
Name         : epel-release
Version      : 8
Release      : 14.el8
Architecture : noarch
Size         : 32 k
Source       : epel-release-8-14.el8.src.rpm
Repository   : @System
From repo    : epel
Summary      : Extra Packages for Enterprise Linux repository configuration
URL          : http://download.fedoraproject.org/pub/epel
License      : GPLv2
Description  : This package contains the Extra Packages for Enterprise Linux
             : (EPEL) repository GPG key as well as configuration for yum.

如您从上面的包描述中看到的,该包不包含可执行文件、库等,只包含用于设置仓库的配置文件和 GPG 密钥。

验证正确安装的另一种方法是查询 rpm 数据库。

rpm -qa | grep epel
epel-release-8-14.el8.noarch

现在您需要运行更新以让 dnf 识别仓库。系统会要求您接受仓库的 GPG 密钥。显然,您必须回答 YES 才能使用它们。

dnf update

更新完成后,您可以使用 dnf repolist 命令检查仓库是否已正确配置,该命令现在应该列出新仓库。

dnf repolist
repo id            repo name
...
epel               Extra Packages for Enterprise Linux 8 - aarch64
epel-modular       Extra Packages for Enterprise Linux Modular 8 - aarch64
...

仓库配置文件位于 /etc/yum.repos.d/

ll /etc/yum.repos.d/ | grep epel
-rw-r--r--. 1 root root 1485 Jan 31 17:19 epel-modular.repo
-rw-r--r--. 1 root root 1422 Jan 31 17:19 epel.repo
-rw-r--r--. 1 root root 1584 Jan 31 17:19 epel-testing-modular.repo
-rw-r--r--. 1 root root 1521 Jan 31 17:19 epel-testing.repo

下面我们可以看到 epel.repo 文件的内容。

[epel]
name=Extra Packages for Enterprise Linux $releasever - $basearch
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place its address here.
#baseurl=https://download.example/pub/epel/$releasever/Everything/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-$releasever&arch=$basearch&infra=$infra&content=$contentdir
enabled=1
gpgcheck=1
countme=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8

[epel-debuginfo]
name=Extra Packages for Enterprise Linux $releasever - $basearch - Debug
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place its address here.
#baseurl=https://download.example/pub/epel/$releasever/Everything/$basearch/debug
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-$releasever&arch=$basearch&infra=$infra&content=$contentdir
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux $releasever - $basearch - Source
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place it's address here.
#baseurl=https://download.example/pub/epel/$releasever/Everything/source/tree/
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-source-$releasever&arch=$basearch&infra=$infra&content=$contentdir
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
gpgcheck=1

使用 EPEL

此时,配置完成后,我们就可以开始安装 EPEL 中的包了。首先,我们可以使用以下命令列出仓库中可用的包:

dnf --disablerepo="*" --enablerepo="epel" list available

以及命令的摘录:

dnf --disablerepo="*" --enablerepo="epel" list available | less
Last metadata expiration check: 1:58:22 ago on Fri 25 Mar 2022 09:23:29 AM CET.
Available Packages
3proxy.aarch64                                                    0.8.13-1.el8                                    epel
AMF-devel.noarch                                                  1.4.23-2.el8                                    epel
AMF-samples.noarch                                                1.4.23-2.el8                                    epel
AusweisApp2.aarch64                                               1.22.3-1.el8                                    epel
AusweisApp2-data.noarch                                           1.22.3-1.el8                                    epel
AusweisApp2-doc.noarch                                            1.22.3-1.el8                                    epel
BackupPC.aarch64                                                  4.4.0-1.el8                                     epel
BackupPC-XS.aarch64                                               0.62-1.el8                                      epel
BibTool.aarch64                                                   2.68-1.el8                                      epel
CCfits.aarch64                                                    2.5-14.el8                                      epel
CCfits-devel.aarch64                                              2.5-14.el8                                      epel
...

从命令中我们可以看到,要从 EPEL 安装,我们必须强制 dnf 使用 --disablerepo--enablerepo 选项来查询请求的仓库,因为否则在其他可选仓库(RPM Fusion、REMI、ELRepo 等)中找到的匹配项可能更新,因此具有更高的优先级。如果您只安装了 EPEL 作为可选仓库,则这些选项不是必需的,因为仓库中的包将永远不会在官方仓库中提供。至少在同一版本!

支持考虑

关于支持(更新、错误修复、安全补丁)需要考虑的一个方面是,EPEL 包没有 RHEL 的官方支持,技术上它们的生命周期可能相当于 Fedora 的开发周期(六个月),然后就会消失。这是一个远程可能性,但需要考虑。

所以,要从 EPEL 仓库安装一个包,您将使用:

dnf --disablerepo="*" --enablerepo="epel" install nmon
Last metadata expiration check: 2:01:36 ago on Fri 25 Mar 2022 04:28:04 PM CET.
Dependencies resolved.
==============================================================================================================================================================
 Package                            Architecture                          Version                                    Repository                          Size
==============================================================================================================================================================
Installing:
 nmon                               aarch64                               16m-1.el8                                  epel                                71 k

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

Total download size: 71 k
Installed size: 214 k
Is this ok [y/N]:

结论

EPEL 不是 RHEL 的官方仓库,但对于使用 RHEL 或其衍生产品并且需要一些为 RHEL 准备好的实用程序的管理员和开发人员来说,它可能很有用,并且这些实用程序来自他们可以信任的来源。

DNF 插件

dnf-plugins-core 包为 dnf 添加了用于管理您的仓库的插件。

注意

在此处查看更多信息:https://dnf-plugins-core.readthedocs.io/en/latest/index.html

在您的系统上安装该包

dnf install dnf-plugins-core

这里不会介绍所有插件,但您可以参考包文档以获取插件的完整列表和详细信息。

config-manager 插件

管理 DNF 选项,添加或禁用仓库。

示例

  • 下载 .repo 文件并使用它
dnf config-manager --add-repo https://packages.centreon.com/ui/native/rpm-standard/23.04/el8/centreon-23.04.repo
  • 您还可以将 URL 设置为仓库的基础 URL
dnf config-manager --add-repo https://repo.rocky.lan/repo
  • 启用或禁用一个或多个仓库
dnf config-manager --set-enabled epel centreon
dnf config-manager --set-disabled epel centreon
  • 在配置文件中添加代理
dnf config-manager --save --setopt=*.proxy=http://proxy.rocky.lan:3128/

copr 插件

copr 是一个自动化的 RPM forge,提供一个包含构建好的包的仓库。

  • 激活一个 copr 仓库
copr enable xxxx

download 插件

下载 rpm 包而不是安装它

dnf download ansible

如果您只想获取包的远程位置 URL

dnf download --url ansible

或者如果您还想下载依赖项

dnf download --resolv --alldeps ansible

needs-restarting 插件

运行 dnf update 后,正在运行的进程将继续运行,但使用旧的二进制文件。为了考虑代码更改,尤其是安全更新,它们需要重新启动。

needs-restarting 插件将允许您检测处于这种情况的进程。

dnf needs-restarting [-u] [-r] [-s]
选项 描述
-u 仅考虑属于运行用户的进程。
-r 检查是否可能需要重启。
-s 检查服务是否需要重启。
-s -r 一次完成两者。

versionlock 插件

有时保护包免受所有更新的影响,或排除特定版本的包(例如,由于已知问题)是有用的。为此,versionlock 插件将非常有帮助。

您需要安装一个额外的包

dnf install python3-dnf-plugin-versionlock

示例

  • 锁定 ansible 版本
dnf versionlock add ansible
Adding versionlock on: ansible-0:6.3.0-2.el9.*
  • 列出锁定的包
dnf versionlock list
ansible-0:6.3.0-2.el9.*

作者:Antoine Le Morvan

贡献者:Colussi Franco、Steven Spencer、Ganna Zhyrnova