Easy Guide to Remove Old Linux Kernels on AlmaLinux 8/9

Posted on

Easy Guide to Remove Old Linux Kernels on AlmaLinux 8/9

This tutorial intends to show you how to Delete or Remove Old Linux Kernels on AlmaLinux with DNF. The kernel is the bridge between software and hardware, and it is part of an operating system that interacts with the hardware.

In AlmaLinux, old Linux kernels refer to previous versions of the Linux kernel that were installed during system updates or upgrades. During system updates, newer kernel versions are installed, but older ones are often held to provide backup options in case of issues with the new kernel. If you plan to Remove Old Linux Kernels on AlmaLinux, it is recommended to keep your system updated and install the latest kernel version.

Upgrade AlmaLinux Kernel Version

To install and Upgrade your Kernel version on AlmaLinux servers, you can visit the following guide:

Upgrade Linux Kernel on AlmaLinux

Now you can follow the steps below to display the current kernel version, list all kernels, and Remove Old Linux Kernels on AlmaLinux.

To Remove Old Linux Kernels on AlmaLinux, you must have access to your server as a root or non-root user with sudo privileges. Then, follow the steps below to use the DNF package manager to delete the old kernels.

Note: To see the difference between DNF and YUm, you can visit this guide on Differences between YUM and DNF package managers.

Step 1 – How To List Current Linux Kernel?

First, you can use the following command to display your current kernel version on your AlmaLinux server:

uname -sr
**Output**
Linux 6.4.1-1.el8.elrepo.x86_64

How To List All Linux Kernels? At this point, you can run the following command in your Linux terminal to display all available kernels on AlmaLinux or Rocky Linux:

rpm -q kernel

Step 2 – How To Delete Old Unused Linux Kernels on AlmaLinux?

It is recommended to keep at least one or two old kernels if there is a problem with an update.

Now you can use the following command to Delete Old Linux Kernels on AlmaLinux:

dnf remove $(dnf repoquery --installonly --latest-limit 2 -q)

Note: This command will remove all old/unused kernels and keep the currently running and old latest kernel as a backup.

Also, there is another way that you can remove the old kernels. You can edit the YUM config file and set the kernel limit. To do this, open the file with your favorite text editor, we use vi editor:

vi /etc/yum.conf

And set the kernel counts as shown below:

installonly_limit=2     #set kernel count

When you are done, save and close the file. At this point, when you run the system update only two kernels will be left on the system.

sudo dnf update

Conclusion

At this point, you have learned to display the current kernel version, list all kernels, and remove old Linux kernels on AlmaLinux with the DNF package manager or you can use this step guide to delete unused kernels on Centos.

Hope you enjoy it. Please Subscribe to us on Facebook.


Alternative Methods to Remove Old Linux Kernels on AlmaLinux

While the original article provides a straightforward method using dnf remove and configuring yum.conf, there are alternative approaches to achieve the same goal of managing and removing old kernels on AlmaLinux. These methods offer different levels of control and automation. Let’s explore two distinct alternatives.

Method 1: Using the package-cleanup Utility

The package-cleanup utility, part of the yum-utils package, offers a more focused way to clean up orphaned packages, including old kernels. It’s particularly useful when you want more precise control over which kernels are removed.

1. Install yum-utils:

First, you need to install the yum-utils package if it’s not already present on your system:

sudo dnf install yum-utils

2. Identify Orphaned Kernels:

The package-cleanup --orphaned command will list packages that are no longer required as dependencies by other installed packages. While it won’t specifically identify kernels, you can use it in conjunction with rpm -q kernel to pinpoint older kernels that are safe to remove.

rpm -q kernel
package-cleanup --orphaned | grep kernel

The grep kernel part filters the output of package-cleanup --orphaned to only show lines containing "kernel," helping you identify old kernel packages.

3. Remove Specific Kernels:

Once you’ve identified the kernels you want to remove, you can use dnf remove with the specific package names:

sudo dnf remove kernel-<version>-<release>.el8.x86_64

Replace <version> and <release> with the actual version and release numbers of the kernel package you want to remove (e.g., kernel-4.18.0-372.19.1.el8_6.x86_64). You can remove multiple kernels in a single command by separating the package names with spaces.

Example:

Let’s say rpm -q kernel shows the following kernels installed:

  • kernel-4.18.0-372.19.1.el8_6.x86_64
  • kernel-4.18.0-408.el8.x86_64 (currently running)

And package-cleanup --orphaned | grep kernel shows:

  • kernel-4.18.0-372.19.1.el8_6.x86_64

This indicates that kernel-4.18.0-372.19.1.el8_6.x86_64 is no longer required. You can then remove it with:

sudo dnf remove kernel-4.18.0-372.19.1.el8_6.x86_64

Advantages:

  • More Control: Allows you to remove specific kernels individually, providing greater control over the process.
  • Safer Removal: By identifying orphaned packages, you reduce the risk of accidentally removing a kernel that is still required.

Disadvantages:

  • More Manual: Requires more manual steps to identify and remove kernels compared to the original method.
  • Requires yum-utils: Relies on the yum-utils package being installed.

Method 2: Automating Kernel Removal with a Script

For users who prefer a more automated approach, a script can be created to identify and remove older kernels, while ensuring that the currently running kernel and a specified number of backup kernels are retained.

Script Example (remove_old_kernels.sh):

#!/bin/bash

# Number of kernels to keep (including the running kernel)
KERNEL_LIMIT=2

# Get the currently running kernel version
CURRENT_KERNEL=$(uname -r)

# Get a list of all installed kernels
INSTALLED_KERNELS=$(rpm -q kernel | sort -r)

# Calculate the number of kernels to remove
NUM_KERNELS=$(echo "$INSTALLED_KERNELS" | wc -l)
REMOVE_COUNT=$((NUM_KERNELS - KERNEL_LIMIT))

# Remove old kernels, skipping the running kernel
i=0
for KERNEL in $INSTALLED_KERNELS; do
  if [ "$i" -lt "$REMOVE_COUNT" ]; then
    if [[ "$KERNEL" != *"$(echo $CURRENT_KERNEL | cut -d'-' -f1)"* ]]; then
      echo "Removing kernel: $KERNEL"
      sudo dnf remove -y "$KERNEL"
    else
      echo "Skipping running kernel: $KERNEL"
    fi
  fi
  i=$((i+1))
done

echo "Finished removing old kernels."

Explanation:

  1. KERNEL_LIMIT: This variable defines how many kernels to keep on the system. The script will always keep the currently running kernel, so the limit represents the running kernel plus the number of backup kernels.
  2. CURRENT_KERNEL: Gets the version of the currently running kernel using uname -r.
  3. INSTALLED_KERNELS: Retrieves a list of all installed kernel packages using rpm -q kernel and sorts them in reverse order (newest first).
  4. NUM_KERNELS and REMOVE_COUNT: Calculates the total number of installed kernels and the number of kernels that need to be removed to meet the KERNEL_LIMIT.
  5. Loop: The script iterates through the list of installed kernels. Inside the loop:
    • It checks if the current iteration is less than the REMOVE_COUNT.
    • It compares the current kernel package name with the currently running kernel version. If the current kernel being checked is not the running kernel, it removes it using dnf remove -y. The -y flag automatically answers "yes" to the removal confirmation prompt.
    • If the kernel is the currently running kernel, it skips the removal.

How to Use:

  1. Save the script to a file, for example, remove_old_kernels.sh.
  2. Make the script executable: chmod +x remove_old_kernels.sh.
  3. Run the script: sudo ./remove_old_kernels.sh.

Important Considerations:

  • Testing: Thoroughly test this script in a non-production environment before using it on a live system.
  • Error Handling: The script lacks robust error handling. Consider adding checks for potential errors (e.g., dnf failing to remove a package).
  • Customization: Adjust the KERNEL_LIMIT variable to suit your needs.
  • Security: Ensure the script is owned by a trusted user and has appropriate permissions to prevent unauthorized modification.

Advantages:

  • Automation: Automates the process of identifying and removing old kernels.
  • Configurable: The KERNEL_LIMIT variable allows you to control the number of kernels to keep.

Disadvantages:

  • Risk of Errors: Incorrectly configured scripts can potentially remove important kernels or cause system instability. Requires careful testing.
  • Security Concerns: Scripts can introduce security vulnerabilities if not properly written and secured.
  • Less Granular Control: Doesn’t offer the same level of granular control as manually removing kernels.

By exploring these alternative methods, you gain a more comprehensive understanding of how to manage and remove old Linux kernels on AlmaLinux, enabling you to choose the approach that best suits your technical skills and system administration requirements. Remember to always back up your system before making significant changes.