21 DNF Commands to Manage RPM Based Linux Distributions

Hi everyone, here we'll learn about DNF commands and how we can use it to manage RPM-based Linux Distributions. Dandified YUM is also known as DNF, is the new version of YUM which is a package manager for RPM-based Linux Distribution. DNF is a fork of YUM and is the default package manager in Fedora  22 and later versions of Fedora. 

Commands available for DNF Package Manager can be displayed by running dnf help in the terminal or shell.

$ dnf help

usage: dnf [options] COMMAND

List of Main Commands

autoremove
check-update                      Check for available package upgrades
clean                                     Remove cached data
distro-sync                          Synchronize installed packages to the latest available versions
downgrade                          downgrade a package
group                                    Display, or use, the groups information
help                                       Display a helpful usage message
history                                  Display, or use, the transaction history
info                                       Display details about a package or group of packages
install                                   Install a package or packages on your system
list                                         List a package or groups of packages
makecache                          Generate the metadata cache
provides                               Find what package provides the given value
reinstall                                reinstall a package
remove                                 Remove a package or packages from your system
repolist                                 Display the configured software repositories
repository-packages          Run commands on top of all packages in given repository
search                                   Search package details for the given string
updateinfo                           Display advisories about packages
upgrade                                Upgrade a package or packages on your system
upgrade-to                           Upgrade a package on your system to the specified version

Here are some usages of main commands that we can run with DNF Package Manager with their possible examples. Please make sure that you run the commands as a sudo or privileged user. permission.

1. Autoremove

Autoremove command removes all the packages those are unnecessary to the system which where installed as dependencies while installing previous packages.

To run autoremove, we can execute dnf [option] autoremove in the command interface.

$ sudo dnf autoremove

2. Check Update Command

Check Update command non-interactively checks if updates of the specified packages are available. DNF exit code will be 100 when there are updates available and a list of the updates will be printed, 0 if not and 1 if an error occurs. Please note that having a specific newer version available for an installed package (and reported by check-update) does not imply that subsequent dnf upgrade will install it.

We can run check update using the following command:

$ sudo dnf check-update python

3. Clean Command

Clean Command of DNF Package Manager cleans all the temporary files of the currently enabled repositories. Clean command requires an option as packages, metadata, dbcache, plugins, expire-cache, rpmdb, all.

The following command removes cache files generated from the repository metadata which forces DNF to regenerate the cache files the next time it is run.

$ sudo dnf clean dbcache

The following command removes repository metadata which are the files that DNF uses to determine the remote availability of packages. It will make DNF to download the metadata when it is run next time.

$ sudo dnf clean metadata

To remove any cached packages in the system, type:

$ sudo dnf clean packages

To remove cached data from the plugins, type

$ sudo dnf clean plugins

To remove local cookie files, when the metadata and mirrorlists were downloaded for each repo.

$ sudo dnf clean expire-cache

To clean up database created by rpm and will regenerate when running next time, type:

$ sudo dnf clean rpmdb

4. Distro-sync command

Distro-sync upgrades, downgrades or keeps selected installed packages to match the latest version of the packages available in the enabled repositories. If no package is specified, all installed packages are considered for distro-sync.

$ sudo dnf distro-sync python

5. Downgrade

Downgrade command downgrades the installed package to the highest of all the available lower versions of the package. We can even define a version to which we want to downgrade.

$ sudo dnf downgrade firefox

6. Group

Groups are the collection of packages in the repository which are tracked by DNF that helps the user to install all the necessary packages and manipulate the packages just at a single simple command.

Note: Here, <group-spec> is [PACKAGE|all|available|installed|updates|extras|autoremove|obsoletes|recent] .

dnf [options] group summary <group-spec>

The above command displays overview of how many groups are installed and are available. Here, summary is the default groups subcommand.

dnf [options] group info <group-spec>

The above command displays package lists of a specified group which are installed or available from a repo when -v is used.

dnf [options] group install with-optional <group-spec>

Above command marks the specified group installed and install packages it contains. It also includes additional optional packages of the group if with-optional is specified in the command line.

dnf [options] group list <group-spec>

It lists all matching groups, either among installed or available groups. The records are ordered by display_order tag defined in comps.xml file.

dnf [options] group remove <group-spec>

Above command marks the group as removed then it removes those packages in the group from the system which are neither comprising another installed group and were installed unnecessarily as dependencies.

dnf [options] group upgrade <group-spec>

This command upgrades both the group and packages of the group.
Groups can be also be marked installed or removed without physically manipualting any packages:

dnf [options] group mark install <group-spec>

It marks the specified group as installed but no packages will be installed by this command but the group is then considered as installed.

dnf [options] group mark remove <group-spec>

It marks the specified group as removed but no packages will be removed by this command but the group is then considered as removed.

7. Help Command

Help command displays all the available commands, options and information on how we can run that and what the command does with the necessary syntax.

$ sudo dnf help group

8. History Command

The history command allows us to view the past transactions made by DNF.

$ sudo dnf history list

