CentOS8的DNF软件管理包教程:dnf如何安装/更新/删除软件包和软件包组


原文地址:CentOS8的DNF软件管理包教程:dnf如何安装/更新/删除软件包和软件包组

本篇文章详细解释了什么是CentOS8的DNF软件管理包,以及如何使用CentOS8的DNF软件包管理安装/更新/删除软件。以及如何列出已经安装的软件包和可用的软件包。

现在,DNF是大多数基于RPM的Linux发行版中的默认软件包管理系统(很尴尬,CentOS的软件管理包和DNF游戏重名了,所以百度搜到的都是DNF游戏)。

DNF从安装,卸载和更新存储库中下载软件包。它为要安装,更新或删除的软件包执行自动的依赖关系解析,并安装满足依赖关系要求所需的所有软件包。

在Fedora上,它是自Fedora 22以来的默认软件包管理器。在RHEL 8.x上,它现在是默认的软件包管理器。

DNF 通过对GPG签名的程序包启用GPG签名验证来提供安全的程序包管理。DNF将不会安装任何未使用该存储库的正确密钥签名的软件包。

为了向后兼容,RHEL 8.x也具有YUM命令,尽管它是DNF的链接。

为什么选择DNF软件管理包?

DNF或Dandified YUM是下一代YUM。与YUM相比,DNF具有更好的内存利用率,改进的依赖关系管理以及同时使用Python 2和Python 3进行运行的能力。

使用DNF的软件包管理

您必须具有超级用户特权才能使用DNF命令安装,更新或删除软件包。以下是系统管理员经常使用的一组DNF命令。

搜索包裹

要搜索所有存储库中已安装或可用的软件包,请 使用

$ sudo dnf search package_name

例如搜索包Nmap

$ sudo dnf search nmap

输出:

Last metadata expiration check: 0:05:14 ago on Tue 02 Jun 2020 08:38:09 PM UTC.
==================================== Name Exactly Matched: nmap =====================================
nmap.x86_64 : Network exploration tool and security scanner
=================================== Name & Summary Matched: nmap ====================================
nmap-ncat.x86_64 : Nmap's Netcat replacement

DNF列出已安装以及可用软件包列表

要查看所有已安装和可用软件包的列表:

$ sudo dnf list all

输出:

Installed Packages
GConf2.x86_64                                        3.2.6-22.el8                                      @rhui-rhel-8-for-x86_64-appstream-rhui-rpms
NetworkManager.x86_64                                1:1.22.8-4.el8                                    @anaconda
NetworkManager-libnm.x86_64                          1:1.22.8-4.el8                                    @anaconda
NetworkManager-team.x86_64                           1:1.22.8-4.el8                                    @anaconda
NetworkManager-tui.x86_64                            1:1.22.8-4.el8
...

列出所有已安装的软件包:

$ sudo dnf list installed

输出:

Installed Packages
NetworkManager.x86_64             1:1.22.8-4.el8          @anaconda
NetworkManager-libnm.x86_64       1:1.22.8-4.el8          @anaconda
NetworkManager-team.x86_64        1:1.22.8-4.el8          @anaconda
NetworkManager-tui.x86_64         1:1.22.8-4.el8          @anaconda
PackageKit.x86_64                 1.1.12-4.el8            @AppStream
PackageKit-glib.x86_64            1.1.12-4.el8            @AppStream
WALinuxAgent.noarch               2.2.38-1.el8            @rhui-rhel-8-for-x86_64-appstream-rhui-rpms
abattis-cantarell-fonts.noarch    0.0.25-4.el8            @AppStream
acl.x86_64                        2.2.53-1.el8            @anaconda
...

列出所有可用的软件包:

$ sudo dnf list available

输出:

Last metadata expiration check: 1:15:10 ago on Tue 02 Jun 2020 05:37:11 PM UTC.
Available Packages
CUnit.i686                                           2.1.3-17.el8                                      rhui-rhel-8-for-x86_64-appstream-rhui-rpms
CUnit.x86_64                                         2.1.3-17.el8                                      rhui-rhel-8-for-x86_64-appstream-rhui-rpms
CUnit-devel.i686                                     2.1.3-17.el8                                      rhui-codeready-builder-for-rhel-8-x86_64-rhui-rpms
CUnit-devel.x86_64                                   2.1.3-17.el8                                      rhui-codeready-builder-for-rhel-8-x86_64-rhui-rpms
...

例如,列出所有可用的MySQL软件包:

$ sudo dnf list available "mysql*"

输出:

