25+ dnf command examples in Linux [Cheat Sheet]

Table of Contents

Introduction to DNF command

DNF (Dandified Yum) is a package manager tool in RPM-based Linux distributions such as RHEL, CentOS, Rocky Linux, OEL and Fedora. It can install, update, or remove the software packages on the system. It is considered the next-generation version of yum package manager. It is intended to replace yum with improvement in features like dependency resolving and memory usage. It was first introduced to Fedora 18 and has been the default package manager for Fedora since Fedora 22.

 

DNF vs YUM - Overview

Both dnf and yum is used to install, remove, or update packages on the RPM-based system. DNF is the updated replacement for yum due to the unsolved issues in YUM. The problems include poor performance, high memory usage, and slow dependency resolution. DNF uses 'libsolv' library for resolving dependencies, whereas yum uses the public API. dnf is written in C, C++, and Python, which results in better performance and low memory usage. Since yum is written only in Python, it has a low performance.

Advertisement

You can also read: YUM vs DNF

 

How DNF works

  • DNF is a package management tool that relies on repositories to install/update/downgrade/remove packages.
  • So you must have a repository to be able to use dnf.
  • By default, dnf uses the global configuration file at /etc/dnf/dnf.conf and all the repository files (.repo) present in /etc/yum.repos.d.
  • The .repo files include repo name, repo path, gpg check and enabled parameter. Based on this parameter, dnf will query the repository for available packages.
  • You can also configure your own repository.

The repository file includes the following things.

[BaseOS]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

Here,

  • name: Repository name
  • mirrorlist/baseurl: Path of the repository, which contains all the packages. Supports HTTP(S), FTP(S), FILE etc. NFS is not supported directly, so you must manually first mount the NFS share and use file:// to access the repo.
  • gpgcheck: perform a checksum match using the GOG key to ensure no one has contaminated the repo. 0 (Zero) means disable the check.
  • enabled: You can enable or disable specific repo. 0 (Zero) means disabled and 1 (One) means enabled
  • gpgkey: Path of the gpgkey to be used for checksum validation if gpgcheck = 1

 

Different examples to use dnf command

Most of the dnf commands are similar to the yum commands. The root privilege is required to run the dnf command. The dnf may not be installed already in your system. You can install the DNF package using the yum command.

# yum install dnf

Then, you will be able to run the dnf command in your terminal. Below are the most used dnf commands to manage the packages and their dependencies in RPM-based systems.

 

1. dnf command to install package and it's dependencies

You can use this command to install the latest version of a package and its dependencies.

Advertisement
# dnf install package_name

Sample Output:

[root@rockylinux ~]# # dnf install iotop
Last metadata expiration check: 0:00:58 ago on Thu 16 Sep 2021 12:16:09 PM +0545.
Dependencies resolved.
================================================================================================================
 Package                  Arch                      Version                       Repository               Size
================================================================================================================
Installing:
 iotop                    noarch                    0.6-4.el7                     base                     52 k

...

Installed:
  iotop-0.6-4.el7.noarch                                                                                        

Complete!

 

2. dnf command to remove a package and its dependencies

To remove an installed package and its dependencies, you can use the following command.

# dnf remove package_name 

Sample Output:

[root@rockylinux ~]# # dnf remove iotop
Dependencies resolved.
================================================================================================================
 Package                  Arch                      Version                      Repository                Size
================================================================================================================
Removing:
 iotop                    noarch                    0.6-4.el7                    @base                    156 k

...
Removed:
  iotop-0.6-4.el7.noarch                                                                                        

Complete!

 

3. Update a package and its dependencies with dnf command

This command allows you to update any installed packages and their dependencies to the latest versions.

# dnf update package_name

Sample Output:

[root@rockylinux ~]# # dnf update python
Last metadata expiration check: 0:29:34 ago on Thu 16 Sep 2021 12:16:09 PM +0545.
Dependencies resolved.
================================================================================================================
 Package                     Arch                 Version                         Repository               Size
================================================================================================================
Upgrading:
 python                      x86_64                 2.7.5-90.el7                    updates                  96 k
 python-libs                 x86_64                 2.7.5-90.el7                    updates                 5.6 M

