Saving a server at 2 AM: A detailed guide to DNF Package Manager on RHEL/Rocky Linux

Quick Start: At 2 AM, whatever you need, DNF has it!

Let me tell you a story: there was a time our company’s production server reported an error after a security patch. It was 2 AM, and my heart was pounding. The first thing I thought of was to quickly identify and resolve the software package causing the error. And DNF was the savior. If you’re in a similar situation, or just want to get familiar with DNF, these are the essential things you need to know. Don’t let yourself struggle when an incident strikes!

DNF (Dandified YUM) is the default package manager on RPM-based operating systems like RHEL 8+, CentOS Stream, Rocky Linux, and AlmaLinux. It is the successor to YUM, offering superior performance, more stable dependency resolution, and many more useful features.

Basic DNF commands to help you survive the night:

# 1. Update the system (always the top priority)
sudo dnf update

# 2. Install a software package (e.g., nginx)
sudo dnf install nginx

# 3. Remove a software package
sudo dnf remove nginx

# 4. Search for a package (when you don't remember the exact name)
dnf search "php-fpm"

# 5. Find the package that provides a specific file (extremely useful when debugging "command not found")
dnf provides /usr/bin/htpasswd

With just these 5 commands, you can perform most basic software management tasks. Mastering them will make you much more confident when working with servers.

Detailed Explanation: How DNF differs from YUM and how it works?

If you’ve ever worked with CentOS 7 or older RHEL versions, you’re probably very familiar with YUM. When switching to RHEL 8+ and its successors like Rocky Linux or AlmaLinux, DNF has become a new alternative. It’s not just a new name, but also a big technological leap.

DNF vs YUM: Why DNF?

  • New backend technology: DNF uses the libsolv library from openSUSE, which helps resolve dependencies much faster and more reliably than YUM. This is an extremely important factor, especially when you need to update complex software packages.
  • Module Management: DNF introduces the concept of modules and streams, allowing multiple versions of the same software (e.g., PHP 7.4 and PHP 8.0) to be installed on the same system without conflict. YUM does not have this feature.
  • Stable API: DNF has a well-defined API, making it easier for third-party tools to integrate.

Essentially, you can consider DNF to be a better and smarter YUM.

Repository Management: Software Providers

Repositories are where DNF searches for and downloads software packages. Repository configuration files are usually located in the /etc/yum.repos.d/ directory. Each .repo file defines one or more repositories.

# List active repositories on your system
dnf repolist

# View details of all enabled and disabled repositories
dnf repolist all

To add a new repository (e.g., EPEL – Extra Packages for Enterprise Linux, a very useful repo for packages not available in the main repo):

# Install the package containing the EPEL repo configuration
sudo dnf install epel-release

# Then, check the repo list again
dnf repolist

Sometimes, you need to enable or disable a repo manually. There are two ways: edit the corresponding .repo file or use the command:

# Enable repo
sudo dnf config-manager --set-enabled <repo_id>

# Disable repo
sudo dnf config-manager --set-disabled <repo_id>

Searching for package and group information

In addition to dnf search, there are other commands to learn more about software packages:

# View detailed information about a package (version, size, description, etc.)
dnf info nginx

# List all available or installed packages
dnf list

dnf list installed
dnf list available

# DNF also supports installing package groups, which is very convenient for specific tasks
dnf group list

dnf group install "Development Tools"

System updates and management: Safety first

# Only check for updates, do not install
dnf check-update

# Update all packages to the latest version
sudo dnf upgrade

One feature I find extremely valuable is dnf history. It records every action you’ve performed with DNF – it has saved me countless times!

# View DNF transaction history
dnf history

# View details of a specific transaction (e.g., transaction ID 50)
dnf history info 50

# Undo a transaction (if something went wrong)
sudo dnf history undo 50

# Redo an undone operation
sudo dnf history redo 50

Honestly, once at 2 AM, the server crashed after I updated a package. Fortunately, dnf history undo helped me revert the changes. Otherwise, I would have lost sleep for a whole week.

Advanced: When DNF is more than just installing and removing

Module and Stream Management: Flexible Power

