How to use the command 'dnf' (with examples)

How to use the command 'dnf' (with examples)

The ‘dnf’ command is a package management utility for RHEL, Fedora, and CentOS. It replaces the ‘yum’ package manager and allows users to manage software packages on their Linux systems. This article provides examples of several use cases of the ‘dnf’ command.

Use case 1: Upgrade installed packages to the newest available versions

Code:

sudo dnf upgrade

Motivation: This use case is helpful when you want to update all installed packages on your Linux system to their latest available versions. Upgrading packages ensures that you have access to the latest features, bug fixes, and security patches.

Explanation: The ‘dnf upgrade’ command upgrades all installed packages to their newest available versions. The ‘sudo’ command is used to run ‘dnf’ with administrative privileges, allowing it to perform system-level operations.

Example output:

---> Package package1.x86_64 0:1.0-1.fc34 will be upgraded
---> Package package2.noarch 0:2.0-1.fc34 will be upgraded
---> Package package3.x86_64 0:3.0-1.fc34 will be upgraded
...
Upgraded:
  package1.x86_64 0:1.0-1.fc34
  package2.noarch 0:2.0-1.fc34
  package3.x86_64 0:3.0-1.fc34
...

Use case 2: Search packages via keywords

Code:

dnf search keyword1 keyword2 ...

Motivation: This use case allows you to search for packages containing specific keywords. It can be useful when you are looking for a particular package or when you want to explore available packages related to a specific topic.

Explanation: The ‘dnf search’ command searches for packages containing one or more specified keywords. Replace ‘keyword1 keyword2 …’ with the keywords you want to search for. ‘dnf’ will return a list of packages that match the provided keywords.

Example output:

Last metadata expiration check: 0:10:45 ago on Mon 01 Nov 2021 10:00:00 AM UTC.
================================= Name & Summary Matched: keyword ==================================
package1.x86_64 : Package description containing keyword1
package2.noarch : Package description containing keyword2
...

Use case 3: Display details about a package

Code:

dnf info package

Motivation: This use case allows you to view information about a specific package. It can be useful when you want to learn more about a package, including its version, dependencies, description, and maintainer.

Explanation: The ‘dnf info’ command displays detailed information about the specified package. Replace ‘package’ with the name of the package you want to view information about. ‘dnf’ will provide information such as package name, version, size, description, and other relevant details.

Example output:

Available Packages
Name     : package1
Version  : 1.0-1.fc34
Arch     : x86_64
Size     : 10 MB
Source   : https://example.com/package1-1.0-1.fc34.src.rpm
License  : MIT
Summary  : A package providing functionality for XYZ
...

Use case 4: Install a new package

Code:

sudo dnf install package1 package2 ...

Motivation: This use case allows you to install new packages on your Linux system. Installing packages is essential when you want to add new software or tools to your system.

Explanation: The ‘dnf install’ command installs one or more specified packages. Replace ‘package1 package2 …’ with the names of the packages you want to install. The ‘sudo’ command is used to run ‘dnf’ with administrative privileges, allowing it to modify system files and install packages.

Example output:

Dependencies resolved.
===================================================================================
 Package         Architecture   Version       Repository                      Size
===================================================================================
Installing:
 package1        x86_64         1.0-1.fc34    fedora                          10 MB
 package2        noarch         2.0-1.fc34    fedora                          50 KB
...
Transaction Summary
===================================================================================
Install  2 Packages
...

Use case 5: Remove a package

Code:

sudo dnf remove package1 package2 ...

Motivation: This use case allows you to remove installed packages from your Linux system. Removing unnecessary or unwanted packages can free up disk space and improve system performance.

Explanation: The ‘dnf remove’ command removes one or more specified packages from the system. Replace ‘package1 package2 …’ with the names of the packages you want to remove. The ‘sudo’ command is used to run ‘dnf’ with administrative privileges, allowing it to modify system files and remove packages.

Example output:

Dependencies resolved.
===================================================================================
 Package         Architecture   Version       Repository                      Size
===================================================================================
Removing:
 package1        x86_64         1.0-1.fc34    @fedora                         10 MB
 package2        noarch         2.0-1.fc34    @fedora                         50 KB

Transaction Summary
===================================================================================
Remove  2 Packages

Freed space: 10 MB
...

Use case 6: List installed packages

Code:

dnf list --installed

Motivation: This use case allows you to view a list of all installed packages on your Linux system. Listing installed packages can help you identify installed software and manage dependencies.

Explanation: The ‘dnf list –installed’ command lists all packages that are currently installed on the system. The ‘–installed’ option filters the output to display only the installed packages.

Example output:

Installed Packages
package1.x86_64     1.0-1.fc34
package2.noarch     2.0-1.fc34
package3.x86_64     3.0-1.fc34
...

Use case 7: Find which packages provide a given command

Code:

dnf provides command

Motivation: This use case allows you to determine which package provides a specific command. It can be useful when you want to install a package that contains a particular command you need.

Explanation: The ‘dnf provides’ command searches for the package that provides the specified command. Replace ‘command’ with the name of the command you want to find. ‘dnf’ will return the package name and version that includes the command.

Example output:

package1-1.0-1.fc34.x86_64 : /usr/bin/command

Use case 8: View all past operations

Code:

dnf history

Motivation: This use case allows you to view a history of all past ‘dnf’ operations performed on your Linux system. Viewing the history can help you track package installations, updates, and removals.

Explanation: The ‘dnf history’ command displays a detailed history of all past ‘dnf’ operations. It includes information such as operation ID, date, transaction type, packages affected, and any errors encountered.

Example output:

ID     | Command line            | Date and time    | Action(s)     | Altered
-------------------------------------------------------------------------------
5      | install package1        | 2021-10-30 10:00 | Install       |    1   
4      | upgrade                | 2021-10-29 10:00 | Upgrade       |   10   
3      | remove package2         | 2021-10-28 10:00 | Erase         |    1
...

Conclusion

The ‘dnf’ command is a powerful package management utility that allows users to manage software packages on RHEL, Fedora, and CentOS systems. With examples of various use cases provided in this article, you can now effectively use ‘dnf’ to upgrade, search, install, remove packages, and perform other package management tasks.

Tags :

Related Posts

How to use the command `pacman --sync` (with examples)

How to use the command `pacman --sync` (with examples)

pacman --sync is a command used in Arch Linux to manage packages.

Read More
Using the `pushd` Command (with examples)

Using the `pushd` Command (with examples)

Use Case 1: Switch to a Directory and Push it on the Stack Code: pushd path/to/directory Motivation: The pushd command allows us to switch to a specified directory and push it onto the directory stack.

Read More
Using the in-toto-run command (with examples)

Using the in-toto-run command (with examples)

1: Tag a git repo and sign the resulting link file in-toto-run -n tag --products .

Read More