Last metadata expiration check: 1:17:04 ago on Tue 02 Jun 2020 05:37:11 PM UTC.
Available Packages
mysql.x86_64        8.0.17-3.module+el8.0.0+3898+e09bb8de  rhui-rhel-8-for-x86_64-appstream-rhui-rpms
mysql-common.x86_64 8.0.17-3.module+el8.0.0+3898+e09bb8de  rhui-rhel-8-for-x86_64-appstream-rhui-rpms
mysql-devel.x86_64  8.0.17-3.module+el8.0.0+3898+e09bb8de  rhui-rhel-8-for-x86_64-appstream-rhui-rpms
mysql-errmsg.x86_64 8.0.17-3.module+el8.0.0+3898+e09bb8de  rhui-rhel-8-for-x86_64-appstream-rhui-rpms
mysql-libs.x86_64   8.0.17-3.module+el8.0.0+3898+e09bb8de  rhui-rhel-8-for-x86_64-appstream-rhui-rpms
mysql-server.x86_64 8.0.17-3.module+el8.0.0+3898+e09bb8de  rhui-rhel-8-for-x86_64-appstream-rhui-rpms
mysql-test.x86_64   8.0.17-3.module+el8.0.0+3898+e09bb8de  rhui-rhel-8-for-x86_64-appstream-rhui-rpms

显示package信息

要显示有关package的信息:

$ sudo dnf info package_name

例如,要显示有关httpd服务器的信息:

$ sudo dnf info httpd

输出:

Last metadata expiration check: 0:15:04 ago on Mon 01 Jun 2020 11:23:11 PM UTC.
Available Packages
Name         : httpd
Version      : 2.4.37
Release      : 21.module+el8.2.0+5008+cca404a3
Architecture : x86_64
Size         : 1.4 M
Source       : httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.src.rpm
Repository   : rhui-rhel-8-for-x86_64-appstream-rhui-rpms
Summary      : Apache HTTP Server
URL          : https://httpd.apache.org/
License      : ASL 2.0
Description  : The Apache HTTP Server is a powerful, efficient, and extensible
             : web server.

DNF安装软件包

安装单个软件包

要安装单个软件包及其所有未安装的依赖项,请执行以下操作:

$ sudo dnf install package_name

例如,安装Nginx

$ sudo dnf install nginx

输出:

Dependencies resolved.
===========================================================================================
 Package             Arch   Version       Repository                                  Size
===========================================================================================
Installing:
 nginx               x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe
                                          rhui-rhel-8-for-x86_64-appstream-rhui-rpms 570 k
Installing dependencies:
 gd                  x86_64 2.2.5-6.el8   rhui-rhel-8-for-x86_64-appstream-rhui-rpms 144 k
 jbigkit-libs        x86_64 2.1-14.el8    rhui-rhel-8-for-x86_64-appstream-rhui-rpms  55 k
..

这将安装Nginx及其所有未安装的依赖项要求。

dnf安装多个软件包

您还可以通过将软件包名称添加到此命令来安装多个软件包。例如,要安装Nginx,PHP和MySQL服务器,请输入:

$ sudo dnf install nginx php mysql-server

输出:

Dependencies resolved.
===========================================================================================
 Package          Arch   Version          Repository                                  Size
===========================================================================================
Installing:
 mysql-server     x86_64 8.0.17-3.module+el8.0.0+3898+e09bb8de
                                          rhui-rhel-8-for-x86_64-appstream-rhui-rpms  22 M
 nginx            x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe
                                          rhui-rhel-8-for-x86_64-appstream-rhui-rpms 570 k
 php              x86_64 7.2.24-1.module+el8.2.0+4601+7c76a223
                                          rhui-rhel-8-for-x86_64-appstream-rhui-rpms 1.5 M
Installing dependencies:
 apr              x86_64 1.6.3-9.el8      rhui-rhel-8-for-x86_64-appstream-rhui-rpms 125 k
 apr-util         x86_64 1.6.1-6.el8      rhui-rhel-8-for-x86_64-appstream-rhui-rpms 105 k
 gd               x86_64 2.2.5-6.el8      rhui-rhel-8-for-x86_64-appstream-rhui-rpms 144 k
 httpd            x86_64 2.4.37-21.module+el8.2.0+5008+cca404a3
                                          rhui-rhel-8-for-x86_64-appstream-rhui-rpms 1.4 M
 httpd-filesystem noarch 2.4.37-21.module+el8.2.0+5008+cca404a3
...

通过RPM二进制文件名安装软件包

如果您知道要安装的RPM二进制文件的名称,但不知道软件包名称,那么即使足够安装相关的软件包也是如此。

例如,如果输入:

$ sudo dnf install /usr/sbin/named

输出:

Last metadata expiration check: 1:21:25 ago on Tue 02 Jun 2020 05:37:11 PM UTC.
Dependencies resolved.
=====================================================================================================
 Package   Arch        Version                 Repository                                       Size
=====================================================================================================
Installing:
 bind      x86_64      32:9.11.13-5.el8_2      rhui-rhel-8-for-x86_64-appstream-rhui-rpms      2.1 M

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

Total download size: 2.1 M
Installed size: 4.5 M
Is this ok [y/N]:

DNF搜索并找到提供了/usr/sbin/named的软件包,并提示您安装它。

查找哪个包拥有文件

如果您知道文件名并想找到提供它的软件包(是否安装),请使用:

$ sudo dnf provides file_name

例如,要查找哪个包提供命名的包,供DNS服务器使用的二进制文件: 

