DNF Command: Install, Update, Remove Packages in CentOS, Fedora, Red Hat

DNF is the default package manager for CentOS, Fedora, and Red Hat-based Linux systems. In this tutorial, we’ll go over how to find, install, update, and remove software packages using the dnf command.

List Installed Packages

To list all packages available and installed on your system, use:

dnf list

If you want to list only installed packages, add the installed option:

dnf list installed

Example: Filter installed packages to check if the httpd server is installed:

dnf list installed | grep httpd

Search for Packages to Install

While dnf list is great for seeing what's installed or available, sometimes you need to find packages based on a description or a broader term. For that, we use the dnf search command.

dnf search keyword

Examples:

dnf search "web server"
dnf search apache

Next, if we want more information on a package, we can use the dnf info command:

dnf info package_name

Example: dnf info httpd

Install Packages

Once you know the package name, you can install it using dnf install . For example, to install the Apache HTTP server, run:

sudo dnf install httpd

By default, DNF will ask for your confirmation before installing. To skip this, you can add the -y option:

sudo dnf install httpd -y

Preferred Command for Installing Local RPMs

dnf install is also the preferred command to install local .rpm packages, as DNF will automatically resolve all necessary dependencies.

sudo dnf install package_name.rpm

Update Packages

Check for available updates with:

dnf check-update

Upgrade a single package:

sudo dnf upgrade package-name

Or upgrade all packages:

sudo dnf upgrade

It's important to note that if you update all packages, and that includes the Linux kernel, you'll need to restart the system to load the new kernel, or the system will continue running on the old kernel.

Uninstall Packages

To remove a package, use:

sudo dnf remove package-name

After uninstalling a package, it's a good practice to run dnf autoremove. This command uninstalls any dependency packages that are no longer needed by any other installed software.

sudo dnf autoremove

Example: Uninstall Apache HTTP Server:

sudo dnf remove httpd
sudo dnf autoremove

Track Changes with DNF History

The dnf history is a powerful feature that lets you view a record of all DNF transactions. To see your history, just type dnf history.

dnf history

A particularly useful feature is the ability to undo a past transaction. You can do this with dnf history undo .

sudo dnf history undo ID

For example, if your last transaction was removing a package, undoing it would reinstall that package.

Caution: While dnf history undo is a powerful recovery tool, use it carefully, especially if other software or system configurations have changed since that original transaction, as reverting can lead to unexpected system issues or dependency conflicts.

Manage DNF Package Groups

DNF also allows you to manage package groups, which are collections of related software. There are two main types:

  • Regular groups are just a collection of packages.
  • Environment Groups consist of packages as well as other groups, designed to set up specific environments, like a desktop.

To see a list of all available and installed groups, run:

dnf group list

For more details on a specific group, use dnf group info group_name.

sudo dnf group info "Server with GUI"

To install a group, use dnf group install . For example, to install the "Server with GUI" group, run:

sudo dnf group install "Server with GUI"
  • By default, only mandatory packages will be installed. If you want optional packages and groups, use the --with-optional flag.
  • To remove a group, use dnf group remove "group_name".

Understand DNF Repositories

DNF repositories are the locations from where DNF downloads and installs packages. To see a list of all currently enabled repositories on your system, run:

dnf repolist

See all repos, enabled or disabled:

dnf repolist all

You can find configuration files for DNF repositories at the /etc/yum.repos.d/ directory.

A DNF repository inside a .repo file will look like this:

[baseos]
name=CentOS Stream $releasever - BaseOS
metalink=https://mirrors.centos.org/metalink?repo=centos-baseos-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial-SHA256
gpgcheck=1
enabled=1
  • A single file can contain multiple repositories.
  • Each section that starts with square brackets represents a different repository.
  • The ID of the repository is found inside these square brackets.
  • baseurl or metalink: Specifies the actual URL where packages are located.
  • enabled: Set to 1 to activate the repository, 0 to disable it.
  • gpgcheck: Set to 1 to perform a GPG signature check on packages for security.
  • gpgcheck: Set to 0 to skip GPG signature checks on packages.

Get Help with DNF

General Help:

dnf --help

For the full manual page:

man dnf

Command-Specific Help:

dnf help <command>

Examples:

dnf help install 
dnf help update

That’s a quick overview of DNF to help you manage packages on CentOS, Fedora, and Red Hat.