What is DNF in Rocky Linux?

DNF (Dandified YUM) is the next-generation package manager for Rocky Linux and other Red Hat-based distributions. It provides improved performance, better dependency resolution, and cleaner command syntax compared to the traditional YUM package manager.

Key Benefits of DNF

  • Faster Performance: Better caching and parallel downloads
  • Improved Dependency Resolution: Smarter conflict handling
  • Enhanced Security: Built-in GPG signature verification
  • Cleaner Syntax: More intuitive command structure

Essential DNF Update Commands

Check for Available Updates

Before updating, check what packages have available updates:

# List all available updates
sudo dnf check-update
 
# Count available updates
sudo dnf check-update --quiet | wc -l

Update All Packages

# Update all installed packages
sudo dnf update
 
# Update with automatic yes to prompts
sudo dnf update -y
 
# Update only security patches
sudo dnf update --security

Update Specific Packages

# Update a specific package
sudo dnf update package-name
 
# Update multiple specific packages
sudo dnf update package1 package2 package3
 
# Update packages matching a pattern
sudo dnf update "kernel*"

Upgrade vs Update

# Update packages (keeps obsolete packages)
sudo dnf update
 
# Upgrade packages (removes obsolete packages)
sudo dnf upgrade

DNF Cleanup Commands

Remove Unnecessary Packages

# Remove orphaned packages
sudo dnf autoremove
 
# Remove specific package and its dependencies
sudo dnf remove package-name
 
# Remove package without dependencies
sudo dnf remove --noautoremove package-name

Clean Package Cache

# Clean all cached packages
sudo dnf clean all
 
# Clean only cached packages
sudo dnf clean packages
 
# Clean metadata cache
sudo dnf clean metadata
 
# Clean expired cache
sudo dnf clean expire-cache

View and Manage Cache

# Show cache information
sudo dnf repoquery --cacheonly
 
# Check cache size
sudo du -sh /var/cache/dnf/
 
# Clean old kernels (keep latest 3)
sudo dnf remove $(dnf repoquery --installonly --latest-limit=-3 -q)

System Maintenance Workflow

Complete Update and Cleanup Process

#!/bin/bash
# Complete system maintenance script
 
echo "Starting system update and cleanup..."
 
# Update package repositories
echo "Updating repository metadata..."
sudo dnf makecache
 
# Check for available updates
echo "Checking for available updates..."
sudo dnf check-update
 
# Perform system update
echo "Updating all packages..."
sudo dnf update -y
 
# Remove orphaned packages
echo "Removing orphaned packages..."
sudo dnf autoremove -y
 
# Clean package cache
echo "Cleaning package cache..."
sudo dnf clean all
 
# Check disk space saved
echo "Cache cleanup completed!"
du -sh /var/cache/dnf/
 
echo "System maintenance completed!"

Scheduled Maintenance

Create a cron job for automatic maintenance:

# Edit crontab
sudo crontab -e
 
# Add weekly maintenance (Sundays at 2 AM)
0 2 * * 0 /usr/bin/dnf update -y && /usr/bin/dnf autoremove -y && /usr/bin/dnf clean all

Advanced DNF Management

Managing Repositories

# List enabled repositories
sudo dnf repolist
 
# List all repositories (enabled and disabled)
sudo dnf repolist --all
 
# Enable a repository
sudo dnf config-manager --enable repository-name
 
# Disable a repository
sudo dnf config-manager --disable repository-name

Package Information and History

# View package information
sudo dnf info package-name
 
# View installation history
sudo dnf history
 
# View specific transaction
sudo dnf history info transaction-id
 
# Undo a transaction
sudo dnf history undo transaction-id

Handling Package Groups

# List available groups
sudo dnf group list
 
# Install a package group
sudo dnf group install "Development Tools"
 
# Remove a package group
sudo dnf group remove "Development Tools"
 
# Update a package group
sudo dnf group update "Development Tools"

Before Major Updates

# Create system snapshot (if using LVM)
sudo lvcreate -L5G -s -n root-snapshot /dev/mapper/rl-root
 
# Backup important configurations
sudo tar -czf /backup/etc-backup-$(date +%Y%m%d).tar.gz /etc/

Safe Update Practices

  1. Test Updates in Staging: Always test major updates in a non-production environment
  2. Schedule Maintenance Windows: Plan updates during low-usage periods
  3. Monitor System Logs: Check /var/log/dnf.log for update issues
  4. Keep Backups Current: Maintain recent system backups before major updates

Performance Optimization

# Configure DNF for better performance
sudo nano /etc/dnf/dnf.conf
 
# Add these lines for optimization:
fastestmirror=1
max_parallel_downloads=10
deltarpm=true

Troubleshooting Common Issues

Broken Dependencies

# Fix broken dependencies
sudo dnf check
 
# Force reinstall problematic packages
sudo dnf reinstall package-name
 
# Reset DNF cache
sudo rm -rf /var/cache/dnf/*
sudo dnf makecache

Repository Issues

# Clear repository cache
sudo dnf clean all
sudo dnf makecache --refresh
 
# Temporarily skip problematic repository
sudo dnf update --disablerepo=problematic-repo

Kernel Update Issues

# List installed kernels
sudo dnf list installed kernel
 
# Remove old kernels manually
sudo dnf remove kernel-old-version
 
# Set kernel retention policy
echo "installonly_limit=3" | sudo tee -a /etc/dnf/dnf.conf

Security Considerations

Security Updates Priority

# Install only security updates
sudo dnf update --security
 
# List available security updates
sudo dnf updateinfo list security
 
# Get detailed security information
sudo dnf updateinfo info security

Package Verification

# Verify package integrity
rpm -Va
 
# Check specific package
rpm -V package-name
 
# Verify GPG signatures
rpm --checksig package-file.rpm

Monitoring and Logging

DNF Logs

# View DNF transaction log
sudo less /var/log/dnf.log
 
# Monitor real-time DNF activity
sudo tail -f /var/log/dnf.log
 
# Check RPM transaction log
sudo less /var/log/dnf.rpm.log

System Health Checks

# Check available disk space
df -h
 
# Monitor system resources during updates
top
htop
 
# Check for failed services after updates
sudo systemctl --failed

Automation Scripts

Simple Update Script

Create /usr/local/bin/system-update.sh:

#!/bin/bash
# Simple system update script
 
LOG_FILE="/var/log/system-update.log"
 
{
    echo "=== System Update Started: $(date) ==="
    
    # Update system
    dnf update -y
    
    # Clean up
    dnf autoremove -y
    dnf clean all
    
    echo "=== System Update Completed: $(date) ==="
    echo ""
} | tee -a "$LOG_FILE"

Make it executable:

sudo chmod +x /usr/local/bin/system-update.sh

Conclusion

Regular software updates and system cleanup are essential for maintaining a secure and efficient Rocky Linux system. DNF provides powerful tools to manage packages effectively:

Key Takeaways:

  • Update regularly with sudo dnf update
  • Clean up with sudo dnf autoremove and sudo dnf clean all
  • Monitor system health after updates
  • Automate routine maintenance with cron jobs
  • Always backup before major system changes

Weekly Maintenance Routine:

# Complete weekly maintenance
sudo dnf update -y
sudo dnf autoremove -y
sudo dnf clean all

By following these practices, you’ll maintain a secure, up-to-date, and optimally performing Rocky Linux system. Remember to always test updates in non-production environments first and maintain current backups of critical data and configurations.