dnf Package Manager

dnf cheatsheet — install, update, remove packages on Fedora/RHEL/CentOS. dnf install, dnf upgrade, dnf remove, dnf search, dnf history. Full RPM package management.

7 min read

What it is

dnf is a high-level package manager for RPM-based Linux distributions (like Fedora, CentOS Stream, RHEL) used for installing, updating, removing, and managing software packages.

Installation

Fedora/CentOS Stream/RHEL (dnf is usually pre-installed):

sudo dnf install <package-name>

Older Fedora versions (may need dnf-command(1) package):

sudo dnf install dnf-command(1)

Core Concepts

  • Package: A file containing application code, libraries, configuration files, and metadata needed to install and run software. Packages are typically in .rpm format.
  • Repository: A collection of software packages accessible over the network or from local media. dnf reads metadata from repositories to know what packages are available and where to find them.
  • Group: A logical collection of packages that provide a specific functionality or set of tools (e.g., "Development Tools", "Web Server").
  • Dependency: A requirement that one package has on another package. dnf automatically resolves and installs dependencies.
  • Transaction: An operation performed by dnf (installing, updating, removing). dnf performs transactions atomically, meaning either the whole operation succeeds or it fails and the system is rolled back to its previous state.

Commands / Usage

Searching and Information

  • Search for packages:

    dnf search httpd
    

    Find packages related to "httpd".

  • List available packages:

    dnf list available
    

    Show all packages available in enabled repositories.

  • List installed packages:

    dnf list installed
    

    Show all packages currently installed on the system.

  • List all packages (installed and available):

    dnf list all
    

    Show all packages known to dnf (installed and available).

  • Show detailed information about a package:

    dnf info httpd
    

    Display description, version, size, dependencies, and other details for the "httpd" package.

  • List files owned by an installed package:

    dnf repoquery --list httpd
    

    Show all files that are part of the installed "httpd" package.

  • Find which package owns a specific file:

    dnf provides /usr/sbin/httpd
    

    Identify the package that contains the file /usr/sbin/httpd.

Installing and Removing Packages

  • Install a package:

    sudo dnf install nano
    

    Install the "nano" text editor.

  • Install multiple packages:

    sudo dnf install htop tree
    

    Install both "htop" and "tree".

  • Install a specific version of a package:

    sudo dnf install httpd-2.4.53-1.fc36
    

    Install version 2.4.53-1.fc36 of the "httpd" package.

  • Remove a package:

    sudo dnf remove nano
    

    Uninstall the "nano" text editor.

  • Remove a package and its dependencies that are no longer needed:

    sudo dnf autoremove
    

    Clean up orphaned dependencies.

  • Erase a package without removing configuration files:

    sudo dnf erase nano
    

    Same as remove, but generally prefers to keep configuration files.

Updating Packages

  • Update all installed packages:

    sudo dnf upgrade
    

    Upgrade all packages on the system to their latest available versions.

  • Update a specific package:

    sudo dnf upgrade httpd
    

    Upgrade only the "httpd" package.

  • Check for available updates:

    dnf check-update
    

    List packages that have available updates without installing them.

Package Groups

  • List available package groups:

    dnf group list
    

    Show all available software groups.

  • List installed package groups:

    dnf group list --installed
    

    Show only the installed software groups.

  • Install a package group:

    sudo dnf group install "Development Tools"
    

    Install all packages in the "Development Tools" group.

  • Remove a package group:

    sudo dnf group remove "Web Server"
    

    Uninstall all packages in the "Web Server" group.

  • List packages within a group:

    dnf group info "Development Tools"
    

    Show details and list of packages for the "Development Tools" group.

Repository Management

  • List enabled repositories:

    dnf repolist
    

    Show repositories that dnf is currently using.

  • List all repositories (enabled and disabled):

    dnf repolist all
    

    Show all configured repositories.

  • Enable a disabled repository:

    sudo dnf config-manager --set-enabled fedora-cisco-openh264
    

    Enable the repository named "fedora-cisco-openh264".

  • Disable a repository:

    sudo dnf config-manager --set-disabled fedora-cisco-openh264
    

    Disable the repository named "fedora-cisco-openh264".

  • Add a new repository from a file:

    sudo dnf config-manager --add-repo /etc/yum.repos.d/myrepo.repo
    

    Add a repository defined in /etc/yum.repos.d/myrepo.repo.

  • Add a new repository from a URL:

    sudo dnf config-manager --add-repo http://example.com/myrepo.repo
    

    Add a repository from a remote URL.