Transaction Summary
================================================================================================================
Upgrade  2 Packages

...

Upgraded:
  python-2.7.5-90.el7.x86_64                             python-libs-2.7.5-90.el7.x86_64                            

Complete!

You can use this command without any arguments to update every package on the system.

# dnf update

 

4. dnf command to upgrade a package and its dependencies

This command has similar function like update command, which is upgrading a package and its dependencies to the newest version.

# dnf upgrade package_name

Sample Output:

Advertisement
[root@rockylinux ~]# # dnf upgrade rsyslog
Last metadata expiration check: 0:43:27 ago on Thu 16 Sep 2021 12:16:09 PM +0545.
Dependencies resolved.
================================================================================================================
 Package                 Arch                 Version                             Repository               Size
================================================================================================================
Upgrading:
 rsyslog                 x86_64                 8.24.0-57.el7_9.1                   updates                 621 k

...

Upgraded:
  rsyslog-8.24.0-57.el7_9.1.x86_64                                                                                

Complete!

Using this command without package as an argument upgrades all the installed packages on the system.

# dnf upgrade

 

5. dnf command to check available updates for installed rpms

You can check the updates available for the installed packages and their dependencies in your system.

# dnf check-update

Sample Output:

25+ dnf command practical examples in Linux

 

6. List all the packages with dnf command

This commands lists all the packages (installed and available for installation) for RPM-based Linux distribution.

# dnf lsit

Sample Output:

[root@rockylinux ~]# # dnf list
Last metadata expiration check: 1:06:52 ago on Thu 16 Sep 2021 12:16:09 PM +0545.
Installed Packages
GConf2.x86_64                                                  3.2.6-8.el7                                @System 
GeoIP.x86_64                                                   1.5.0-14.el7                               @System 
ModemManager.x86_64                                            1.6.10-4.el7                               @System 
ModemManager-glib.x86_64                                       1.6.10-4.el7                               @System 
NetworkManager.x86_64                                          1:1.18.8-2.el7_9                           @System 
NetworkManager-adsl.x86_64                                     1:1.18.8-2.el7_9                           @System 
NetworkManager-glib.x86_64                                     1:1.18.8-2.el7_9                           @System 
NetworkManager-libnm.x86_64                                    1:1.18.8-2.el7_9                           @System 
NetworkManager-libreswan.x86_64                                1.2.4-2.el7                                @System 
NetworkManager-libreswan-gnome.x86_64                          1.2.4-2.el7                                @System 

...
Available Packages
389-ds-base.x86_64 1.3.10.2-12.el7_9 updates 
389-ds-base-devel.x86_64 1.3.10.2-12.el7_9 updates 
389-ds-base-libs.x86_64 1.3.10.2-12.el7_9 updates 
389-ds-base-snmp.x86_64 1.3.10.2-12.el7_9 updates 
Cython.x86_64 0.19-5.el7 base 
ElectricFence.x86_64 2.2.2-39.el7 base 
GConf2-devel.x86_64 3.2.6-8.el7 base 
GeoIP-data.noarch 1.5.0-14.el7 base 
GeoIP-devel.x86_64 1.5.0-14.el7 base

 

7. dnf command to get list of only installed packages

To get the list of only installed packages on the system, you can use the following command.

# dnf list installed

Sampe Output:

25+ dnf command practical examples in Linux

 

8. dnf command to search a package in available repositories

This command allows you to search a specific package by using the word. If the word matches with the name or summary of the package, it will print the result.

# dnf search [words]

Sample Output:

25+ dnf command practical examples in Linux

 

9. dnf command to get list of specific package

You can also get the list of a specific package by passing the package name as an argument. But make sure you have the exact package name or else this command will not work and may return Error: No matching Packages to list

# dnf list package_name

Sample Output:

25+ dnf command practical examples in Linux

 

10. Get information of package with dnf command

With this command, you can view the detailed information of every package available in the repositories. This command expects the exact package name.

# dnf info package_name

Sample Output:

25+ dnf command practical examples in Linux

 

11. dnf command to reinstall a package and its dependencies

Sometimes, there may come errors when using a certain package. Instead of removing it and install again, you can directly reinstall a package and its dependencies.

