Deleting the old kernels lost on your hard drive

Spring Cleaning

© Lead Image © Amy Walters, 123RF.com

© Lead Image © Amy Walters, 123RF.com

Author(s): , Author(s):

When you update the kernel, the old version remains on the disk. If you clean up, the reward is several hundred megabytes of free disk space.

All is flux; nothing stays still. This adage also applies to long-term Linux versions such as Ubuntu LTS and CentOS. The most important reasons for regular updates are security and troubleshooting. And the kernel develops, too. Recent patches for Spectre and Meltdown are good examples of important kernel changes that had to arrive through system updates.

But what happens to the old kernel after an update? First of all, the kernel is a normal software package. It is thus managed through the package management system for your Linux variant. In the case of a Debian or Ubuntu system, this means Apt; in the case of Red Hat-based systems, RPM and its friends Yum and DNF.

If you take a closer look, you'll see that there is no update for the kernel package: The package manager simply adds the new version to the system without deleting the previous version. Often several old versions will remain on the disk (see the box "Turning Back the Clock"), which requires a large amount of disk space.

Turning Back the Clock

The Linux kernel is responsible for many tasks, including managing hardware resources, enabling access to individual system components, and providing interfaces for system communication. Various hardware embodiments can sometimes impair this harmonious relationship. In the worst case, the system will not boot after a kernel update. If there are problems after the update, the old kernel might help, at least until the next kernel update. For this reason, most distributions keep one, but more likely several, of the previous kernel packages on disk.

This article shows how to remove old kernel images from your system. The reference systems are Debian 9, Fedora 28, Ubuntu LTS 16.04, and CentOS 7.5. By default, these distributions limit the number of kernel versions installed on the system – at least if they have be installed via a package update. For manually installed versions, or for additional kernel-related packages, the techniques described in this article might not apply (see the box entitled "Kernel-Related Packages").

Kernel-Related Packages

Kernel-related packages are packages with files that are directly related to the version of the kernel. These files include, for example, the kernel modules, the RAM disks used at boot time, the kernel headers, and sometimes a few add-ons that depend directly on the kernel version. Which of these components are in which packages is determined by the distribution.

The software manager remembers which packages are installed. For example, if you have installed the kernel headers, the new package with kernel headers will be installed on your disk each time the kernel is updated. The system keeps the previous version along with the actual kernel.

For the purposes of this article, the term "kernel package" refers to all packages belonging to the kernel, including kernel-related packages. As Figure 1 shows, these packages occupy a significant amount of disk space.

Figure 1: The kernel headers also need space (Debian).

Manual Removal

Handling the software installed on the system is the package manager's task. The basic procedure is similar for all distributions: First, determine the version of the running and working kernel. You need to be sure this current version stays on the system. Then display all kernel packages. The last step is to delete the packages that are no longer required. (Be sure you observe the instructions in the "Caution" box.)

Caution

Problems and data loss can occur when using the package management system. Therefore, make a backup before deleting old kernel versions.

Some distributions even let you delete the current kernel version. Please pay attention to the screen output. Do not delete the last working kernel. Uninstall older kernel versions only if the system is working perfectly.

Use the abstracted commands of the package manager, for example the yum command instead of rpm for CentOS. The abstracted commands display an overview before the actions are executed and let you cancel.

Determining the Current Version

The GRUB bootloader usually displays a selection of installed kernel versions at boot time. For Red Hat-based versions, this output is direct; for Debian the list appears in the Advanced Options area (Figure 2); for Ubuntu, you need to press the Shift key to see the list.

Figure 2: Debian provides a complete list of installed kernel versions in the Advanced Options area.

The uname -r command, which is available with all distributions, displays the version of the running kernel. The output for a Debian system is as follows:

$ uname -r
4.9.0-6-rt-amd64

Package Service

Distributions differ in the name and contents of kernel packages. Table 1 gives an overview of the commands you might need to determine the package names, organized by distribution. The commands listed in the table provide the complete names. The names of the packages alone indicate whether they are kernel packages or components.

Table 1

Determining Package Names

Distribution

Command

Examples of Package Names

CentOS

rpm -qa "kernel*"

kernel-3.10.0-862.9.1, kernel-headers-3.10.0-862.9.1, kernel-devel-3.10.0-862.9.1

Fedora

rpm -qa "kernel*"

kernel-4.16.3-301, kernel-modules-4.16.3-301, kernel-devel-4.16.3-301

Debian

apt list --installed linux-*

linux-image-4.9.0-6-amd64, linux-headers-4.9.0-4-amd64

Ubuntu LTS

apt list --installed linux-*

linux-image-4.15.0-30-generic, linux-headers-4.15.0-30-generic, linux-image-extra-4.15.0-15-generic

Making Space