To lists history of all the transactions in a table.

$ sudo dnf history info [<spec>]

The above command displays the description of the specified transaction. Here, [<spec>] may be any transaction id or any transaction of a package. If no specification is mentioned, the latest transaction information will be displayed.

$ sudo dnf history redo <transaction-spec>

This command repeats a specified transaction. Here, <transaction-spec> is the transaction id that we want to redo.

$ sudo dnf history rollback <transaction-spec>

This command rollbacks all the transaction performed after specified transaction. Here, <transaction-spec> is the transaction id that we want to rollback.

$ sudo dnf history undo <transaction-spec>

It performances an opposite operation done by the specified transaction. Here, <transaction-spec> is the transaction id that we want to undo.

$ sudo dnf history userinstalled

It lists all the packages that were installed by a user. The output can be used as the %packages section in a kickstart file.

9. Info Command

Info is command with DNF is used to display the summary and description about the available packages. This command by default does not force a sync of expired metadata.

$ sudo dnf info firefox

10. Install Command

This command will install the specified package and its dependencies from the Repository or the local path for the RPM package or the specified group where the required package is tagged.

To Install a specific package:

$ sudo dnf install lynx

Group Installation

$ sufo dnf install @"Minimal Install"

11. List Command

List Command displays the lists of packages depending on the package(s) relation to the system whether it is available in the repository or installed in the system. We can also specify package name so that the list command displays about the specified packages only.

Lists packages or specified package which are present in the RPMDB, in a repo or in both.

dnf [options] list all [<package-name>]

Lists the installed packages or installed specified packages

dnf [options] list installed [<package-name>]

Lists the available packages or available specified packages.

dnf [options] list available [<package-name>]

Lists  the extra packages that are installed but are not available in any known repositories

dnf [options] list extras [<package-name>]

Lists the obsoletes packages which are installed but are not available in any known repositories.

dnf [options] list obsoletes [<package-name>]

Lists the recently added packages in the repositories.

dnf [options] list recent [<package-name>]

Lists the upgrades available for all the packages or specified packages.

dnf [options] list upgrades [<package-name>]

Lists all the packages that will be removed by dnf autoremove command.

dnf [options] list autoremove

12. Makecache Command

This command creates metadata cache for downloads and makes sure that it doesn't download the metadata if its available in the cache whenever its possible until the local metadata is expired or timestamp is changed.

$ sudo dnf makecache

To instruct DNF to be more resource-aware and toterminate immediately if it’s too soon after the last successful makecache run we add timer to it.

$ sudo dnf makecache timer

13. Provides Command

Provide command finds the packages providing the given <provide-spec>. It is useful when one knows a filename and wants to find what package provides that file.

$ sudo dnf provides /usr/bin/cat

14. Reinstall Command

It reinstalls the specified packages and fails to install if its not installed or if its unavailable in the system.

$ sudo dnf reinstall firefox

15. Remove Command

Remove command uninstalls the specified package which is installed in the system along with the packages depending on it. Here, <package-specs> can be either specified package name or group name. If clean_requirements_on_remove is enabled, it also removes any dependencies that are no longer needed.

$ sudo dnf remove lynx

16. Repolist Command

Repolist command lists all enabled repositories by default. We can list all the enabled, disabled and all repositories added in the system. For more information about those repositories, we can add -v flag into the command.

$ sudo dnf repolist

Repolist with more information

$ sudo dnf repolist -v

17. Repository-Packages Command

The repository-packages command allows us to run commands on top of all packages in the repository named <repoid>.

Note: We can get the repoid from dnf repolist command.

dnf [options] repository-packages <repoid> check-update [<package-name-spec>]

It checks if updates of the specified packages in the repository are available or not.

dnf [options] repository-packages <repoid> info [all] [<package-name-spec>]

It lists all the related packages.

dnf [options] repository-packages <repoid> info installed [<package-name-spec>]

It lists all packages installed from the repository.

dnf [options] repository-packages <repoid> info available [<package-name-spec>]

It lists the specified packages that are available in the repository.

dnf [options] repository-packages <repoid> info extras [<package-name-specs>]

It lists packages installed from the repository that are not available in any repository.

dnf [options] repository-packages <repoid> info obsoletes [<package-name-spec>]

It lists obsolete packages that are installed on the system and is available in the repository.

dnf [options] repository-packages <repoid> info recent [<package-name-spec>]

It lists packages which are recently added into the repository.

dnf [options] repository-packages <repoid> info upgrades [<package-name-spec>]

It lists packages in the repository that upgrade packages installed on the system.

dnf [options] repository-packages <repoid> install [<package-spec>]

It installs all the packages or specified packages that are available in the repository.

dnf [options] repository-packages <repoid> list all [<package-name-spec>]

It lists all the related packages.

dnf [options] repository-packages <repoid> list installed [<package-name-spec>]

It list installed packages from the repository.

dnf [options] repository-packages <repoid> list available [<package-name-spec>]

It lists packages available in the repository.

