How to manage packages with yum on CentOS/RHEL
How to Manage Packages with YUM on CentOS/RHEL
Table of Contents
1. [Introduction](#introduction)
2. [Prerequisites](#prerequisites)
3. [Understanding YUM Package Manager](#understanding-yum-package-manager)
4. [Basic YUM Commands](#basic-yum-commands)
5. [Installing Packages](#installing-packages)
6. [Updating Packages](#updating-packages)
7. [Removing Packages](#removing-packages)
8. [Searching and Information Commands](#searching-and-information-commands)
9. [Repository Management](#repository-management)
10. [YUM Groups](#yum-groups)
11. [YUM History and Rollback](#yum-history-and-rollback)
12. [Advanced YUM Operations](#advanced-yum-operations)
13. [Troubleshooting Common Issues](#troubleshooting-common-issues)
14. [Best Practices](#best-practices)
15. [Security Considerations](#security-considerations)
16. [Conclusion](#conclusion)
Introduction
The Yellowdog Updater Modified (YUM) is the primary package management system for Red Hat Enterprise Linux (RHEL), CentOS, and other Red Hat-based distributions. YUM provides a powerful command-line interface for installing, updating, removing, and managing software packages, making system administration more efficient and reliable.
This comprehensive guide will teach you everything you need to know about managing packages with YUM, from basic operations to advanced techniques. Whether you're a system administrator, developer, or Linux enthusiast, mastering YUM is essential for effective package management on CentOS and RHEL systems.
Prerequisites
Before diving into YUM package management, ensure you have:
- A CentOS or RHEL system (versions 6.x, 7.x, or 8.x)
- Root or sudo privileges for package management operations
- Active internet connection for downloading packages
- Basic understanding of Linux command-line interface
- Familiarity with terminal operations
Note: CentOS 8 and RHEL 8 have transitioned to DNF as the default package manager, but YUM commands are still available through compatibility layers.
Understanding YUM Package Manager
What is YUM?
YUM is a package management utility that automatically handles dependencies, making software installation and maintenance straightforward. It works with RPM (Red Hat Package Manager) packages and can download packages from configured repositories.
Key Features of YUM
- Automatic dependency resolution: YUM automatically identifies and installs required dependencies
- Repository management: Supports multiple software repositories
- Transaction rollback: Ability to undo package operations
- Group installations: Install related packages as groups
- Security updates: Streamlined security patch management
- Package verification: Integrity checking and validation
YUM Architecture
YUM consists of several components:
```
YUM Client → Repository Metadata → RPM Database → Package Files
```
Basic YUM Commands
Essential Command Structure
All YUM commands follow this basic syntax:
```bash
yum [options] command [package_name]
```
Getting Help
Display YUM help information:
```bash
yum --help
yum help
man yum
```
Checking YUM Version
Verify your YUM version:
```bash
yum --version
```
Installing Packages
Installing Single Packages
Install a specific package:
```bash
sudo yum install package_name
```
Example - Installing the Apache web server:
```bash
sudo yum install httpd
```
Installing Multiple Packages
Install multiple packages simultaneously:
```bash
sudo yum install package1 package2 package3
```
Example:
```bash
sudo yum install vim wget curl
```
Installing from Local RPM Files
Install packages from local RPM files:
```bash
sudo yum localinstall /path/to/package.rpm
```
Example:
```bash
sudo yum localinstall /tmp/custom-software-1.0.rpm
```
Installing Specific Package Versions
Install a specific version of a package:
```bash
sudo yum install package_name-version
```
Example:
```bash
sudo yum install httpd-2.4.6
```
Downloading Packages Without Installing
Download packages to local directory without installation:
```bash
yum download package_name
```
Or download with dependencies:
```bash
yum download --resolve package_name
```
Updating Packages
Updating All Packages
Update all installed packages to their latest versions:
```bash
sudo yum update
```
Warning: This command updates all packages and may cause system changes. Always test in non-production environments first.
Updating Specific Packages
Update only specific packages:
```bash
sudo yum update package_name
```
Example:
```bash
sudo yum update kernel
```
Security Updates Only
Install only security-related updates:
```bash
sudo yum update --security
```
Checking for Available Updates
List packages with available updates:
```bash
yum check-update
```
Updating Package Groups
Update all packages in a specific group:
```bash
sudo yum groupupdate "Group Name"
```
Removing Packages
Removing Single Packages
Remove a specific package:
```bash
sudo yum remove package_name
```
Example:
```bash
sudo yum remove httpd
```
Removing Multiple Packages
Remove multiple packages:
```bash
sudo yum remove package1 package2 package3
```
Removing with Dependencies
Remove a package and its unused dependencies:
```bash
sudo yum autoremove package_name
```
Removing Orphaned Packages
Remove packages that are no longer needed:
```bash
sudo yum autoremove
```
Searching and Information Commands
Searching for Packages
Search for packages by name or description:
```bash
yum search keyword
```
Example:
```bash
yum search apache
```
Getting Package Information
Display detailed information about a package:
```bash
yum info package_name
```
Example:
```bash
yum info httpd
```
Listing Installed Packages
List all installed packages:
```bash
yum list installed
```
List specific installed packages:
```bash
yum list installed | grep package_name
```
Listing Available Packages
List all available packages:
```bash
yum list available
```
Finding Package Dependencies
Show package dependencies:
```bash
yum deplist package_name
```
Finding Which Package Provides a File
Identify which package provides a specific file:
```bash
yum provides /path/to/file
```
Example:
```bash
yum provides /usr/bin/vim
```
Repository Management
Listing Configured Repositories
Display all configured repositories:
```bash
yum repolist
```
Show detailed repository information:
```bash
yum repolist -v
```
Enabling and Disabling Repositories
Disable a repository:
```bash
sudo yum-config-manager --disable repository_name
```
Enable a repository:
```bash
sudo yum-config-manager --enable repository_name
```
Adding New Repositories
Add a repository using yum-config-manager:
```bash
sudo yum-config-manager --add-repo repository_url
```
Example - Adding EPEL repository:
```bash
sudo yum install epel-release
```
Repository Configuration Files
Repository configurations are stored in:
- `/etc/yum.repos.d/` - Repository definition files
- `/etc/yum.conf` - Main YUM configuration
Example repository file (`/etc/yum.repos.d/example.repo`):
```ini
[example-repo]
name=Example Repository
baseurl=https://example.com/repo/
enabled=1
gpgcheck=1
gpgkey=https://example.com/repo/RPM-GPG-KEY
```
Cleaning Repository Cache
Clean YUM cache:
```bash
sudo yum clean all
```
Clean specific cache types:
```bash
sudo yum clean packages
sudo yum clean metadata
sudo yum clean expire-cache
```
YUM Groups
Understanding Package Groups
YUM groups are collections of related packages that can be installed together, such as "Development Tools" or "Web Server".
Listing Available Groups
Display available package groups:
```bash
yum grouplist
```
Show detailed group information:
```bash
yum groupinfo "Group Name"
```
Installing Package Groups
Install a complete package group:
```bash
sudo yum groupinstall "Group Name"
```
Example:
```bash
sudo yum groupinstall "Development Tools"
```
Updating Package Groups
Update all packages in a group:
```bash
sudo yum groupupdate "Group Name"
```
Removing Package Groups
Remove a package group:
```bash
sudo yum groupremove "Group Name"
```
YUM History and Rollback
Viewing YUM History
Display YUM transaction history:
```bash
yum history
```
Show detailed information about a specific transaction:
```bash
yum history info transaction_id
```
Rolling Back Transactions
Undo a specific YUM transaction:
```bash
sudo yum history undo transaction_id
```
Redo a previously undone transaction:
```bash
sudo yum history redo transaction_id
```
Rolling Back to Previous State
Roll back to a specific point in history:
```bash
sudo yum history rollback transaction_id
```
Advanced YUM Operations
Using YUM with Specific Repositories
Install from a specific repository:
```bash
sudo yum --enablerepo=repository_name install package_name
```
Disable specific repositories during operation:
```bash
sudo yum --disablerepo=repository_name install package_name
```
Downloading Source RPMs
Download source RPM packages:
```bash
yum download --source package_name
```
Creating Local Repositories
Create a local repository from RPM files:
```bash
createrepo /path/to/rpm/directory
```
YUM Configuration Tuning
Edit `/etc/yum.conf` for performance tuning:
```ini
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=1
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=3
timeout=300
retries=10
```
Using YUM Plugins
Popular YUM plugins:
- `yum-plugin-security`: Security update management
- `yum-plugin-fastestmirror`: Automatically select fastest mirrors
- `yum-plugin-priorities`: Repository priority management
Install plugins:
```bash
sudo yum install yum-plugin-security
```
Troubleshooting Common Issues
Resolving Dependency Conflicts
When encountering dependency issues:
1. Check for conflicting packages:
```bash
yum check
```
2. Force dependency resolution:
```bash
sudo yum install package_name --skip-broken
```
3. Use whatprovides to find alternatives:
```bash
yum whatprovides dependency_name
```
Fixing Corrupted RPM Database
Rebuild the RPM database:
```bash
sudo rpm --rebuilddb
```
Resolving GPG Key Issues
Import missing GPG keys:
```bash
sudo rpm --import /path/to/GPG-KEY
```
Fixing Repository Errors
1. Clear YUM cache:
```bash
sudo yum clean all
```
2. Update repository metadata:
```bash
sudo yum makecache
```
3. Check repository configuration:
```bash
yum repolist enabled
```
Handling Lock Files
Remove stale YUM lock files:
```bash
sudo rm -f /var/run/yum.pid
```
Network-Related Issues
1. Check network connectivity:
```bash
ping repository_server
```
2. Configure proxy settings in `/etc/yum.conf`:
```ini
proxy=http://proxy_server:port
proxy_username=username
proxy_password=password
```
3. Test repository accessibility:
```bash
curl -I repository_url
```
Memory and Disk Space Issues
1. Check available disk space:
```bash
df -h /var/cache/yum
```
2. Monitor memory usage during operations:
```bash
free -m
```
3. Clean unnecessary cache:
```bash
sudo yum clean all
```
Best Practices
Regular System Maintenance
1. Perform regular updates:
```bash
Weekly security updates
sudo yum update --security
Monthly full updates (test first)
sudo yum update
```
2. Clean cache regularly:
```bash
sudo yum clean packages
```
3. Monitor system logs:
```bash
tail -f /var/log/yum.log
```
Repository Management Best Practices
1. Use official repositories when possible
2. Verify repository GPG keys
3. Set repository priorities appropriately
4. Test third-party repositories in non-production environments
Package Installation Guidelines
1. Always read package descriptions before installation
2. Check dependencies and potential conflicts
3. Create system backups before major updates
4. Test installations in development environments first
Security Best Practices
1. Keep systems updated with security patches:
```bash
sudo yum update --security
```
2. Verify package signatures:
```bash
rpm -K package.rpm
```
3. Use only trusted repositories
4. Monitor installed packages regularly:
```bash
rpm -qa --last
```
Performance Optimization
1. Configure fastest mirror plugin:
```bash
sudo yum install yum-plugin-fastestmirror
```
2. Enable delta RPMs for bandwidth savings:
```ini
In /etc/yum.conf
deltarpm=1
```
3. Optimize cache settings:
```ini
In /etc/yum.conf
keepcache=1
metadata_expire=7d
```
Documentation and Change Management
1. Document all package installations and removals
2. Maintain inventory of installed packages
3. Use YUM history for change tracking
4. Implement approval processes for production changes
Security Considerations
Package Verification
Always verify package integrity:
```bash
rpm -V package_name
```
GPG Key Management
1. Import and verify GPG keys:
```bash
sudo rpm --import RPM-GPG-KEY-file
```
2. List imported keys:
```bash
rpm -q gpg-pubkey --qf '%{name}-%{version}-%{release} --> %{summary}\n'
```
Repository Security
1. Use HTTPS repositories when available
2. Verify repository signatures
3. Avoid untrusted third-party repositories
4. Regularly audit enabled repositories
Security Updates
Prioritize security updates:
```bash
List available security updates
yum --security check-update
Install only security updates
sudo yum update --security
```
Access Control
1. Limit sudo access to YUM commands
2. Use specific sudoers entries:
```bash
In /etc/sudoers
user ALL=(ALL) NOPASSWD: /usr/bin/yum update, /usr/bin/yum install
```
3. Monitor package management activities
4. Implement logging and auditing
Conclusion
Mastering YUM package management is essential for effective administration of CentOS and RHEL systems. This comprehensive guide has covered everything from basic package operations to advanced repository management and troubleshooting techniques.
Key takeaways include:
- Understanding YUM fundamentals: YUM provides powerful package management with automatic dependency resolution
- Essential operations: Installing, updating, and removing packages safely and efficiently
- Repository management: Configuring and maintaining software repositories for optimal package availability
- Troubleshooting skills: Resolving common issues and maintaining system stability
- Security practices: Implementing secure package management procedures
- Best practices: Following industry standards for system maintenance and change management
As you continue working with CentOS and RHEL systems, remember that package management is a critical responsibility that affects system stability, security, and performance. Always test changes in non-production environments, maintain proper documentation, and stay informed about security updates.
For systems running CentOS 8 or RHEL 8 and later, consider transitioning to DNF, which offers improved performance and additional features while maintaining compatibility with YUM commands. The skills and concepts learned in this guide will serve as a solid foundation for working with any RPM-based package management system.
Regular practice with YUM commands, combined with careful attention to security and best practices, will help you become proficient in managing packages effectively and maintaining robust, secure Linux systems.