# dnf reinstall package_name
[root@rockylinux ~]# # dnf reinstall python
Last metadata expiration check: 0:22:43 ago on Thu 16 Sep 2021 10:41:12 PM +0545.
Dependencies resolved.
================================================================================
 Package          Arch           Version                Repository         Size
================================================================================
Reinstalling:
 python           x86_64           2.7.5-90.el7           updates            96 k

...

Reinstalled:
  python-2.7.5-90.el7.x86_64                                                      

Complete!

 

12. dnf command to download package instead of installing

You can also download package instead of installing it in the system. To do so, you can use --downloadonly option and you can specify the download directory using --downloaddir=<DIR_PATH> option.

# dnf install package_name --downloadonly --downloaddir=DIR_PATH

Sample Output:

25+ dnf command practical examples in Linux

 

13. List currently enabled repositories with dnf command

To print the list of all enabled repositories, you can use the following command.

# dnf repolist

OR

# dnf repolist enabled

Sample Output:

25+ dnf command practical examples in Linux

 

14. List currently disabled repositories with dnf command

To get the list of all disabled repositories, you can use the disabled option.

# dnf repolist disabled

Sample Output:

25+ dnf command practical examples in Linux

 

15. dnf command to list all repositories

You can also list all the repositories that are configured on your system.

# dnf repolist all

Sample Output:

25+ dnf command practical examples in Linux

 

16. Enable specific repository for runtime execution with dnf command

You can install a package from a specific repository using --enablerepo option. It can also enable a disabled repository to install a package.

# dnf install package_name --enablerepo=repo_name

OR

You can disable all the repositories and use only a single repository using:

# dnf install package_name -disablerepo=* --enablerepo=repo_name

For Example, I will install vsftpd package using appstream repo only and will disable all other repository.
Sample Output:

~]# dnf install vsftpd --disablerepo=* --enablerepo=appstream -y
Last metadata expiration check: 0:08:51 ago on Sat 18 Sep 2021 05:04:00 PM IST.
Dependencies resolved.
=====================================================================================================
 Package              Architecture         Version                     Repository               Size
=====================================================================================================
Installing:
 vsftpd               x86_64               3.0.3-33.el8                appstream               179 k

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

...

Installed:
  vsftpd-3.0.3-33.el8.x86_64

Complete!

 

 

17. Disable specific repository for runtime execution with dnf command

--disablerepo allows you to disable repository for runtime execution. It restricts the dnf to install a package from that repository. If the package is not available in other enabled repositories, you may not be able to install the package.

# yum install package_name --disablerepo=repo_name

For example, here I was having some issues with appstream repo so I have temporarily disabled it for this command and using other repos to install the package. But you should know that this command will only work if the provided package is part of a different repository. If any of the dependent package are part of the disabled repo, then the package installation will fail.

Sample Output:

 ~]# dnf install whois --disablerepo=baseos -y
Last metadata expiration check: 0:12:18 ago on Sat 18 Sep 2021 05:04:05 PM IST.
Dependencies resolved.
=====================================================================================================
 Package                Architecture        Version                     Repository              Size
=====================================================================================================
Installing:
 whois                  x86_64              5.5.1-2.el8                 appstream               77 k
Installing dependencies:
 whois-nls              noarch              5.5.1-2.el8                 appstream               37 k

...
Installed:
  whois-5.5.1-2.el8.x86_64                        whois-nls-5.5.1-2.el8.noarch

Complete!

 

18. Find which rpm package provides a specific file

You can use this command to find out which packages contain a specific file.

# dnf provides file_name

OR

# dnf whatprovides file_name

 

For Example, let us try to find the rpm which provides us the python3 binary:

dnf provides python3

Sample Output:

25+ dnf command practical examples in Linux

As you can see, it provides us with the RPM name which is responsible for installing python3 binary. There may be some more files with the same name.

To get the complete list of files by the name python3, you can use regex with dnf command.

dnf provides */python3

Sample Output:

25+ dnf command practical examples in Linux

 

19. List package's dependencies and what packages provide them

You can use deplist

command to list the package's dependencies and what packages provide them.

# dnf deplist package_name

For Example, to get the list of dependent rpms required to install nmap package we can use the following command:

dnf deplist nmap

Sample Output:

