Home Package management DNF Command Examples For Beginners

DNF Command Examples For Beginners

Essential DNF Commands For Beginners

By sk
Published: Last Updated on 7.6K views

This tutorial explains what is DNF package manager and lists the useful DNF command examples for package management in Fedora, RHEL, and its clones like CentOS, CentOS Stream, AlmaLinux and Rocky Linux.

At the end of this guide, you will get a basic idea of how to use DNF package manager to search, install, update, upgrade and remove packages in RPM-based systems.

What Is DNF?

DNF, short for Dandified YUM, is the next generation version of YUM and it has been the default package manager since Fedora 22.

DNF is intended to be the replacement for YUM in RPM-based systems such as Fedora, RHEL, and its clones such as CentOS, AlmaLinux and Rocky Linux.

Compared to YUM, DNF has some significant advantages, such as support for multiple repositories, package group support, faster dependency resolving speed and consumes less memory, runs in both Python 2 and Python 3, solid API documentation, compatibility with YUM and many.

DNF Command Examples For Beginners

As of Fedora 22, yum has been replaced with DNF, so you don't need to install it if you're on Fedora. On CentOS 7 and RHEL 7, you can install it as described in the tutorial given below.

DNF usage is very similar to YUM. All commands provided here were tested on CentOS 7 minimal server edition with root user.

1. Display DNF Help

To list all main commands, plugin commands and options, run:

# dnf help

To get help about a specific command, for example 'install', run:

# dnf help install

To know about the currently installed DNF version, run:

# dnf --version

2. Managing Packages

This section explains installing, updating, listing, searching, downloading, reinstalling and removing packages.

2.1. Getting Package Information

To get the details of any installed or non-installed package, run:

# dnf info vim

2.2. Installing Packages

To install new package, for example vim, run:

# dnf install vim

We can also install multiple packages like below.

# dnf install vim nano emacs

You need to type "y" to continue the installation. If you don't want to do that each time, you can include the option "y" in the commands for the auto-confirmation.

# dnf install vim nano emacs -y

This will not wait for the user confirmation. Instead, it will take the user confirmation as Yes and simply install all packages automatically.

If you've already downloaded a .rpm file, you can install it using command:

$ sudo dnf localinstall code-1.74.2-1671533504.el7.x86_64.rpm

2.3. Listing Packages

To list all available and installed packages:

# dnf list

To list all installed packages only, we use:

# dnf list installed

To list the available packages only:

# dnf list available

2.4. Updating Packages

To check for all available updates, do:

# dnf check-update

Or,

# dnf list updates

To update a single package (Eg. bash), run:

# dnf update bash

To view if there is any update to an installed package, run:

# dnf list yum.noarch 
Using metadata from Sat Feb 10 14:47:22 2018
Installed Packages
yum.noarch 3.4.3-150.el7.centos @System
Available Packages
yum.noarch 3.4.3-154.el7.centos.1 update

This command will display the currently installed version and the available update from the repository. In the above output, @system specifies the installed version and update specifies the available update from the repository.

To update multiple packages:

# dnf update package1 package2

Pass "y" option for auto confirmation.

# dnf update package1 package2 -y

To update/upgrade all packages, run:

# dnf update
# dnf upgrade

To synchronize installed packages to the latest available versions:

# dnf distro-sync

2.5. Searching Packages

To search a package from the repository, run:

# dnf search vim

This command will list all packages that matches the word "vim".

2.6. Downloading Packages

Sometimes you might want to download a package, but don't want to install it right now. If so, run:

# dnf download vim

This command will download vim-enhanced-xx.xx.x.rpm package, but won't install it.

To install the downloaded package, run:

# dnf install vim-enhanced-7.4.160-2.el7.x86_64.rpm

Or,

# dnf localinstall vim-enhanced-7.4.160-2.el7.x86_64.rpm

All other required dependencies will be downloaded and installed from the repository.

2.7. Downgrading Packages

To downgrade a package which you previously updated, run:

# dnf downgrade bash

