The DNF Command Handbook

Written by: Nimesha Jinarajadasa   |   Last updated: August 7, 2022

Different software package managers are available to use with Linux distributions. The DNF is the newest addition to the Red hat-based package managers list. In this guide, we will discuss DNF commands and its usage.

What is DNF

DNF is the successor of the YUM package manager that is developed for RedHat-based Linux flavors. As the name suggests, dandified YUM operates the same as YUM but with improved performance, memory consumption, and a set of features.

DNF is used to manage the software package life cycle which includes search, installation, update, and removal of software packages. The significant thing about DNF is automatic dependency handling while establishing the work necessary to install software packages. This stamps out the manual work involved in rpm package management.

Though it detached the YUM and became the default package manager for Linux distributions such as Fedora, RHEL, AlmaLinux, Rocky Linux and it still maintains the CLI compatibility with YUM.

Syntax

The following is the basic syntax for the DNF command that accepts a command with some arguments like the package name, subcommand, or group. Additionally, you can provide optional flags to obtain a rich output.

dnf [options] <command> [<arguments> …]

Useful DNF Command in Linux

DNF supports the common commands such as install, search, remove, upgrade, reinstall, etc. In addition, it supports third-party extensions and plugins which enhance the existing DNF features and provides additional commands.

1. List packages

Package managers should be capable of listing down software packages that are available to install from the repositories. DNF uses the list command and its variants to list down the software packages based on different filter criteria.

The DNF list command lists available packages from all the third-party package repositories and the already installed software packages in your Linux machine.

dnf list

Different options can be passed to the list command to filter out the output. The following command is used to list all the installed packages in your system.

dnf list installed
dnf list installed packages
List installed packages

You can pipe the above command output with the grep command to check whether a specified package has been installed or not in the system.

dnf list installed | grep shutter

Linux can configure several third-party repositories to fetch software packages. Hence, you can run the following command to examine all the software packages that are available to be installed from enabled repositories.

dnf list available
dnf list available packages
List available packages

2. List detailed information about a package

The DNF info command is used to get the information or a summary about the packages installed or available. This command limits the output in the same way as the list command. Hence, the following command will return the information on the specified package shutter.

dnf info shutter
dnf command list detailed information of the package
Display more information about a package

The DNF info command outputs useful information related to the package shutter such as package name, version, release, size, repository, license, description, etc.

3. Search for a package

Whenever you need to install an rpm package, you might not be sure about the exact package name. In that case, the DNF search command helps you by searching and matching the guessed name with all the available packages in repositories. 

dnf search samba
dnf search for package
Search for a package

As shown in the above output, all the packages have been listed that match the specified name samba.

4. Install the package

Since we have searched for the samba, let’s install it now. The exact name should be the one without the architecture part of the search results. In this example, the ‘.x86_64’ should be omitted.

For example to install the package named samba, type:

dnf install samba
dnf install a package
install a package

You can install multiple packages by specifying their names simultaneously as arguments.

dnf install package1_name package2_name

5. Remove an installed package

Whenever you need to trash a specified package, the following command can be used.

dnf remove shutter

We can use this command to remove multiple specified packages simultaneously as in the install command.

6. Check for update

The DNF check-update command can be used to check whether the latest updates are available for a given package. Furthermore, it can be used without the package name argument that will check for all the updates in your system.

dnf check-update
dnf check updates
check for update

We can use the check-update command to check the latest updates for a specific package. Let’s run the same command to check the updates for anaconda package.

dnf check-update anaconda
dnf check updates for specific package
check update for specific package

7. System Package Upgrade

Both the DNF update and upgrade operations do the same thing where all the installed packages are checked for the latest available version and updated to the latest possible. At the same time, all the dependencies will be updated as well. The upgrade is recommended over the update command since the upgrade will remove the obsolete packages from the system. The following shows the syntax of both the commands.

dnf upgrade

Upon the execution of DNF upgrade command, system will update packages that are installed in your system.

It is possible to upgrade a specified software package using the following command. Let’s upgrade the anaconda package to its latest version.

dnf upgrade anaconda

8. List repositories

The DNF repolist command can be used with different options to list enabled, disabled, or all the repositories available to your system. The default behavior is without any options that list the enabled repositories only. In addition, you can use the DNF repolist command with the enabled option to get the same output.

dnf repolist
or 
dnf repolist enabled
dnf list repositories
List repositories

Let’s use the same command with the option all to list all the repositories known.

dnf repolist all

9. Package groups commands

The package groups are virtual collections of packages. Multiple packages of the same nature are wrapped into different groups such as Administration tools, Cloud infrastructure, Workspace, etc. DNF keeps track of these installed/available groups and is open to manipulation with DNF commands.

You can use the following command to list all the installed and available groups in your system.

dnf group list
dnf group list
List available group

The following command can be used to inspect the associated group packages. Let’s find out the associated group packages for the “Web Server” group.

dnf group info "Web Server"
dnf group info

DNF allows you to install a whole group in your system using the following command. It will install all the packages inside the group as well. In case the group has been already installed, then the missing packages will only be installed.

dnf group install "Web Server"

10. Find package for file

In some cases, we can only remember or locate a certain file but have no clue about which package it belongs to. The “DNF provides” command is very useful in these scenarios.

dnf provides zless
dnf find package for file
Find what package (installed or not) provides file

Since the zless utility belongs to the gzip package, it provides the output as above.

11. Remove orphaned packages

Several dependency packages are installed when a software package is installed. Sometimes, these dependency packages become useless when they are not used by any of the other packages. In this case, you can use the following command to remove such packages and free up your system space.

dnf autoremove

12. View History

DNF provides the ability to check the history of all transactions in your linux system. By default, it displays all the available transactions in a table view. Options can be specified to list the transactions in a given range, or by a package that has been involved in transactions.

dnf history
dnf history list
List history

13. Get package dependencies

Sometimes we need to know the dependencies of package. Use repoquery with the query option --deplist to retrieve all the dependencies for a given package. 

For example:

dnf repoquery --deplist shutter

14. List Files in a Package

Using DNF you can easily list files inside a package. Use dnf repoquery with -l option to list files in a package.

For example to list files in shutter package, type:

dnf repoquery -l shutter
dnf list files in package
List files in a package

15. Remove cached packages

Linux systems keep temporary configuration files for package repositories and these become useless when such repositories are removed or disabled. Hence, DNF offers a way to delete all these cached files from your system using the following command.

dnf clean all

Conclusion

To conclude, the DNF is the next version of the YUM package manager which has a rich set of features and high performance with low memory consumption. As you already know, it has been the default rpm package manager for most of the RPM-based distributions. Software package search, installation, update, and removal can be performed using the DNF package manager. As discussed, several DNF commands are available to manipulate rpm packages easily. Overall, the DNF package manager is a great tool to use with RedHat-based flavors to manage packages with less manual work.

About The Author

Nimesha Jinarajadasa

Nimesha Jinarajadasa

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called “03 tiers(DB, M-Tier, and Client)”. Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment