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
.rpmformat. - Repository: A collection of software packages accessible over the network or from local media.
dnfreads 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.
dnfautomatically resolves and installs dependencies. - Transaction: An operation performed by
dnf(installing, updating, removing).dnfperforms 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 httpdFind packages related to "httpd".
-
List available packages:
dnf list availableShow all packages available in enabled repositories.
-
List installed packages:
dnf list installedShow all packages currently installed on the system.
-
List all packages (installed and available):
dnf list allShow all packages known to
dnf(installed and available). -
Show detailed information about a package:
dnf info httpdDisplay description, version, size, dependencies, and other details for the "httpd" package.
-
List files owned by an installed package:
dnf repoquery --list httpdShow all files that are part of the installed "httpd" package.
-
Find which package owns a specific file:
dnf provides /usr/sbin/httpdIdentify the package that contains the file
/usr/sbin/httpd.
Installing and Removing Packages
-
Install a package:
sudo dnf install nanoInstall the "nano" text editor.
-
Install multiple packages:
sudo dnf install htop treeInstall both "htop" and "tree".
-
Install a specific version of a package:
sudo dnf install httpd-2.4.53-1.fc36Install version
2.4.53-1.fc36of the "httpd" package. -
Remove a package:
sudo dnf remove nanoUninstall the "nano" text editor.
-
Remove a package and its dependencies that are no longer needed:
sudo dnf autoremoveClean up orphaned dependencies.
-
Erase a package without removing configuration files:
sudo dnf erase nanoSame as
remove, but generally prefers to keep configuration files.
Updating Packages
-
Update all installed packages:
sudo dnf upgradeUpgrade all packages on the system to their latest available versions.
-
Update a specific package:
sudo dnf upgrade httpdUpgrade only the "httpd" package.
-
Check for available updates:
dnf check-updateList packages that have available updates without installing them.
Package Groups
-
List available package groups:
dnf group listShow all available software groups.
-
List installed package groups:
dnf group list --installedShow 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 repolistShow repositories that
dnfis currently using. -
List all repositories (enabled and disabled):
dnf repolist allShow all configured repositories.
-
Enable a disabled repository:
sudo dnf config-manager --set-enabled fedora-cisco-openh264Enable the repository named "fedora-cisco-openh264".
-
Disable a repository:
sudo dnf config-manager --set-disabled fedora-cisco-openh264Disable the repository named "fedora-cisco-openh264".
-
Add a new repository from a file:
sudo dnf config-manager --add-repo /etc/yum.repos.d/myrepo.repoAdd 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.repoAdd a repository from a remote URL.
History and Rollback
-
Show transaction history:
dnf historyList past
dnfoperations. -
Show details of a specific transaction:
dnf history info 5Display the packages installed/removed in transaction ID
5. -
Undo a transaction:
sudo dnf history undo 5Revert all changes made by transaction ID
5. -
Redo a transaction:
sudo dnf history redo 5Re-apply changes from transaction ID
5(useful ifundofailed or was partial). -
Rollback to a specific transaction ID:
sudo dnf history rollback 5Undo all transactions after transaction ID
5, effectively reverting the system to the state after transaction5completed.
Other Useful Commands
-
Clean package cache:
sudo dnf clean allRemove cached package files and metadata.
-
Rebuild RPM database:
sudo dnf distro-sync --refreshSynchronize installed packages to the latest available versions, potentially downgrading or removing packages if necessary to match repository metadata. The
--refreshensures metadata is updated first. -
Install local RPM file:
sudo dnf install /path/to/package.rpmInstall a package from a local
.rpmfile, automatically resolving dependencies from repositories. -
Downgrade a package:
sudo dnf downgrade httpdInstall an older version of the "httpd" package.
-
Mark a package as installed without installing it:
sudo dnf mark install nanoTell
dnfthat "nano" is installed, useful for manual package management or fixing database inconsistencies. -
Mark a package as not installed:
sudo dnf mark exclude nanoPrevent
dnffrom automatically installing or updating "nano". -
Exclude packages from updates:
sudo dnf versionlock add nanoPrevent "nano" from being upgraded. Use
dnf versionlock listto see locked packages anddnf versionlock delete nanoto 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 autoremoveEnsures 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 httpdUseful 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
sudois required: Mostdnfoperations that modify the system (install, remove, upgrade) require root privileges.- Transaction limitations:
dnfperforms 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
.repofiles can sometimes influence dependency resolution if the same package is available in multiple repositories with different versions.dnfusually prioritizes the repository listed first. dnf history undovsrollback:undoreverses a single transaction.rollbackundoes a range of transactions. Be careful when usingrollbackas it can revert many changes.- Configuration files: When removing packages,
dnfmight 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:
dnfrelies heavily on network access to repositories. Intermittent network problems can cause installation or update failures. Ensure stable connectivity or consider using local repositories. dnfvsyum: Whilednfis the successor toyumand aims for backward compatibility, some older scripts or very specificyumbehaviors might not translate perfectly.dnfis generally faster and has better dependency resolution.