1. Introduction
The Linux kernel is the core component of the Linux operating system (OS). Each kernel version may include bug fixes, security patches, and feature enhancements.
As Linux systems evolve, regular kernel updates are a norm. The updates are important for improving performance, adding features, and ensuring security. However, each update may leave older versions of the kernel as backup.
Over time, these old kernels can clutter the boot menu and consume valuable storage space. White it’s recommended to keep at least one previous version for fallback purposes, cleaning up unused kernels can help to maintain a clean system and an efficient boot process.
In this tutorial, we’ll explore how to safely remove old kernel versions for a more minimalistic boot menu.
2. Importance and Safety Precautions
Let’s look at some of the main advantages of removing old kernels:
- old kernels and their associated modules and headers consume storage space, so removing them can be significant in systems with limited storage
- it can also help to clear boot menu clutter since each kernel version appears as an option in the boot menu
- although old kernels don’t directly affect running performance, managing fewer kernels can simplify system maintenance and updates
To avoid issues, we mainly discuss approved methods to safely remove old versions. These mainly involve package managers, which act on the current bootloader in addition to the actual files.
Before removing old kernel versions, we should always back up important data. Furthermore, we should verify the current kernel version to avoid accidental removal of the active kernel version.
3. Identifying Current and Old Kernels
Before we remove old kernels, it’s essential to identify the versions currently installed on the system.
We can use the dpkg command to list all installed kernel versions:
$ dpkg --list | grep linux-image
rc linux-image-5.10.0-1044-oem 5.10.0-1044.46 amd64 Signed kernel image oem
rc linux-image-5.14.0-1038-oem 5.14.0-1038.42 amd64 Signed kernel image oem
rc linux-image-5.14.0-1042-oem 5.14.0-1042.47 amd64 Signed kernel image oem
rc linux-image-5.14.0-1044-oem 5.14.0-1044.49 amd64 Signed kernel image oem
rc linux-image-5.15.0-100-generic 5.15.0-100.110~20.04.1 amd64 Signed kernel image generic
[...]
Alternatively, we can use the uname command to check the current kernel version:
$ uname -r
5.15.0-107-generic
We need to be keen to avoid removing the current kernel or its associated headers and modules.
4. Removing Old Kernel Versions on Debian (Ubuntu)
Ubuntu and other Debian-based systems typically handle kernel updates automatically, but we can manually remove old kernels using the APT package manager.
4.1. Manual Removal
To manually remove kernels, we need to identify the kernel versions that we want to remove, and then use APT to purge them:
$ sudo apt-get purge linux-image-X.X.X-XX-generic
In this command, we replace X.X.X-XX with the version numbers of the kernels we want to remove.
Finally, after removing the kernels, we update the GRUB bootloader:
$ sudo update-grub
This command ensures the bootloader has the most current information. This way, we can see fewer options in the next system boot.
4.2. Automatic Removal
The APT package manager includes an auto-remove feature that can clean up unnecessary packages, including old kernels:
$ sudo apt autoremove --purge
This command removes packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed, including old kernels.
However, autoremove only removes kernels that were installed automatically. In case we install a kernel manually, this method might not remove it.
Alternatively, we can also install byobu. It’s a command-line tool which can simplify the cleanup process:
$ sudo apt-get install byobu
After installation, let’s remove the old kernels:
$ sudo purge-old-kernels
The purge-old-kernels command keeps the current and previous kernel, removing all other old kernels.
5. Removing Old Kernel Versions on RedHat or Fedora (CentOS)
RedHat-based distros, including CentOS and Fedora, use YUM or DNF for package management.
5.1. Manual Removal
Let’s first start with listing installed kernels using rpm:
$ rpm -qa | grep kernel
Next, we can identify all the kernels that we want to remove and then purge them:
$ sudo yum remove kernel-X.X.X-XX.elX.x86_64
Of course, the command is similar for DNF:
$ sudo dnf remove kernel-X.X.X-XX.elX.x86_64
We should replace X.X.X-XX.elX with the version numbers of the kernels we want to remove.
Finally, we can refresh the GRUB bootloader to reflect the changes:
$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg
Next, let’s look at how to automate the cleanup of obsolete kernels.
5.2. Automatic Removal
Fedora and similar distros have built-in tools to manage old kernels. For example, DNF has a configuration setting to limit the number of kernels kept on the system.
To configure DNF to limit kernels, we open the configuration file /etc/dnf/dnf.conf and then add or modify the installonly_limit line:
This setting keeps the current kernel and the last two.
Next, we run a cleanup:
$ sudo dnf autoremove
This command enforces the new settings, removing old kernels and other unneeded packages.
6. Cleaning Up GRUB Manually
Sometimes, we may need to clean up the GRUB boot menu entries manually. However, the process can vary depending on the distro and GRUB version.
In most cases, the first step is to edit the GRUB configuration files in the /etc/default/grub directory or /boot/grub2/grub.cfg directory.
Next, we carefully edit the configuration files to remove entries for the old kernels. We should be cautious to avoid deleting wrong entries, as it can render the system unbootable.
We then update GRUB:
$ sudo update-grub
Alternatively, we can use another command for GRUB2:
$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg
This should clear all the unwanted boot menu entries. Usually, this isn’t the preferred approach, due to the danger involved.
7. Conclusion
In this article, we explored how to remove old kernel versions in different Linux distros to maintain a clean boot menu.
Managing old kernel versions is important for maintaining a clean, efficient, and secure Linux system. By regularly removing old kernels, we can simplify the boot menu, free up storage space, and reduce the potential for system management errors.
It’s good practice to back up data and verify the current kernel version before making changes.