CentOS Package Management: Top 20 Command Examples for System Administrators

Managing packages is a crucial aspect of system administration and development in Linux environments, such as CentOS. This guide is designed to introduce you to the top 20 most common examples of package manager usage on CentOS. Whether you are a new system administrator or a seasoned developer, understanding these examples will help you effectively manage your CentOS system’s packages, ensuring it runs smoothly and securely.

In this tutorial you will learn:

  • How to install, update, and remove packages
  • How to manage package repositories
  • Advanced package management operations
CentOS Package Management: Top 20 Command Examples for System Administrators
CentOS Package Management: Top 20 Command Examples for System Administrators
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System CentOS Linux Distribution
Software YUM or DNF package manager
Other Internet connection for downloading packages
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Top 20 Package Manager Commands for CentOS

Below are detailed examples and explanations of the most common package management operations you can perform on a CentOS system. These operations will help you maintain the software packages on your system effectively.

CentOS Package Management Cheat Sheet

A quick reference guide for system administrators on using YUM and DNF command-line tools for package management in CentOS.

YUM vs DNF
YUM (Yellowdog Updater, Modified) and DNF (Dandified YUM) are both package managers used in CentOS and other RPM-based Linux distributions, with DNF being introduced as the next-generation version of YUM. While YUM has been the traditional tool for package management, offering capabilities to install, update, and remove packages along with dependency resolution, DNF aims to improve on YUM by offering faster and more accurate dependency resolution, a cleaner, well-structured codebase for easier maintenance, and additional features like automatic synchronization of package metadata. DNF was introduced in Fedora and has been adopted as the default package manager in CentOS 8 and above, marking a significant step forward in the evolution of Linux package management.
YUM & DNF Commands Summary
# Task YUM Command DNF Command
1. Install a package sudo yum install package_name sudo dnf install package_name
2. Update a package sudo yum update package_name sudo dnf update package_name
3. Remove a package sudo yum remove package_name sudo dnf remove package_name
4. Search for a package yum search keyword dnf search keyword
5. List all installed packages yum list installed dnf list installed
6. Check for available updates yum check-update dnf check-update
7. Clean cached data sudo yum clean all sudo dnf clean all
8. List enabled repositories yum repolist dnf repolist
9. Enable a repository sudo yum-config-manager --enable repo_name sudo dnf config-manager --set-enabled repo_name
10. Disable a repository sudo yum-config-manager --disable repo_name sudo dnf config-manager --set-disabled repo_name
11. Upgrade all packages sudo yum upgrade sudo dnf upgrade
12. Show package information yum info package_name dnf info package_name
13. Find which package provides a file yum provides file_name dnf provides file_name
14. Check package dependencies yum deplist package_name dnf repoquery --requires --resolve package_name
15. Download a package without installing yum download package_name dnf download package_name
16. Install a local RPM package sudo yum localinstall /full/path/to/package_name.rpm sudo dnf install /full/path/to/package_name.rpm
17. Lock a package version sudo yum versionlock package_name sudo dnf versionlock add package_name
18. Unlock a package version sudo yum versionlock delete package_name sudo dnf versionlock delete package_name
19. Reinstall a package sudo yum reinstall package_name sudo dnf reinstall package_name
20. List all available packages yum list all dnf list all
  1. Install a Package: To install a new package on your CentOS system, use the following command. This command will search the configured repositories and install the specified package along with its dependencies.
    $ sudo dnf install package_name
    Install a Package
    Install a Package

    This command is fundamental for adding new software to your system.

  2. Update a Package: Keep your system secure and up-to-date by updating individual packages as follows. This will ensure you have the latest features and security fixes.
    $ sudo dnf update package_name

    To update all packages, simply omit the package name.

  3. Remove a Package: If you no longer need a package, you can remove it using the command below. This helps in maintaining a clean system environment.
    $ sudo dnf remove package_name

    This command will also remove the dependencies that are no longer needed by other packages.

    Remove a Package
    Remove a Package
  4. Search for a Package: Finding the right package can be done by searching the repositories with the following command.
    $ dnf search keyword

    Replace “keyword” with a relevant term or package name to find.

    Search for a Package
    Search for a Package
  5. List Installed Packages: To see what packages are currently installed on your system, use the command below.
    $ dnf list installed

    This is useful for auditing or reviewing current software.

  6. Check for Updates: It’s important to regularly check for package updates to maintain system security and stability.
    $ dnf check-update

    This command lists all packages that have updates available.

  7. Clean the Cache: DNF stores downloaded packages and metadata in cache. Cleaning the cache can free up disk space.
    $ sudo dnf clean all

    This is a good maintenance practice, especially before a backup.

    Clean the Cache
    Clean the Cache
  8. Install a Package from a Specific Repository: If you have multiple repositories configured, you can specify from which repository to install a package.
    $ sudo dnf --enablerepo=repository_name install package_name

    This is useful for testing or when a package is only available in a specific repository.

  9. List Available Repositories: To see which repositories are configured and enabled on your system, use the following command.
    $ dnf repolist

    This can help you manage where your packages are coming from.

    List Available Repositories
    List Available Repositories
  10. Enable or Disable Repositories: Control which repositories are used for package management with these commands.
    $ sudo dnf config-manager --set-enabled repository_name
    $ sudo dnf config-manager --set-disabled repository_name

    Enabling or disabling repositories can affect package availability and versioning.

    Enable or Disable Repositories
    Enable or Disable Repositories
  11. Upgrade System: To upgrade your entire system to the latest version of all packages, use the following command. This is more comprehensive than updating individual packages.
    $ sudo dnf upgrade

    This operation can significantly change your system, so use it with caution.



  12. Show Package Information: Get detailed information about a package, including version, release, and description.
    $ dnf info package_name

    This is useful for understanding what a package does before installing it.

    Show Package Information
    Show Package Information
  13. Find Which Package Provides a File: If you need a specific file but don’t know which package provides it, use this command.
    $ dnf provides file_name

    This can be very helpful in resolving dependencies or conflicts.

    Find Which Package Provides a File
    Find Which Package Provides a File
  14. Check Package Dependencies: Before installing a package, you might want to check its dependencies.
    $ dnf deplist package_name
    OR
    $ dnf repoquery --requires --resolve package_name

    This command lists all dependencies required by the package.

    Check Package Dependencies
    Check Package Dependencies
  15. Download a Package Without Installing: To download a package and all its dependencies without installing them, use the following command.
    $ dnf download package_name

    This is useful for manual inspection or installation on another system.

    Download a Package Without Installing
    Download a Package Without Installing
  16. Install a Local RPM Package: If you have a .rpm file downloaded, you can install it directly with DNF.
    $ sudo dnf install /full/path/to/package_name.rpm

    This command resolves and installs dependencies from the repositories.

    Install a Local RPM Package
    Install a Local RPM Package
  17. Lock a Package Version: Prevent a package from being updated accidentally by locking it at a specific version.
    $ sudo dnf versionlock add package_name

    This ensures stability, especially for sensitive applications.

    Lock a Package Version
    Lock a Package Version
  18. Unlock a Package Version: If you need to update a locked package, you can unlock it with the following command.
    $ sudo dnf versionlock delete package_name

    This restores the ability to update the package.

    Unlock a Package Version
    Unlock a Package Version
  19. Reinstall a Package: To reinstall a package, perhaps to fix a broken installation, use the following command.
    $ sudo dnf reinstall package_name

    This can be a quick fix for corrupted package installations.

    Reinstall a Package
    Reinstall a Package
  20. List All Available Packages: To see every package available in your enabled repositories, use this command.
    $ dnf list all

    This can help you discover new packages or versions.

    List All Available Packages
    List All Available Packages

Conclusion

Mastering package management on CentOS with YUM and DNF is essential for maintaining a secure, up-to-date, and efficient system. This guide has covered the top 20 most common package manager examples to help you manage your CentOS system more effectively. Whether you’re installing new software, updating existing packages, or managing repositories, these commands will serve as a solid foundation for your package management tasks.