25+ dnf command practical examples in Linux

 

20. List all group packages with dnf command

Group package is the collection of multiple packages in a certain group. You can use the following command to list all the available groups for your system.

# dnf grouplist

Sample Output:
25+ dnf command practical examples in Linux

 

21. dnf command to install a group package

When you install a group package, all the packages inside the group will be installed. To install a group package, you can use this command.

# dnf install group_package

Sample Output:

[root@rockylinux ~]# dnf groupinstall 'Security Tools'
Last metadata expiration check: 0:03:47 ago on Sat 18 Sep 2021 12:32:39 PM +0545.
Dependencies resolved.
================================================================================
 Package                 Arch       Version                   Repository   Size
================================================================================
Installing group/module packages:
 scap-security-guide     noarch     0.1.54-3.el7.centos       updates     8.3 M
Installing dependencies:
 openscap                x86_64       1.2.17-13.el7_9           updates     3.9 M
 openscap-scanner        x86_64       1.2.17-13.el7_9           updates      61 k
Installing Groups:
 Security Tools                                                                

...
Installed:
  scap-security-guide-0.1.54-3.el7.centos.noarch  openscap-1.2.17-13.el7_9.x86_64 
  openscap-scanner-1.2.17-13.el7_9.x86_64          

Complete!

 

22. dnf command to remove group package

When you remove a group package, all the packages belonging to that group will also be removed.

# dnf remove group_package

Sample Output:

[root@rockylinux ~]# dnf groupremove 'Security Tools'
Dependencies resolved.
================================================================================
 Package                 Arch       Version                  Repository    Size
================================================================================
Removing:
 scap-security-guide     noarch     0.1.54-3.el7.centos      @updates     231 M
Removing unused dependencies:
 openscap                x86_64       1.2.17-13.el7_9          @updates      62 M
 openscap-scanner        x86_64       1.2.17-13.el7_9          @updates     118 k
Removing Groups:
 Security Tools                                                                

...
Removed:
  scap-security-guide-0.1.54-3.el7.centos.noarch  openscap-1.2.17-13.el7_9.x86_64 
  openscap-scanner-1.2.17-13.el7_9.x86_64          

Complete!

 

23. Update a group package using dnf command

To update an installed group package in your system, you can use this command.

# update groupupdate group_package

Sample Output:

[root@rockylinux ~]# dnf groupupdate "Headless Management"
Last metadata expiration check: 0:00:14 ago on Sat 18 Sep 2021 05:44:42 PM IST.
Dependencies resolved.
=====================================================================================================
 Package               Architecture  Version                                  Repository        Size
=====================================================================================================
Upgrading:
 cockpit-podman        noarch        32-2.module+el8.4.0+643+525e162a         appstream        409 k
Installing Groups:
 Headless Management

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

...

Upgraded:
  cockpit-podman-32-2.module+el8.4.0+643+525e162a.noarch

Complete!

 

24. View the history of all dnf commands executed in the node

You can use this command to view the information of what has happened in the past execution of dnf command.

# dnf history

Sample Output:

25+ dnf command practical examples in Linux

 

25. dnf command to undo and redo changes

You can undo and redo the changes made by dnf command execution. You will need a transaction id which you can get from history command.

To undo the change, you can use:

# dnf undo transaction_id

Sample Output:

[root@rockylinux ~]# dnf history undo 3
Last metadata expiration check: 0:29:53 ago on Sat 18 Sep 2021 12:32:39 PM +0545.
Undoing transaction 3, from Thu 16 Sep 2021 12:33:05 PM +0545
    Removed iotop-0.6-4.el7.noarch @@System
Dependencies resolved.
================================================================================
 Package          Arch              Version               Repository       Size
================================================================================
Installing:
 iotop            noarch            0.6-4.el7             base             52 k

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

...

Installed:
  iotop-0.6-4.el7.noarch                                                        

Complete!

To redo the change, you can use:

# dnf history redo transaction_id

Sample Output:

[root@rockylinux ~]# dnf history redo 3
Last metadata expiration check: 0:32:38 ago on Sat 18 Sep 2021 12:32:39 PM +0545.
Repeating transaction 3, from Thu 16 Sep 2021 12:33:05 PM +0545
    Removed iotop-0.6-4.el7.noarch @@System