As mentioned, module management is a highlight of DNF. It allows you to install different software versions without worrying about conflicts. For example, you need PHP 7.4 for an old application and PHP 8.0 for a new one.

# List all available modules and streams
dnf module list

# Enable PHP 8.0 stream and install
sudo dnf module enable php:8.0
sudo dnf install php php-fpm

# If you want to switch to PHP 7.4 (you need to reset the current stream first)
sudo dnf module reset php
sudo dnf module enable php:7.4
sudo dnf install php php-fpm

This feature is extremely important in development and production environments, where you face various software version requirements.

Using DNF offline: Preparing for difficult situations

There are times when your server does not have an internet connection (e.g., server on a local network, air-gapped environment). In these cases, downloading packages and installing them offline becomes essential.

# Download the nginx package and all its dependencies to the current directory
sudo dnf download --resolve --destdir=./ nginx

# Then, you can copy these .rpm packages to the offline server and install them manually
sudo rpm -ivh *.rpm

Optimizing DNF: Increasing Speed and Efficiency

You can fine-tune DNF’s behavior through the configuration file /etc/dnf/dnf.conf:

# Example of some useful customizations in dnf.conf
[main]
kecache=True  # Retain downloaded packages in cache, allowing faster reinstallation
max_parallel_downloads=10 # Download 10 packages concurrently, speeding up updates
deltarpm=True # Only download changed parts of packages, saving bandwidth

These small adjustments can make a big difference in performance when you work with DNF frequently.

Hard-earned experience: Migrating CentOS 7 to AlmaLinux and DNF’s role

My company still has a few servers running CentOS 7. I personally handled the migration of these to AlmaLinux. During that transition, DNF played a crucial role. After changing the operating system, I needed to reinstall many services and software. Thanks to its strong dependency resolution capabilities and flexible module management, DNF made this process much smoother.

I remember some old services on CentOS 7 had complex dependencies. However, when switching to AlmaLinux and using DNF, everything became much easier to control. I could easily reinstall the desired PHP and MySQL versions without having to deal with tangled dependency errors like before with YUM. DNF truly reduced the burden in an already stressful process.

Practical Tips: Make DNF your powerful ally

As any sysadmin knows, theory and practice are very different. Here are some tips I’ve learned, helping you use DNF more effectively:

  1. Always read the output carefully: DNF often lists packages that will be installed, updated, or removed. Never type y and Enter unconsciously. I almost mistakenly removed an important dependency just because I didn’t read the list of packages to be removed carefully.
  2. Clean cache regularly: DNF’s cache is located at /var/cache/dnf. If not cleaned, it can take up quite a bit of disk space.
  3. sudo dnf clean all
    
  4. Understand dependencies: DNF automatically resolves dependencies. However, you still need to understand which packages depend on which, especially when installing software from external sources or when encountering errors. The commands dnf provides and dnf repoquery --requires (requires dnf-utils to be installed) will be very helpful.
  5. # Install dnf-utils to get repoquery
    sudo dnf install dnf-utils
    
    # View dependencies of a package
    dnf repoquery --requires nginx
    
  6. Backup is gold: Before performing any major system update (e.g., from Rocky 8 to Rocky 9), always back up your server or create a virtual machine snapshot. DNF can help you rollback packages, but it cannot save you if the entire operating system encounters a serious problem.
  7. When DNF encounters an error:
    • Check network connection: Sometimes the error is simply an inability to connect to the repo.
    • Check repo configuration: Make sure your .repo files don’t have syntax errors or point to incorrect paths.
    • Clear cache and retry: Sometimes the cache is corrupted.
    • Search for the error on Google/Stack Overflow: Many DNF errors have been encountered and resolved by others.
  8. dnf history is your best friend: I’ve emphasized it before, but it bears repeating: this is your savior when you need to debug or revert to a previous state. Never forget it!

DNF is powerful and flexible. Hopefully, with these shares, you will be more confident when working with DNF on RHEL/Rocky Linux, no longer feeling “heart pounding, legs trembling” when the server reports an error in the middle of the night.

Share: