1. Introduction to DNF and RPM-based Package Management

The RPM Package Manager (rpm): At the core of package management in distributions like Fedora, CentOS, RHEL, and others lies RPM (originally Red Hat Package Manager). RPM is both a package format (.rpm files) and a low-level command-line tool used to install, uninstall, verify, query, and update software packages. While you can use the rpm command directly for certain tasks, it doesn't automatically handle dependencies.

What is DNF? DNF (Dandified YUM) is the modern, high-level package manager for RPM-based Linux distributions. It effectively replaced YUM (Yellowdog Updater, Modified). DNF provides a user-friendly command-line interface that handles tasks like fetching packages from software repositories, automatically resolving and installing all necessary dependencies, and updating and removing packages. Key improvements of DNF over YUM include better performance and more robust dependency resolution.

Distributions Using DNF: Fedora, Red Hat Enterprise Linux (RHEL) version 8 and later, CentOS Stream (version 8 and later), AlmaLinux, Rocky Linux, and Oracle Linux (version 8 and later).

Repositories: DNF gets its software from repositories. Configuration files are typically stored in .repo files located in the /etc/yum.repos.d/ directory. Common repositories include official distribution repos, EPEL (Extra Packages for Enterprise Linux), and RPM Fusion (for Fedora).

2. Updating System and Package Lists

sudo dnf check-update

Action: This command queries all your enabled repositories and checks if newer versions of your installed packages are available. It does not download or install anything; it only reports what updates exist.

Why: To see a list of available updates before committing to an upgrade.

Example:

sudo dnf check-update
sudo dnf upgrade

Action: This command downloads and installs the newest versions of all currently installed packages for which updates are available from the enabled repositories. If a package update requires an update to the system itself (like a kernel update), dnf upgrade will handle that too.

Why: This is the standard command to keep your system's software up-to-date with security patches, bug fixes, and new features.

Example:

sudo dnf upgrade

DNF will list the packages to be upgraded, any new dependencies to be installed, and the total download size, then ask for confirmation (Is this ok? [y/N]:).

sudo dnf upgrade --refresh

Action: This command behaves like sudo dnf upgrade but first forces DNF to refresh its local metadata cache from all enabled repositories.

Why: Ensures that DNF is checking against the absolute latest information available in the repositories before performing the upgrade. Useful if you suspect your local cache might be stale.

Example:

sudo dnf upgrade --refresh
sudo dnf system-upgrade

Action: This is not a standalone DNF command but rather a process typically involving the dnf-plugin-system-upgrade package. It's used to upgrade the entire operating system to a new major release (e.g., from Fedora 39 to Fedora 40).

Why: To move from one major version of your Linux distribution to the next.

Note: This is an advanced topic and involves several steps. Users should always consult the official documentation for their distribution before attempting a system version upgrade.

Example (conceptual steps for Fedora):

sudo dnf install dnf-plugin-system-upgrade
sudo dnf system-upgrade download --releasever=<new_version_number>
sudo dnf system-upgrade reboot

3. Searching for Packages

dnf search <search_term>
dnf info <package_name>

Action: Displays detailed information about a specific package. This includes its name, version, release, architecture, installed size, source repository, license, summary, and a longer description. It will also indicate if the package is already installed.

Why: Useful for verifying package details before installation, checking its version, or understanding its purpose.

Example:

dnf info gcc
dnf info htop
dnf provides <file_path_or_feature>

Action: Determines which package provides a specific file (e.g., /usr/bin/somecommand) or a particular feature/capability. Also aliased as dnf whatprovides. You can use wildcards.

Why: Very useful if you have a file path or know a specific capability and need to find out which package to install to get it.

Example:

dnf provides /usr/sbin/sshd
dnf whatprovides "*/libcrypto.so.3"
dnf provides "perl(strict)"

4. Installing Packages

sudo dnf install <package_name>

Action: Downloads the specified package (and all its required dependencies) from the repositories and installs it on your system.

Why: This is the primary command for adding new software.

Example:

sudo dnf install firewalld  # Installs the firewalld firewall service
sudo dnf install nano       # Installs the nano text editor

Installing Multiple Packages: You can install several packages at once:

sudo dnf install tmux htop ncdu

DNF will show you a summary of packages to be installed, their dependencies, download size, and installed size, then ask for confirmation.

sudo dnf reinstall <package_name>

Action: Reinstalls a package that is already present on your system.

Why: Can be useful if a package's installation has become corrupted, some of its files are missing, or you want to reset it to its initial installed state.

Example:

sudo dnf reinstall coreutils

5. Removing Packages

sudo dnf remove <package_name>

Action: Uninstalls the specified package. DNF will also offer to remove any dependencies that were installed solely for that package and are no longer needed by any other package on the system.

Why: To remove software you no longer require, helping to keep your system clean.

Example:

sudo dnf remove anaconda

DNF will list the packages to be removed (including dependent packages that would be auto-removed) and ask for confirmation.

sudo dnf autoremove

Action: Removes "leaf" packages from the system that were originally installed as dependencies of explicitly user-installed packages but are no longer required by any such package. This typically happens after you remove a package that had unique dependencies.

Why: Frees up disk space and keeps the system tidy by removing unnecessary packages. It's good practice to run this after removing other packages.

Example:

sudo dnf autoremove

6. Managing Package Cache and History

sudo dnf clean packages

Action: Removes cached package files (.rpm files) from the system. These are the files downloaded during installation or upgrades.

Why: Frees up disk space. If you need to reinstall a package, DNF will simply re-download it.

Example:

sudo dnf clean packages
sudo dnf clean metadata

Action: Removes repository metadata (files describing the packages available in each repository). This metadata will be downloaded again and regenerated the next time DNF needs it.

Why: Can help resolve issues if you suspect the local metadata cache has become corrupted or inconsistent.

Example:

sudo dnf clean metadata
sudo dnf clean all

Action: A more comprehensive cleanup. It removes all cached files from enabled repositories, including downloaded packages and repository metadata.

Why: Thoroughly clears the DNF cache.

Example:

sudo dnf clean all
dnf history list

Action: Displays a list of past DNF transactions, showing an ID, the command line used, the date and time, the action taken (e.g., Install, Upgrade, Remove), and how many packages were affected.

Why: Allows you to review the history of package management operations performed on your system.

Example:

dnf history list
dnf history list all # Shows all history entries
dnf history info <transaction_id_or_package_name>

Action: Shows detailed information about a specific transaction (using its ID from dnf history list) or all transactions involving a specific package.

Example:

dnf history info 15
dnf history info httpd
sudo dnf history undo <transaction_id>

Action: Attempts to revert (undo) the changes made by a specific transaction ID.

Why: Can be a lifesaver if a recent installation or upgrade caused problems. DNF will try to return the system to the state it was in before that transaction. Use with caution, as not all operations can be perfectly undone.

Example:

sudo dnf history undo 23
sudo dnf history redo <transaction_id>

Action: Attempts to repeat a previously undone transaction.

Why: If you undid a transaction and then realized you wanted it after all.

Example:

sudo dnf history redo 23

7. Listing Packages and Group Operations

dnf list installed

Action: Lists all packages currently installed on the system, along with their version, release, and the repository they came from.

Why: To see what software is present on your system. The output can be long, so piping to less or grep is common.

Example:

dnf list installed
dnf list installed | grep kernel
dnf list available

Action: Lists all packages available for installation from your currently enabled repositories.

Why: To browse or search for software that you can install.

Example:

dnf list available | less
dnf list available | grep "office suite"
dnf group list

Action: Lists available and installed package groups. Package groups are collections of related packages that serve a common purpose (e.g., "Development Tools," "Web Server," "GNOME Desktop Environment").

Why: Provides an easy way to install or remove a whole suite of related software with a single command.

Example:

dnf group list
dnf group list --installed
dnf group list --available
sudo dnf groupinstall "<group_name>"

Action: Installs all the packages that are part of the specified group. Make sure to quote the group name if it contains spaces.

Why: Convenient for setting up a system for a specific role, like development or server hosting.

Example:

sudo dnf groupinstall "Development Tools"
sudo dnf groupinstall "Audio Production"
sudo dnf groupremove "<group_name>"

Action: Removes the packages that were installed as part of the specified group. It might not remove every single package if some are also dependencies of other explicitly installed packages or groups.

Why: To uninstall a suite of related software.

Example:

sudo dnf groupremove "Audio Production"
dnf groupinfo "<group_name>"

Action: Shows detailed information about a package group, including its description and a list of mandatory, default, and optional packages within that group.

Why: To understand what a group contains before installing or removing it.

Example:

dnf groupinfo "Development Tools"

8. Tips and Common Practices

  • Regularly update your system with sudo dnf upgrade.
  • Use sudo dnf autoremove to clean up unneeded dependencies.
  • Pay attention to the output of DNF commands, as it will tell you what changes are being made.
  • If you encounter issues, sudo dnf clean all and then retrying the operation (e.g., sudo dnf upgrade --refresh) can sometimes help.
  • Enabling additional repositories like EPEL (for RHEL-based systems) or RPM Fusion (for Fedora, for multimedia codecs and proprietary software) can greatly expand available software. This usually involves installing a specific .rpm package that sets up the repository configuration. Always obtain these from trusted, official sources.
  • For scripting, you can use the -y option to automatically answer "yes" to DNF prompts (e.g., sudo dnf -y install cowsay). Use this with caution as it bypasses your review of the changes.

9. Conclusion

DNF is a robust and user-friendly package manager that forms the backbone of software management on modern Fedora and RHEL-based Linux distributions. Mastering its commands will allow you to efficiently install, update, and maintain the software on your system. As with any powerful tool, understanding its options and practicing its use will lead to a smoother and more secure Linux experience.

Copied!