Each installed Linux kernel occupies a significant amount of disk space. Over time, as you update your kernel to newer versions, older kernels can accumulate on your system, taking up valuable disk space. By removing old kernels, you can free up disk space, allowing for more efficient utilization and preventing potential storage shortages. In this guide, we will learn how to find and remove old unused Kernels in Linux operating systems.
DISCLAIMER: The following instructions for deleting old unused Linux kernels involve operations that can significantly affect your system. Incorrect usage of these commands can lead to system instability or data loss. It's strongly advised to:
Remember, you bear the full responsibility for the results of following these instructions. Proceed with caution and at your own risk.
Table of Contents
All installed Linux kernels and their associated files are typically stored under the /boot directory. And the Kernel modules are usually stored in /lib/modules directory.
The /boot directory contains the files related to the Linux kernel, including the kernel images, initramfs files, and bootloader configurations. The kernel files typically have names starting with "vmlinuz-" or "vmlinuz." and end with the version number.
The /lib/modules directory stores the kernel modules associated with each installed kernel. Kernel modules are dynamically loadable components that extend the functionality of the Linux kernel. Each kernel version has its own subdirectory within /lib/modules/.
By default, most recent Linux distributions retain the current Linux kernel version and one previous version for rescue purposes. This ensures that if any issues arise after upgrading to a new kernel version, you can easily boot into the older, known-working kernel.
However, there are instances where you might have manually installed Linux kernels or installed a specific Kernel from third-party repositories. In such cases, the older kernels will not be removed automatically when running package cleaning commands such as sudo apt autoremove.
Consequently, these older versions continue to accumulate in the /boot directory, gradually occupying more disk space over time. Therefore, it becomes necessary to identify and delete these old, unused Linux kernels to free up valuable disk space.
Debian and its derivatives, along with RHEL and its variants, are among the most prevalent operating systems in use today. Therefore, we will delve into the process of removing Linux kernels specifically within these two categories of operating systems.
First, we will see how to remove old, unused Kernels in Debian-based systems.
There are a few ways to delete old Kernels in Debian, Ubuntu and its derivatives such as elementary OS, Linux Mint, and Pop!_OS.
First, you need to verify the list of all installed Linux Kernels. To check all installed Kernels in Debian, Ubuntu and its derivatives, run:
$ dpkg --list | grep linux-image
Sample Output from my Ubuntu 22.04 system:
rc linux-image-5.15.0-25-generic 5.15.0-25.25 amd64 Signed kernel image generic rc linux-image-5.15.0-30-generic 5.15.0-30.31 amd64 Signed kernel image generic rc linux-image-5.15.0-39-generic 5.15.0-39.42 amd64 Signed kernel image generic ii linux-image-5.15.0-41-generic 5.15.0-41.44 amd64 Signed kernel image generic ii linux-image-5.19.0-42-generic 5.19.0-42.43~22.04.1 amd64 Signed kernel image generic ii linux-image-generic-hwe-22.04 5.19.0.42.43~22.04.15 amd64 Generic Linux kernel image
As you may noticed in the output, the first column displays "rc" and "ii". You might wonder what are those.
The "ii" and "rc" are package status indicators. Here's what they represent:
So, in the above output, the packages with "ii" status (linux-image-5.15.0-41-generic and linux-image-5.19.0-42-generic) are currently installed on my system. On the other hand, the packages with "rc" status (linux-image-5.15.0-25-generic, linux-image-5.15.0-30-generic, and linux-image-5.15.0-39-generic) have been removed, but their configuration files are still present.
Heads Up: The package status information provided here is specific to the output of the dpkg --list command, where the first column indicates the package status.
Now, let us delete old and unused Linux Kernels in Debian-based systems using the following command:
$ sudo apt-get autoremove --purge
The command sudo apt-get autoremove --purge is used to remove automatically installed packages that are no longer needed on your system, including their configuration files. Here's what each part of the command does:
When you run sudo apt-get autoremove --purge, the command scans your system for packages that were installed as dependencies but are no longer needed. It then removes those packages and their associated configuration files, freeing up disk space and cleaning up the system from unnecessary packages.
It is important to review the list of packages to be removed before confirming the command execution. This ensures that no critical packages are unintentionally removed.
Now let us check again the list of available Kernels with command:
$ <strong>dpkg --list | grep linux-image</strong> rc linux-image-5.15.0-25-generic 5.15.0-25.25 amd64 Signed kernel image generic rc linux-image-5.15.0-30-generic 5.15.0-30.31 amd64 Signed kernel image generic rc linux-image-5.15.0-39-generic 5.15.0-39.42 amd64 Signed kernel image generic ii linux-image-5.15.0-41-generic 5.15.0-41.44 amd64 Signed kernel image generic ii linux-image-5.19.0-42-generic 5.19.0-42.43~22.04.1 amd64 Signed kernel image generic ii linux-image-generic-hwe-22.04 5.19.0.42.43~22.04.15 amd64 Generic Linux kernel image
As I already said, the "rc" packages are removed but their configuration files are present in the system. Why the 'sudo apt autoremove' command didn't remove them? You might wonder.
The "rc" packages are not removed when you the "sudo apt autoremove --purge" command because the "autoremove" operation specifically targets packages that were installed as dependencies but are no longer needed by any other packages on your system.
The "rc" status indicates that the package has been removed but its configuration files are still present on the system. When you run "apt autoremove --purge", it only removes packages that are marked as "auto" or "orphans", meaning they were installed automatically as dependencies and are no longer required.
Since "rc" packages are not considered as dependencies and do not fall into the category of "auto" or "orphans", they are not automatically removed by the "apt autoremove" operation.
In this case, you can manually find and remove the unneeded Kernels as described in the following section.
To remove old unused Linux kernels, you can follow these steps:
1. Check your current kernel version
Open a terminal and run the command:
$ uname -r
This will display the currently running kernel version. Make sure you don't remove this kernel.
2. List installed kernels
Run the following command to see the list of installed Linux kernels on your system.
$ dpkg --list | grep linux-image
3. Identify unused kernels
Review the list and identify the kernels you want to remove. Typically, you would want to keep the latest kernel and perhaps one or two previous versions as a backup.
4. Remove unused kernels
To remove a specific kernel, use the command:
$ sudo apt-get purge linux-image-x.x.x-x-generic
Where "x.x.x-x" represents the kernel version. Replace it with the actual version number you want to remove. Repeat this command for each kernel you want to uninstall.
4. Update the GRUB boot loader
After removing the old kernels, you should update the GRUB boot loader configuration to reflect the changes. Run the following command to update the boot loader.
$ sudo update-grub
5. Reboot your system
It's a good practice to reboot your system to ensure that everything is working correctly with the remaining kernel.
Please exercise caution when removing kernels, as removing the wrong kernel can render your system unbootable. Always double-check the version you want to remove and keep a backup kernel as a precaution.
Please note that we have only removed the unused old Kernels. There could be many unneeded package configuration files might still exist in the system. To remove them all, please follow the steps below.
To remove all "rc" packages, you can use the following command:
$ sudo dpkg --purge $(dpkg -l | awk '/^rc/{print $2}')
This command will purge (remove) all packages with the "rc" status on your system. However, exercise caution when using this command and ensure that the packages you are removing are no longer needed, as they might contain important configuration files.
Now let us verify the list of installed Kernels using command:
$ <strong>dpkg --list | grep linux-image</strong> ii linux-image-5.15.0-41-generic 5.15.0-41.44 amd64 Signed kernel image generic ii linux-image-5.19.0-42-generic 5.19.0-42.43~22.04.1 amd64 Signed kernel image generic ii linux-image-generic-hwe-22.04 5.19.0.42.43~22.04.15 amd64 Generic Linux kernel image
See? This time we get only the installed Linux Kernels. The Linux Kernels marked as "rc" are gone!
We now have two installed Kernels in our Ubuntu system, the currently running Kernel and one previous version acting as backup Kernel. What if you want to keep only the current Kernel and delete everything else? You can use the following one-liner commands.
Command 1:
To retain only the currently running Linux Kernel and and remove all other old, unused Kernels in Debian-based systems, run this:
$ echo $(dpkg --list | grep linux-image | awk '{ print $2 }' | sort -V | sed -n '/'`uname -r`'/q;p') $(dpkg --list | grep linux-headers | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p') | xargs sudo apt-get -y purge
Let us break down the above command and see what each option does.
In summary, the command identifies older versions of installed Linux kernel image and header packages, and then performs a purge operation to remove them using apt-get.
It helps to automatically remove old, unused kernels from the system. However, it is essential to exercise caution and verify the list of packages to be removed before executing the command to avoid removing any critical kernels.
Command 2:
Below is yet another one-liner command to keep only the currently running Linux Kernel and delete all other Kernels.
$ sudo apt-get purge $(for tag in "linux-image" "linux-headers"; do dpkg-query -W -f'${Package}\n' "$tag-[0-9]*.[0-9]*.[0-9]*" | sort -V | awk 'index($0,c){exit} //' c=$(uname -r | cut -d- -f1,2); done)
This command will remove old unused Linux kernels and their associated headers using the apt-get purge command. Let's break down what the code does:
In summary, this command removes all the old Linux kernel and header packages that have a version number lower than the currently running kernel. It's a more automated way of removing unused kernels without having to manually specify each version number.
Here are the steps to remove old unused kernels in RHEL, Fedora and RHEL-based systems such as CentOS, AlmaLinux and Rocky Linux.
1. Update your system:
Before removing old kernels, it's recommended to update your system's packages and active kernel. You can do this by running the following command:
$ sudo dnf update
This command will update all packages and the active kernel in your system.
2. Check the current kernel version:
To avoid accidentally removing the kernel you're currently using, you should check your current kernel version. You can do this with the following command:
$ uname -r
This command will display the kernel version.
If you want to display both the kernel name and version, add -s flag.
$ uname -sr
3. List all installed kernels:
To see all the kernels that are installed on your system, use the following command:
$ sudo rpm -q kernel
This command will list all the kernels installed on your system
4. Remove old kernels:
You can use the DNF command to remove old unused kernels. The following command will keep the latest two kernels (assuming the latest one is active) and remove all the others:
$ sudo dnf remove --oldinstallonly --setopt installonly_limit=2 kernel
Here's a breakdown of the command:
So, to summarize, this command will remove all but the 2 most recent versions of the kernel that were installed via the installonly command. It's a way to clean up old kernel versions that you're no longer using.
Be aware that this command only works if the latest kernel is active. If an older version is functional, the command will unsuccessfully try to remove it.
If you're using an older version of CentOS/RHEL, you can use the package-cleanup utility from the yum-utils package to remove old kernel versions.
First, make sure yum-utils package is installed.
$ sudo yum install yum-utils
Now run the following command below to keep the latest 2 kernels and remove the rest.
$ sudo package-cleanup --oldkernels --count=2
This method has stopped working with the release of CentOS 8, but it still works for older versions of CentOS/RHEL.
Please remember to proceed with caution when performing these steps. It's always a good idea to keep at least one or two old kernels as a backup in case there's an issue with the current one. Always verify the kernel versions and ensure that your current kernel is not removed. It's also recommended to perform a system backup before making these changes.
Here's a few FAQs about removing unnecessary Linux kernels.
Q: Why should I remove old kernels from my system?A: Keeping old kernels can take up disk space. Removing old kernels is essential for optimizing disk space, enhancing system performance, improving security, and simplifying maintenance. It helps prevent storage shortages, ensures up-to-date security patches, streamlines the boot process, and simplifies system management. While it's a good practice to keep a couple of older versions as backup, you can remove the rest to free up some space.
Q: How do I know which kernel I'm currently using?A: You can check your current kernel version by running the command uname -r in the terminal. This command will display the kernel name and version.
Q: How can I identify the installed Linux kernels on Debian-based systems?A: There are several ways to identify installed Linux kernels:1. Use the command dpkg --list | grep linux-image to list installed kernel packages.2. Check the /boot directory for files starting with "vmlinuz-" or "vmlinuz." followed by the kernel version.3. Run the command uname -r to display the version of the currently running kernel.
Q: How can I see all the kernels installed on RHEL-based systems?A: To list all the kernels installed on your system, you can use the command sudo rpm -q kernel. This command will list all the kernels installed on your system.
Q: Can I use sudo apt autoremove --purge to remove old kernels on Debian-based systems?A: No, sudo apt autoremove --purge typically removes automatically installed packages that are no longer needed but does not specifically target old kernels. It may not remove "rc" packages, which are removed but still have configuration files present.
Q: What is the command to remove old kernels on my RHEL or CentOS system?A: You can use the command sudo dnf remove --oldinstallonly --setopt installonly_limit=2 kernel to remove all but the 2 most recent versions of the kernel. Please note that this command only works if the latest kernel is active. If an older version is functional, the command will unsuccessfully try to remove it.
Q: I'm using an older version of CentOS/RHEL, is there a different command to remove old kernels?A: Yes, if you're using an older version of CentOS/RHEL, you can use the package-cleanup utility from the yum-utils package to remove old kernel versions. The command sudo package-cleanup --oldkernels --count=2 will keep the latest 2 kernels and remove the rest.
Q: What do the package status indicators "ii" and "rc" mean in the output of dpkg --list?A: In the output, "ii" stands for "installed, and successfully installed/configured," indicating an installed and functional package. "rc" stands for "removed, but the configuration files are still present," indicating a removed package with configuration files remaining.
Q: What precautions should I take before removing old kernels?A: It's always a good idea to keep at least one or two old kernels as a backup in case there's an issue with the current one. Always verify the kernel versions and ensure that your current kernel is not removed. It's also recommended to perform a system backup before making these changes.
Managing Linux kernels effectively is a crucial aspect of system administration. Over time, as systems are updated and new kernel versions are installed, it's common for old and unused kernels to accumulate. These old kernels can take up valuable disk space and, in some cases, could pose a security risk if they contain vulnerabilities. So it is recommended to clean up older and unused Kernels once in a while.
However, it's essential to approach the task of removing old kernels with caution. While freeing up disk space is beneficial, each kernel serves as a fallback if the current version fails or encounters issues. Therefore, it's recommended to always keep at least one or two old kernels as a safety measure.
As always, prior to making significant changes to your system, ensure you have a current backup and understand the implications of the commands you're executing. If done correctly, the process of removing old kernels can contribute to a more efficient and secure Linux environment.
Reference:
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!