2.8. Reinstalling Packages

Having any issues with an already installed package? No worries! You can reinstall it any time.

To reinstall a package, use:

# dnf reinstall vim

2.9. Removing Packages

To remove a package (Eg. vim) from the system, run:

# dnf remove vim

You can also use "erase" option like below.

# dnf erase vim

3. Package Groups

A package group is a collection of packages that serve a common purpose, for instance "Development Tools" or "Web server". When you install a package group, all dependent packages will be downloaded and installed. Installing groups will save you considerable amount of time.

3.1. Installing Package Groups

To list all available package groups, run:

# dnf grouplist

To install a package group, for example "Development Tools", run:

# dnf groupinstall 'Development Tools'

This command will install all packages required to setup a development environment.

3.2. Updating Package Groups

To Update a package group, run:

# dnf groupupdate 'Development Tools'

3.3. Removing Package Groups

To remove a package group, run:

# dnf groupremove 'Development Tools'

4. Working with Repositories

4.1. Listing installed Repositories

By default, all repositories will be added under /etc/yum.repos.d/ directory in CentOS/RHEL. You can list installed repositories using command:

# dnf repolist

The above command will only display the enabled repositories. To display all repositories ( both enabled and disabled), run:

# dnf repolist all

4.2. Adding Repositories

You can add a repository by manually creating a package.repo file under /etc/yum.repos.d/ directory. However, DNF makes it even easier.

To add a new repository, run:

# dnf config-manager --add-repo repository_url

Example:

# dnf config-manager --add-repo http://springdale.math.ias.edu/data/puias/unsupported/7/x86_64/

To view the newly added repository, we do:

# dnf repolist
created by dnf config-manager from http://springdale.math.ias.edu/data/puias/unsupported/7/x86_64/ 115 kB/s | 3.5 MB 00:31 
Using metadata from Sat Feb 10 14:47:22 2018
repo id repo name status
base CentOS-7 - Base 9,591
epel Extra Packages for Enterprise Linux 7 - x86_64 12,278
extras CentOS-7 - Extras 388
springdale.math.ias.edu_data_puias_unsupported_7_x86_64_ created by dnf config-manager from http://springdale.math.ias.edu/data/puias/unsupporte 2,760
updates CentOS-7 - Updates

To view the .repo file only:

# cat /etc/yum.repos.d/springdale.math.ias.edu_data_puias_unsupported_7_x86_64_.repo 
[springdale.math.ias.edu_data_puias_unsupported_7_x86_64_]
name=created by dnf config-manager from http://springdale.math.ias.edu/data/puias/unsupported/7/x86_64/
baseurl=http://springdale.math.ias.edu/data/puias/unsupported/7/x86_64/
enabled=1

4.3. Enabling Repositories

To enable a repository, use:

# dnf config-manager --set-enabled <repo-id>

Example:

# dnf config-manager --set-enabled epel

We can verify by running the following command to see the enabled repositories only.

# dnf repolist

Also, we can combine the enable and install options together like below.

# dnf config-manager --set-enabled epel install zsh

The above command enables the EPEL repository and installs zsh package.

4.4. Disabling Repositories

To disable a repository, for example EPEL, we use:

# dnf config-manager --set-disabled epel

We can also combine enable and disable options together like below.

# dnf --enablerepo=epel --disablerepo=updates

The above command will enable EPEL repository and disable updates repository.

5. Finding Which Provides Something

DNF allows you to find which package provides a specific command or file. If you don't remember exactly which package provides a particular command you wanted to use, DNF can help you.

5.1. Find Which Package Provides A Specific Command

I miss the good old "ifconfig" command. This command is deprecated and not available in the recent versions. To find which package provides this gem, run:

# dnf provides */ifconfig

Sample output:

Using metadata from Thu Feb 8 15:52:17 2018
net-tools-2.0-0.22.20131004git.el7.x86_64 : Basic networking tools
Repo : base

Ahh, yes! net-tools package provides this command. So you can install this package to use ifconfig command.

5.2. Find Which Package Provides A File

Let us you want to find what package provides /bin/ls file. To do so, run:

# dnf provides */bin/ls
Using metadata from Thu Jun 30 12:51:16 2016
coreutils-8.22-18.el7.x86_64 : A set of basic GNU tools commonly used in shell scripts
Repo : @System

coreutils-8.22-18.el7.x86_64 : A set of basic GNU tools commonly used in shell scripts
Repo : base

As you see in the above output, coreutils package provides /bin/ls file.

6. Working with DNF history

Using "dnf history" command, we can view all the actions which we performed with DNF command.

# dnf history
Using metadata from Sat Feb 10 14:47:22 2018
ID | Command line | Date a | Action | Altere
-------------------------------------------------------------------------------
 2 | update bash | 2018-02-10 14:55 | Update | 1 
 1 | install vim | 2018-02-10 14:51 | E, I | 33 EE

As you see in the above output,there were two transactions. I installed a package named "vim" and updated the package "bash". The newest transactions will appear on the top.

To view the details of a specific transaction, use its ID like below.

# dnf history list 2

We can also view the complete details of a specific transaction like below.

# dnf history info 2
Using metadata from Sat Feb 10 14:47:22 2018
Transaction ID : 2
Begin time : Sat Feb 10 14:55:38 2018
Begin rpmdb : 360:fa7bea798a10e850b2bf800182b377443b481652
End time : 14:55:39 2018 (1 seconds)
End rpmdb : 360:dc9f6bb33b00fe1d74dd205586c0ff8c4674f8f7
User : root <root>
Return-Code : Success
Command Line : update bash
Transaction performed with:
 Installed dnf-0.6.4-2.sdl7.noarch (unknown)
 Installed rpm-4.11.3-21.el7.x86_64 (unknown)
Packages Altered:
 Upgraded bash-4.2.46-21.el7_3.x86_64 (unknown)
 Upgrade 4.2.46-29.el7_4.x86_64 @updates

As you see, the transaction with ID of 2 was used to update the package named "bash". We can undo this transaction at any time using command:

# dnf history undo 2

To redo the previous 'undo' transaction, just run:

# dnf history redo 2

7. Building DNF metadata Cache

DNF metadata cache will be automatically created as we install, update packages. So we don't need to manually build it. However, DNF has a built-in command to build the cache to make the future transactions quicker.

To create metadata cache, run:

# dnf makecache
Extra Packages for Enterprise Linux 7 - x86_64 155 kB/s | 15 MB 01:35 
created by dnf config-manager from http://springdale.math.ias.edu/data/puias/unsupported/7/x86_64/ 136 kB/s | 3.5 MB 00:26 
CentOS-7 - Base 195 kB/s | 9.5 MB 00:49 
CentOS-7 - Updates 240 kB/s | 7.9 MB 00:33 
CentOS-7 - Extras 84 kB/s | 820 kB 00:09 
Metadata cache created.

8. Cleaning up

To remove unwanted/orphaned packages, run:

# dnf autoremove

To remove all cached packages, run:

# dnf clean all

Instead of clean the cache completely, you may want to do individual cleaning of metadata, dbcache, packages, plugins etc.

To remove cache files generated from the repository metadata:

# dnf clean dbcache

This forces DNF to regenerate the cache files the next time it is run.

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

# dnf clean expire-cache

DNF will re-validate the cache for each repo the next time it is used.

To remove repository metadata:

# dnf clean metadata

Using this option will make DNF download all the metadata the next time it is run.

To remove all cached packages from the system:

# dnf clean packages

The following command tells all enabled plugins to eliminate their cached data.

# dnf clean plugins

These are just enough to get an basic usage of DNF on Fedora, RHEL and CentOS. For more details, refer the man pages.

$ man dnf

Conclusion

In this DNF command user manual, we learned essential DNF commands with examples for RPM package management in Linux.

Resource:

You May Also Like

1 comment

Praveen Pious May 30, 2019 - 9:24 am

Thanks for this curated info

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More