================================================================================
 Package          Arch              Version              Repository        Size
================================================================================
Removing:
 iotop            noarch            0.6-4.el7            @base            156 k

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

...

Removed:
  iotop-0.6-4.el7.noarch

 

26. dnf command to undo all the transactions after the specified transaction

You can also use rollback command with dnf to undo all the transactions after the specified transactions. For example, if you have 15 transactions in the history and you specify the transaction id 10, it will undo all the transactions after 10 i.e. 11, 12, 13, 14, 15.

# dnf hisroty rollback transaction_id

You should use dnf rollback very cautiously because it will rollback all the transactions between the current and the provided transaction ID. In complex scenarios where there are multiple transactions, the dnf command will fail to perform the rollback.

For example I have following dnf history:

 ~]# dnf history
ID     | Command line                                   | Date and time    | Action(s)      | Altered
-----------------------------------------------------------------------------------------------------
     5 | install whois --disablerepo=baseos -y          | 2021-09-18 17:16 | Install        |    2
     4 | install vsftpd --disablerepo=* --enablerepo=ap | 2021-09-18 17:12 | Install        |    1
     3 | -y install nss-tools                           | 2021-08-12 10:51 | Install        |    1
     2 | -y install openldap-servers openldap-clients   | 2021-08-12 10:17 | I, U           |    3 EE
     1 |                                                | 2021-08-12 09:31 | Install        | 1384 EE

Here I had installed vsftpd as part of ID 4 and installed whois as part of ID 5, so let me try to rollback the DNF transaction to ID 3 which means vsftpd and whois package should get removed:

 ~]# dnf history rollback 3
Last metadata expiration check: 0:25:50 ago on Sat 18 Sep 2021 05:04:05 PM IST.
Dependencies resolved.
=====================================================================================================
 Package                Architecture        Version                    Repository               Size
=====================================================================================================
Removing:
 vsftpd                 x86_64              3.0.3-33.el8               @appstream              343 k
 whois                  x86_64              5.5.1-2.el8                @appstream              237 k
Removing dependent packages:
 whois-nls              noarch              5.5.1-2.el8                @appstream              104 k

Transaction Summary
=====================================================================================================
Remove  3 Packages

...

Removed:
  vsftpd-3.0.3-33.el8.x86_64       whois-5.5.1-2.el8.x86_64       whois-nls-5.5.1-2.el8.noarch

Complete!

As expected, vsftpd and whois package has been removed. Additionally whois-nls also has been removed because that was also installed with ID 5 as part of dependency.

Check the new dnf history:
25+ dnf command practical examples in Linux

 

27. dnf command to clean all the caches

To remove all the caches stored by dnf, you can use the following command.

# dnf clean all

Sample Output:

~]# dnf clean all
35 files removed

It also removes repository metadata. Due to which, when you run dnf command next time, it downloads all the metadata.

 

28. Enter the dnf shell using dnf command

You can use the interactive dnf shell where you can execute all of the dnf commands.

# dnf shell

Sample Output:

[root@rockylinux ~]# dnf shell
inLast metadata expiration check: 0:08:29 ago on Sat 18 Sep 2021 02:01:52 PM +0545.
> info rpm
Installed Packages
Name         : rpm
Version      : 4.11.3
Release      : 45.el7
Arch         : x86_64
Size         : 2.5 M
Source       : rpm-4.11.3-45.el7.src.rpm
Repo         : @System
Summary      : The RPM package management system
URL          : http://www.rpm.org/
License      : GPLv2+
Description  : The RPM Package Manager (RPM) is a powerful command line driven
             : package management system capable of installing, uninstalling,
             : verifying, querying, and updating software packages. Each
             : software package consists of an archive of files along with
             : information about the package like its version, a description,
             : etc.

> exit
Leaving Shell

 

Conclusion

In this article, we have shown you the different examples of dnf commands to install/remove/update the packages and their dependencies. You can also get the information of packages and their dependencies. dnf is a powerful tool to manage the packages and repositories in the RPM-based Linux distributions.

 

What's Next

25+ yum command practical examples in Linux

 

Further Reading

man page for dnf command

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment