DNF软件包管理

时间:2019-04-29 03:17:29  来源:igfitidea点击:

DNF是一种新的软件包管理工具,旨在替代YUM软件包管理器。

什么是DNF?

DNF(Dandified Yum)是yum(Yellowdog Update Modified)软件包管理器的下一代。DNF的创建是对yum软件包管理器的改进,旨在解决性能,内存和依赖关系解决问题。DNF最终将取代基于RPM的系统上当前找到的yum软件包管理器。DNF最初是在Fedora 18中引入的,现在是Fedora 25的默认软件包管理器。 在CentOS 8中也开始使用这种管理器。

DNF命令示例

以下是一些使用DNF在Linux系统上安装,删除,更新软件包的示例。以下示例均在Fedora Linux服务器上执行。

显示DNF版本

[root@fedora22srv ~]# dnf --version
1.0.0
  Installed: dnf-0:1.0.0-1.fc22.noarch at 2015-10-25 23:16
  Built    : Fedora Project at 2015-05-02 13:00

显示存储库

[root@fedora22srv ~]# dnf repolist

Last metadata expiration check performed 0:00:49 ago on Tue Oct 27 10:10:09 2015.
repo id                    repo name                                       status
*fedora                    Fedora 22 - x86_64                              44,762
*updates                   Fedora 22 - x86_64 - Updates                    16,035

将显示所有启用的存储库。您可以传递一个附加参数all以显示启用和禁用的存储库。

列出已安装的软件包

[john@fedora22srv ~]$ dnf list installed

Last metadata expiration check performed 0:04:23 ago on Tue Oct 27 10:10:09 2015.
Installed Packages
NetworkManager.x86_64                   1:1.0.2-1.fc22                   @System
NetworkManager-libnm.x86_64             1:1.0.2-1.fc22                   @System

此命令将显示系统上所有已安装的软件包。上面的输出只是所显示信息的一部分。

显示可用和已安装的软件包

[john@fedora22srv ~]$ dnf list 

Last metadata expiration check performed 0:07:21 ago on Tue Oct 27 10:10:09 2015.
Installed Packages
NetworkManager.x86_64                    1:1.0.2-1.fc22                  @System
NetworkManager-libnm.x86_64              1:1.0.2-1.fc22                  @System
PackageKit.x86_64                        1.0.6-4.fc22                    @System

在上面的示例中,我们可以看到Installed PackagesAvailable Packages

仅显示可用软件包

[root@fedora22srv ~]# dnf list available

Last metadata expiration check performed 0:12:53 ago on Tue Oct 27 10:10:09 2015.
Available Packages
0ad.x86_64                               0.0.18-1.fc22                   fedora

显示软件包信息

[root@fedora22srv ~]# dnf info htop

Last metadata expiration check performed 0:41:34 ago on Tue Oct 27 09:51:50 2015.
Available Packages
Name        : htop
Arch        : x86_64
Epoch       : 0

上面的命令显示有关指定软件包的信息。提供发行/版本和体系结构信息

搜索软件包

[john@fedora22srv tmp]$ dnf search htop

显示哪个包提供指定的文件。

[root@fedora22srv ~]# dnf provides htop

Last metadata expiration check performed 0:40:19 ago on Tue Oct 27 09:51:50 2015.
htop-1.0.3-4.fc22.x86_64 : Interactive process viewer
Repo        : fedora

安装软件包

dnf install命令用于安装指定的软件包。显示所有依赖关系,然后为指定的软件包安装。

[root@fedora22srv ~]# dnf install htop
Last metadata expiration check performed 0:44:44 ago on Tue Oct 27 09:51:50 2015.
Dependencies resolved.
=================================================================================
 Package        Arch             Version                  Repository        Size
=================================================================================
Installing:
 htop           x86_64           1.0.3-4.fc22             fedora            91 k

重新安装软件包

dnf reinstall命令用于重新安装指定的软件包。

[root@fedora22srv ~]# dnf reinstall htop

Last metadata expiration check performed 0:06:19 ago on Tue Oct 27 11:18:12 2015.
Dependencies resolved.
=================================================================================
 Package        Arch             Version                  Repository        Size
=================================================================================
Reinstalling:
 htop           x86_64           1.0.3-4.fc22             fedora            91 k

更新特定软件包

dnf update命令用于更新特定程序包。在使用更新选项之前,检查软件包的当前版本并检查是否有较新的版本很有用。为此,我们使用dnf list命令显示当前安装的版本以及可用的任何版本。

[root@fedora22srv ~]# dnf list zip.x86_64

Last metadata expiration check performed 0:46:26 ago on Tue Oct 27 09:51:50 2015.
Installed Packages
zip.x86_64                          3.0-13.fc22                           @System
Available Packages
zip.x86_64                          3.0-14.fc22                           updates

