dnf

All posts tagged dnf by Linux Bash
  • Posted on

    Automating system updates using Bash scripts and RHEL's yum or dnf package manager ensures your Red Hat Enterprise Linux (RHEL)-based systems remain secure and up-to-date. Below is a guide tailored for RHEL environments.


    Steps for Automating System Updates

    1. Create a Bash Script for Updates The script will:
      • Update the package repository.
      • Upgrade installed packages.
      • Remove unused packages (optional).
      • Log the results for review.

    Example Script:

    #!/bin/bash
    
    # Variables
    LOG_FILE="/var/log/system_update.log"
    DATE=$(date '+%Y-%m-%d %H:%M:%S')
    
    # Update and upgrade
    echo "[$DATE] Starting system update..." | tee -a $LOG_FILE
    
    # Update package list and upgrade packages
    if sudo dnf update -y; then
        echo "[$DATE] System update successful." | tee -a $LOG_FILE
    else
        echo "[$DATE] System update failed." | tee -a $LOG_FILE
        exit 1
    fi
    
    # Clean up unused packages
    if sudo dnf autoremove -y && sudo dnf clean all; then
        echo "[$DATE] Cleanup successful." | tee -a $LOG_FILE
    else
        echo "[$DATE] Cleanup failed." | tee -a $LOG_FILE
    fi
    
    echo "[$DATE] Update process completed." | tee -a $LOG_FILE
    exit 0
    

    Replace dnf with yum if using an older RHEL version.


    1. Make the Script Executable Save the script as update_system.sh and make it executable.
    chmod +x update_system.sh
    

    1. Test the Script Run the script manually to confirm it works.
    ./update_system.sh
    

    1. Schedule the Script Using cron Automate execution by scheduling the script with cron.
    • Open the crontab editor:
    crontab -e
    
    • Add a line to schedule the script. For example:

      • Run daily at 2:00 AM:
      0 2 * * * /path/to/update_system.sh
      
      • Run weekly at 3:00 AM on Sundays:
      0 3 * * 0 /path/to/update_system.sh
      
    • Save and exit.


    1. Verify the Cron Job Check the scheduled cron job.
    crontab -l
    

    1. Monitor Logs Review logs for successful updates or troubleshoot errors. The script logs updates to /var/log/system_update.log.

    Advanced Automation Enhancements

    1. Email Notifications Configure the script to send email alerts upon success or failure.

    Example (Add to Script):

    if sudo dnf update -y; then
        echo "System update completed successfully." | mail -s "System Update Success" admin@example.com
    else
        echo "System update failed!" | mail -s "System Update Failure" admin@example.com
    fi
    
    1. Exclude Certain Packages Avoid updating specific packages by marking them as held.
    sudo dnf mark exclude <package-name>
    
    1. Automate Kernel Updates Separately Automate kernel updates only when needed to avoid unnecessary reboots.

    By combining Bash scripting with RHEL-specific tools like dnf-automatic, you can fully automate system updates, enhance security, and reduce administrative overhead.