History and Rollback

  • Show transaction history:

    dnf history
    

    List past dnf operations.

  • Show details of a specific transaction:

    dnf history info 5
    

    Display the packages installed/removed in transaction ID 5.

  • Undo a transaction:

    sudo dnf history undo 5
    

    Revert all changes made by transaction ID 5.

  • Redo a transaction:

    sudo dnf history redo 5
    

    Re-apply changes from transaction ID 5 (useful if undo failed or was partial).

  • Rollback to a specific transaction ID:

    sudo dnf history rollback 5
    

    Undo all transactions after transaction ID 5, effectively reverting the system to the state after transaction 5 completed.

Other Useful Commands

  • Clean package cache:

    sudo dnf clean all
    

    Remove cached package files and metadata.

  • Rebuild RPM database:

    sudo dnf distro-sync --refresh
    

    Synchronize installed packages to the latest available versions, potentially downgrading or removing packages if necessary to match repository metadata. The --refresh ensures metadata is updated first.

  • Install local RPM file:

    sudo dnf install /path/to/package.rpm
    

    Install a package from a local .rpm file, automatically resolving dependencies from repositories.

  • Downgrade a package:

    sudo dnf downgrade httpd
    

    Install an older version of the "httpd" package.

  • Mark a package as installed without installing it:

    sudo dnf mark install nano
    

    Tell dnf that "nano" is installed, useful for manual package management or fixing database inconsistencies.

  • Mark a package as not installed:

    sudo dnf mark exclude nano
    

    Prevent dnf from automatically installing or updating "nano".

  • Exclude packages from updates:

    sudo dnf versionlock add nano
    

    Prevent "nano" from being upgraded. Use dnf versionlock list to see locked packages and dnf versionlock delete nano to remove the lock.

Common Patterns

  • Install essential development tools:

    sudo dnf group install "Development Tools" && sudo dnf group install "C Development Tools and Libraries"
    

    A common setup for compiling software.

  • Update the system and clean up old packages:

    sudo dnf upgrade --refresh && sudo dnf autoremove
    

    Ensures the system is up-to-date and removes orphaned dependencies.

  • Find and install a package that provides a command:

    # If you know 'htop' is a command but don't know the package name
    dnf provides '*/htop'
    # Then install it
    sudo dnf install htop
    
  • Install a package and then immediately check its version:

    sudo dnf install htop && htop --version
    
  • Reinstall a package:

    sudo dnf reinstall httpd
    

    Useful for fixing corrupted installations.

  • List all packages installed from a specific repository:

    dnf list installed | grep '@fedora'
    

    Shows all installed packages that originated from the 'fedora' repository.

Gotchas

  • sudo is required: Most dnf operations that modify the system (install, remove, upgrade) require root privileges.
  • Transaction limitations: dnf performs operations atomically. If a transaction fails mid-way, it will attempt to roll back. This is generally reliable but complex failures can sometimes leave the system in an inconsistent state.
  • Repository ordering: The order in which repositories are listed in .repo files can sometimes influence dependency resolution if the same package is available in multiple repositories with different versions. dnf usually prioritizes the repository listed first.
  • dnf history undo vs rollback: undo reverses a single transaction. rollback undoes a range of transactions. Be careful when using rollback as it can revert many changes.
  • Configuration files: When removing packages, dnf might leave configuration files behind. This is usually intentional to preserve user settings, but it means a clean install might require manual cleanup of these files.
  • Network issues: dnf relies heavily on network access to repositories. Intermittent network problems can cause installation or update failures. Ensure stable connectivity or consider using local repositories.
  • dnf vs yum: While dnf is the successor to yum and aims for backward compatibility, some older scripts or very specific yum behaviors might not translate perfectly. dnf is generally faster and has better dependency resolution.