从上面的输出中,我们可以看到软件包zip.X86_64的版本为3.0-13.fc22,并且提供了更新版本3.0-14.fc22

[root@fedora22srv ~]# dnf update zip.x86_64
Last metadata expiration check performed 0:49:37 ago on Tue Oct 27 09:51:50 2015.
Dependencies resolved.
=================================================================================
 Package       Arch             Version                  Repository         Size
=================================================================================
Upgrading:
 zip           x86_64           3.0-14.fc22              updates           267 k

降级软件包

dnf downgrade命令用于将指定的软件包降级为所有已知的较低版本中的最高版本。

[root@fedora22srv ~]# dnf list zip.x86_64
Last metadata expiration check performed 0:10:01 ago on Tue Oct 27 11:18:12 2015.
Installed Packages
zip.x86_64                          3.0-14.fc22                          @updates


[root@fedora22srv ~]# dnf downgrade zip.x86_64

Last metadata expiration check performed 0:10:10 ago on Tue Oct 27 11:18:12 2015.
Dependencies resolved.
=================================================================================
 Package        Arch              Version                Repository         Size
=================================================================================
Downgrading:
 zip            x86_64            3.0-13.fc22            fedora            267 k

删除软件包

dnf remove命令用于删除指定的程序包。您也可以使用erase选项,但是erase现已被弃用。

[root@fedora22srv ~]# dnf remove htop
Dependencies resolved.
=================================================================================
 Package        Arch             Version                 Repository         Size
=================================================================================
Removing:
 htop           x86_64           1.0.3-4.fc22            @System           180 k

检查系统更新

dnf check-update命令用于检查Linux系统上的可用更新。检查更新检查指定软件包的任何可用更新。如果没有给出软件包名称,它将检查系统中所有可用的更新。

[root@fedora22srv ~]# dnf check-update

Last metadata expiration check performed 0:54:40 ago on Tue Oct 27 09:51:50 2015.

NetworkManager.x86_64                  1:1.0.6-6.fc22                     updates
NetworkManager-libnm.x86_64            1:1.0.6-6.fc22                     updates
PackageKit.x86_64                      1.0.10-1.fc22                      updates
PackageKit-glib.x86_64                 1.0.10-1.fc22                      updates
abrt.x86_64                            2.6.1-5.fc22                       updates

列出可用更新

dnf list updates命令用于列出系统可用的所有可用更新。

Last metadata expiration check performed 0:56:17 ago on Tue Oct 27 09:51:50 2015.
Upgraded Packages
NetworkManager.x86_64                  1:1.0.6-6.fc22                     updates
NetworkManager-libnm.x86_64            1:1.0.6-6.fc22                     updates
PackageKit.x86_64                      1.0.10-1.fc22                      updates

更新所有软件包

dnf update命令用于更新系统上的所有软件包。可以指定单个包装。如果未指定软件包,则将更新所有软件包

root@fedora22srv ~]# dnf update

Last metadata expiration check performed 0:58:50 ago on Tue Oct 27 09:51:50 2015.
Dependencies resolved.
================================================================================
 Package                      Arch   Version                      Repository
                                                                           Size
================================================================================
Installing:
 abrt-dbus                    x86_64 2.6.1-5.fc22                 updates 127 k
 audit-libs-python            x86_64 2.4.4-1.fc22                 updates  96 k
Transaction Summary
================================================================================
Install   2 Packages
Upgrade  36 Packages

Total download size: 27 M
Is this ok [y/N]:y

软件包整理

dnf autoremove命令用于从系统中删除所有软件包,这些软件包最初是作为用户安装的软件包的依赖项安装的,但不再是任何此类软件包所必需的。将显示所有可以删除的软件包。

[root@fedora22srv ~]# dnf autoremove
Last metadata expiration check performed 1:52:57 ago on Tue Oct 27 11:18:12 2015.
Dependencies resolved.
=================================================================================
 Package                   Arch     Version                    Repository   Size
=================================================================================
Removing:
 aajohan-comfortaa-fonts   noarch   2.004-4.fc22               @anaconda   515 k
 cairo                     x86_64   1.14.2-1.fc22              @anaconda   1.7 M
 cairo-gobject             x86_64   1.14.2-1.fc22              @anaconda    35 k
 fontconfig                x86_64   2.11.94-4.fc22             @updates    604 k

Transaction Summary
=================================================================================
Remove  4 Packages

Installed size: 9.2 M
Is this ok [y/N]: 

使用软件包组

许多软件包被分组为方便的组。这使系统管理员可以使用一个命令安装一组软件包。命令dnf grouplist可用于显示系统上的可用组。

[root@fedora22srv ~]# dnf group list

Last metadata expiration check performed 0:12:24 ago on Tue Oct 27 11:18:12 2015.
Available environment groups:
   Minimal Install
   Fedora Server
   Fedora Workstation

安装一组软件包

在下面的示例中,我们将安装许多可用的编辑工具。要安装所有可用的Editors,我们执行命令dnf groupinstall Editors。(请注意,如果组包的名称中包含空格,则必须在组名周围加上单引号。)

[root@fedora22srv ~]# dnf groupinstall Editors

Last metadata expiration check performed 0:13:54 ago on Tue Oct 27 11:18:12 2015.

更新一组软件包

在下面的示例中,我们将更新Editors组中的所有软件包。

[root@fedora22srv ~]# dnf groupupdate Editors

删除一组软件包

在下面的示例中,我们将删除一个组。要删除组Editors,请使用命令dnf groupremove Editors。(请注意,如果组包的名称中包含空格,则必须在组名的周围加上单引号)。

[root@fedora22srv ~]# dnf groupremove Editors

Last metadata expiration check performed 0:18:34 ago on Tue Oct 27 11:18:12 2015.
Dependencies resolved.
=================================================================================
 Group          Packages                                                       
=================================================================================
Marking removed:
 Editors        xemacs-packages-extra      nedit                     zile      
                xmlcopyeditor              emacs-bbdb                geany     

同步所有软件包

dnf distro-sync会将所有软件包升级或降级为可从任何启用的存储库获得的最新版本。

[root@fedora22srv ~]# dnf distro-sync
Last metadata expiration check performed 0:23:39 ago on Tue Oct 27 11:18:12 2015.
Dependencies resolved.
=================================================================================
 Package       Arch             Version                  Repository         Size
=================================================================================
Upgrading:
 zip           x86_64           3.0-14.fc22              updates           267 k

Transaction Summary
=================================================================================
Upgrade  1 Package

Total download size: 267 k
Is this ok [y/N]: y
Downloading Packages:
zip-3.0-14.fc22.x86_64.rpm                       703 kB/s | 267 kB     00:00    
---------------------------------------------------------------------------------
Total                                            187 kB/s | 267 kB     00:01     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Upgrading   : zip-3.0-14.fc22.x86_64                                       1/2 
  Cleanup     : zip-3.0-13.fc22.x86_64                                       2/2 
  Verifying   : zip-3.0-14.fc22.x86_64                                       1/2 
  Verifying   : zip-3.0-13.fc22.x86_64                                       2/2 

Upgraded:
  zip.x86_64 3.0-14.fc22                                                         

Complete!
  

清理临时文件

dnf clean命令用于为Linux系统上任何当前启用的存储库执行临时文件的清除。提供各种选项来执行特定功能。dnf clean all将执行所有选项。

dnf clean dbcache

删除从存储库元数据生成的缓存文件。这将强制DNF在下次运行时重新生成缓存文件。

dnf clean expire-cache

删除本地cookie文件,该文件说明何时为每个存储库下载了元数据和镜像列表。DNF将在下一次使用每个存储库时重新验证该缓存。

dnf clean metadata

删除存储库元数据。这些是DNF用于确定程序包的远程可用性的文件。使用此选项将使DNF在下次运行时下载所有元数据。

dnf clean packages

从系统中删除所有缓存的软件包。

dnf clean plugins

告诉所有启用的插件消除其缓存的数据。

dnf clean all

执行上述所有操作。

[root@fedora22srv ~]# dnf clean all

Cleaning repos: fedora updates
Cleaning up Everything

显示历史dnf命令记录

dnf history命令用于显示过去发生的事务。

[root@fedora22srv ~]# dnf history

Last metadata expiration check performed 2:52:46 ago on Tue Oct 27 11:18:12 2015.
ID     | Command line             | Date a | Action | Altere
    3 | remove htop              | 2015-10-27 10:36 | Erase          |    1   
     2 | install htop             | 2015-10-27 10:36 | Install        |    1   
     1 |                          | 2015-10-25 23:14 | Install        |  658 EE

获得DNF帮助

dnf help命令用于显示可用命令的列表。您也可以在帮助选项之后指定命令以显示特定信息。下面是使用中的dnf help命令的一些示例。有关dnf命令可用选项的完整详细信息,可以通过执行命令man dnf来查询dnf的手册页。另一个有用的命令是man yum2dnf。此手册页显示yum软件包管理器和dnf之间的区别。

[root@fedora22srv ~]# dnf help install
install PACKAGE...

Install a package or packages on your system


[root@fedora22srv ~]# dnf help remove
remove PACKAGE...

Remove a package or packages from your system

alias: erase


[root@fedora22srv ~]# dnf help list
list [PACKAGE|all|available|installed|updates|extras|autoremove|obsoletes|recent]

List a package or groups of packages
[root@fedora22srv ~]# dnf help update
upgrade [PACKAGE...]

Upgrade a package or packages on your system

alias: update