$ sudo dnf provides named

输出:

 

Last metadata expiration check: 1:27:30 ago on Mon 01 Jun 2020 11:23:11 PM UTC.
bind-32:9.11.4-16.P2.el8.x86_64 :
The Berkeley Internet Name Domain (BIND) DNS (Domain Name System) :
server Repo : rhui-rhel-8-for-x86_64-appstream-rhui-rpms Matched from: Filename :
/usr/sbin/named

DNF移除(卸载)软件包

删除软件包:

sudo dnf remove package_name

但是请注意,DNF无法在不删除依赖于该软件包的软件包的情况下删除该软件包。要删除软件包nmap,请使用:

$ sudo dnf remove nmap
Dependencies resolved.
=====================================================================================================
 Package    Architecture Version             Repository                                         Size
=====================================================================================================
Removing:
 nmap       x86_64       2:7.70-5.el8        @rhui-rhel-8-for-x86_64-appstream-rhui-rpms        24 M

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

Freed space: 24 M
Is this ok [y/N]:

管理软件包组 package group

程序包组是紧密相关并用于共同目的的程序包的集合。安装软件包组将安装一组软件包,从而节省大量时间。

dnf列出所有包裹组

列出所有软件包组:

$ sudo dnf group list

输出:

Available Environment Groups:
   Server with GUI
   Server
   Workstation
   Custom Operating System
   Virtualization Host
Installed Environment Groups:
   Minimal Install
Installed Groups:
   Security Tools
   Container Management
Available Groups:
   Legacy UNIX Compatibility
   Development Tools
   Scientific Support
   .NET Core Development
   Graphical Administration Tools
   Headless Management
   RPM Development Tools
   Network Servers
   System Tools
   Smart Card Support

dnf列出组中的所有软件包

列出组中的所有软件包:

$ sudo dnf group info package_name

例如,要查看“安全工具”组中的软件包:

$ sudo dnf group info “security tools”

输出:

Group: Security Tools
 Description: Security tools for integrity and trust verification.
 Default Packages:
   scap-security-guide
 Optional Packages:
   aide
   hmaccalc
   openscap
   openscap-engine-sce
   openscap-utils
   scap-security-guide-doc
   scap-workbench
   tpm-quote-tools
   tpm-tools
   tpm2-tools
   trousers

dnf安装软件包组

安装组将在组中安装所有软件包。要安装软件包组,请使用:

sudo dnf group install package_group_name

例如,要安装“网络服务器”组:

$ sudo dnf group install “Network Servers”

输出:

Last metadata expiration check: 1:40:14 ago on Wed 03 Jun 2020 05:53:11 PM UTC.
Dependencies resolved.
===========================================================================================
 Package              Architecture        Version               Repository            Size
===========================================================================================
Installing Groups:
 Network Servers

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

Is this ok [y/N]:

dnf删除软件包组

删除软件包组:

sudo dnf remove package_name

因此,要删除“安全工具”软件包组:

$ sudo dnf group remove "security tools"

输出:

Dependencies resolved.
=====================================================================================================
 Package               Arch     Version          Repository                                     Size
=====================================================================================================
Removing:
 scap-security-guide   noarch   0.1.48-7.el8     @rhui-rhel-8-for-x86_64-appstream-rhui-rpms   182 M
Removing unused dependencies:
 GConf2                x86_64   3.2.6-22.el8     @rhui-rhel-8-for-x86_64-appstream-rhui-rpms   6.3 M
 libxslt               x86_64   1.1.32-4.el8     @rhui-rhel-8-for-x86_64-baseos-rhui-rpms      734 k
 openscap              x86_64   1.3.2-6.el8      @rhui-rhel-8-for-x86_64-appstream-rhui-rpms    58 M
 openscap-scanner      x86_64   1.3.2-6.el8      @rhui-rhel-8-for-x86_64-appstream-rhui-rpms   137 k
 xml-common            noarch   0.6.3-50.el8     @rhui-rhel-8-for-x86_64-baseos-rhui-rpms       78 k
Removing Groups:
 Security Tools

Transaction Summary
=====================================================================================================
Remove  6 Packages

Freed space: 247 M
Is this ok [y/N]: 

dnf更新软件包

更新所有软件包

要更新所有软件包及其依赖性,可以使用:

$ sudo dnf upgrade

输出:

Last metadata expiration check: 1:09:31 ago on Tue 02 Jun 2020 08:38:09 PM UTC.
Dependencies resolved.
Nothing to do.
Complete!

这将安装所有可以安装的更新。无论DNF升级和更新DNF有同样的效果,但DNF升级首选。

dnf更新单个软件包

要更新单个软件包,请运行:

$ sudo dnf upgrade package_name

例如,要更新python包:

$ sudo dnf upgrade python

总结

学习DNF可能是有益的,因为它通常是执行系统管理任务的最快方法。关于CentOS DNF如何使用的教程就分享到这里。

评论

登录后评论

服务器优惠活动

Top