Once you have completed the preparations, you can start removing the packages you no longer need. Thanks to the autocomplete function with the tab key, which also works when entering the package name, and the typical Linux copy-paste function with the middle mouse button, handling the long file names is no problem.

Table 2 shows some commands for deleting kernel packages. As with all active package management activities, you need root privileges on the system to delete kernel packages.

Table 2

Removing Kernel Packages

Distribution

Command

Sample Command

With Wildcard

CentOS

yum remove package_name

yum remove kernel-3.10.0-693.2.2 kernel-devel-3.10.0-693.2.2

yum remove "kernel*3.10.0-693*"

Fedora

dnf remove package_name

dnf remove kernel-4.16.16-300 kernel-core-4.16.16-300

dnf remove "kernel*4.16.16-300*"

Debian

apt purge package_name

apt purge linux-image-4.9.0-3

apt purge linux-*4.9.0-3-rt*

Ubuntu LTS

apt purge package_name

apt purge linux-image-4.9.0-3

apt purge linux-*4.9.0-3-rt*

The fact that the kernel packages within a distribution typically start with a uniform name, and that the commands allow wildcards, means that you can delete all the kernel packages of a version with a shortcut. The last column of the table shows some examples. If necessary, add more packages or wildcards to the call.

The commands shown in Table 2 provide an overview of the subsequent actions. Pay special attention to this output if you are using wildcards, and cancel the action if necessary. All commands let you specify multiple packages. Removing a kernel version also deletes the corresponding entry in the boot menu.

Some distributions draw "logical conclusions" and sometimes delete more packages than specified. For Fedora, deleting the kernel modules or kernel core packages also deletes the kernel (Figure 3).

Figure 3: In Fedora, deleting the modules will delete more packages.

Some of the distributions have their own tools for handling the kernel packages. The following sections describe a few of those tools.

Fedora and CentOS

By default, Fedora limits the number of installed kernel packages to three; CentOS sets the limit at five. The value of the variable installonly_limit determines the number. Fedora understands installonly packages to mean packages that can be updated via the package manager, but the old version is not deleted until this limit is reached.

Fedora sets this installonly_limit value in /etc/dnf/dnf.conf; CentOS sets the value in /etc/yum.conf. If you leave this value at 2 or higher, a fallback kernel always remains on the disk [1]. To edit these files, you need root privileges [2]. Be sure you make a backup copy before you make any changes.

Using the Package-config tool from the yum-utils package, CentOS gives you a simple approach to deleting previous kernel packages. The yum-utils package is installed by typing sudo yum install yum-utils. The sudo package-cleanup --oldkernels --count=2 command deletes all kernel packages except the last two.

Debian and Friends

The Synaptic program is part of many Debian-based distributions. It is installed by typing sudo apt install synaptic. Depending on the distribution, the call is either synaptic or with synaptic-pkexec. Alternatively, you can launch Synaptic via the application menu.

Here too, it is a good idea to first determine the version of the currently running kernel by typing uname -r. To quickly access the kernel packages, launch Synaptic and set the Status to Installed in the left part of the window, click to activate the area where the packages are listed, and type linux.

Mark the packages (*image*, *header*, etc.) of the old versions and select the entry Mark for complete removal in the context menu (right mouse button, Figure 5). Clicking on Apply starts the de-installation. See the box entitled "Debian Automatic" for additional information.

Figure 5: Graphical software management with Synaptic.

Debian Automatic

Where software requires further packages as dependencies (e.g., libraries for extended functions), the package manager automatically includes them when installing the application. If you remove the program from the system again, these additional "orphaned" packages often remain on the hard disk without serving any useful purpose.

The package managers on all Debian-based distributions (including Ubuntu or Linux Mint) have a routine with the sudo apt autoremove --purge that lets you remove this unnecessary ballast from the system. In addition to the orphaned packages, the command also cleans up the kernels (Figure 4). This automatic procedure keeps the current and the second-to-last kernel on the system.

If the routine ignores the obsolete kernels, it may be because they are tagged as "manually installed." The apt-mark showmanual | grep linux-image command lists the packages (and filters out the kernel packages immediately). If you then change the status by typing apt-mark auto package_name, the caretaker should now clean up the kernel packages as well.

Figure 4: The apt autoremove command removes unneeded packages from the hard disk on Debian and its descendants.

Conclusions

Automatic management of the kernel packages is typically fine for normal use, but automatic systems sometimes show some weaknesses. Manually installed kernel packages are usually not governed by the automatic mechanisms. If you manually install kernel packages on the disk, it makes sense to search the system for obsolete packages from time to time. If you need disk space in a hurry and can do without the previous kernel versions, removing large kernel packages will help in the short term.

The Author

Roman Jordan has been working with Linux for over 20 years. His main focus is on the Linux kernel and on programming small embedded platforms.