dnf [options] repository-packages <repoid> list extras [<package-name-specs>]

It lists packages installed from the repository that are not available in any repository.

dnf [options] repository-packages <repoid> list obsoletes [<package-name-specs>]

It lists obsolete packages that are available in the repository and are installed on the system.

dnf [options] repository-packages <repoid> list recent [<package-name-spec>]

It lists recently added packages in the repository.

dnf [options] repository-packages <repoid> list upgrades [<package-name-spec>]

It list packages in the repository that upgrade packages installed on the system.

dnf [options] repository-packages <repoid> move-to [<package-name-spec>]

It reinstalls all those packages that are available in the repository.

dnf [options] repository-packages <repoid> reinstall [<package-name-spec>]

It runs reinstall-old subcommand but if it fails, it runs move-to subcommand.

dnf [options] repository-packages <repoid> reinstall-old [<package-name-spec>]

It reinstalls all those packages that were installed from the repository and are available in the repository.

dnf [options] repository-packages <repoid> remove [<package-name-spec>]

It removes all packages installed from the repository along with any packages depending on the packages being removed. If clean_requirements_on_remove is enabled, it also removes any dependencies that are no longer needed by the system.

dnf [options] repository-packages <repoid> remove-or-distro-sync [<package-name-spec>]

It selects all packages installed from the repository and upgrades, downgrades or keep those of them that are available in another repositories to match the latest version available there and remove the others along with any packages depending on the packages being removed.

dnf [options] repository-packages <repoid> remove-or-reinstall [<package-name-spec>]

It selects all packages or specified packages installed from the repository and reinstalls those of them that are available in another repositories and remove the others along with any packages depending on the packages being removed.

dnf [options] repository-packages <repoid> upgrade [<package-name-spec>]

It updates all packages to the highest resolvable version available in the repository.

dnf [options] repository-packages <repoid> upgrade-to [<package-version-specs>]

It updates packages to the specified versions that are available in the repository.

18. Search Command

Search Command is a very much useful command which helps us able to find a package name from the metadata. It searches the metadata for the keyword which is case-insensitive and then tries to find the keyword in the description, package names and URLs and displays in sorted form with the most relevant result at the top.

$ sudo dnf search all xchat

19. Updateinfo Command

The updateinfo command displays information about the update advisories. Here, <output> is either list or summary or info according to the needed information, we can even add -v to info for detailed information. <availability> specifies installed if the advisories is about equal and older versions of installed packages or specifies available if the advisories is about the newer version of installed packages or specifies update if the advisories is about a newer versions of those installed packages for which a newer version is available or specifies all if advisories is about any versions of installed packages. The <spec> consists of  ID, type (bugfix, enhancement, security) or a package name of an advisory.

Summary

$ sudo dnf updateinfo summary all firefox

List

$ sudo dnf updateinfo list all firefox

Information

$ sudo  dnf updateinfo info all firefox

20. Upgrade Command

Upgrade command updates all the packages  to the highest version that is both resolvable and installed in the system. If a specific package is specified, the upgrade command updates that package to the highest available version and updates dependencies if necessary.

Upgrading all the Packages

$ sudo dnf upgrade

Upgrading Specific Packages

$ sudo dnf upgrade firefox

21. Upgrade-to Command

The upgrade-to command updates the packages to the specified version.

$ sudo dnf upgrade-to grub2-1:2.02-0.16.fc22

Enabling Automatic Package Updates

DNF has the option to automatic package update as there was in YUM named yum-updatesd. The Automatic Package Updates is handled by dnf-automatic in DNF. To get started, first of all we need to install dnf-automatic in our Linux System.

$ sudo dnf -y install dnf-automatic

So, there is one configuration file named /etc/dnf/automatic.conf that has pretty sensible default configurations. We can configure it according to our need. Here, I have enabled apply_updates = yes so that the updates will be installed when its available.

Now, we'll want to enable the timer of dnf-automatic and wanna start it so that it will run.

$ sudo systemctl enable dnf-automatic.timer
$ sudo systemctl start dnf-automatic.timer

We can check the job time and schedules of dnf-automatic by running the following command.

$ sudo systemctl list-timers *dnf*

Keeping Old Kernels with DNF

While upgrading kernels in DNF, the new kernel is installed as well as there is old kernel available in the system to choose incase there is corrupt or bug in the new installed kernel. And the old kernels are cleaned up when it reaches up to 3 old kernels by default. We can change the configuration on how many old kernels to keep by editing /etc/dnf/dnf.conf file using our favorite text editor.

$ sudo vi /etc/dnf/dnf.conf

Then, we'll need to find line with installonly_limit=3 . Here, we can change the value by replacing 3 with the number of old kernel we want to keep. If we want to keep all the kernels, we need to replace 3 with 0.

Conclusion

DNF aka Dandified YUM is really a stable and awesome fork of YUM. The commands are way too very easy to execute and work with ease. So, if you have any questions, comments, feedback please do write on the comment box below and let us know what stuffs needs to be added or improved.

Leave a Comment