Home Linux Safely Remove Old Kernels form Debian Distributions

Safely Remove Old Kernels form Debian Distributions

by Lakindu Jayasena
7.1K views 6 mins read
Remove Old Debian Kernels

When you are using LVM installation or encrypted installations it uses a separate /boot partition to store everything required for the boot process. Normally the /boot partition is capable of holding only four or five kernels but in some systems having a limited amount of storage systems, the most frequent problem is that the /boot partition gets full quickly.

ShowDiskSpaceUsage

To prevent your /boot partition from getting full, you need to configure the automatic removal of old kernels, or manually remove old kernels regularly.

You can remove old kernels using the following command in a terminal:

sudo apt-get autoremove --purge

Note that the apt-get autoremove command will not remove all automatically installed old kernel providing packages as fallback versions are kept. Also, the list of kept kernels is maintained and automatically updated in the file /etc/apt/apt.conf.d/01autoremove-kernels as a list of matching regular expressions.

Errors when getting the /boot is full

The package manager cannot install a pending upgrade due to a lack of space. Additionally, apt-get can not remove a package due to broken dependency.

DependencyErrors

“Kernel Panic – not syncing: VFS: Unable to mount root fs on unknown-block(0,0)”

KernelPanicError

This problem can be fixed from the terminal by manually removing one or two old kernels and then we can provide the package manager enough space to install the queued upgrade. This article will show you how to remove old kernels in a manual way.

Steps to remove old kernels manually

Note: In this article, I’m performing all the operations inside the root login.
In Ubuntu 16.04 and earlier there may be leftover temporary files from previous kernel updates. First, remove it.

rm -rv ${TMPDIR:-/var/tmp}/mkinitramfs-*

Check the version number of the currently running kernel, which you DO NOT want to remove.

uname -r
4.4.0-124-generic

List all the kernels, excluding the booted one (4.4.0-124-generic in this example), in the package database and their statuses.

dpkg -l | tail -n +6 | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r)

## Status 'ii' means Installed and eligible for removal since it is old kernel
ListOldKernels

To free space in /boot we’ll remove an initrd.img file for a suitable old kernel manually, this is necessary due to a kernel packaging bug.

update-initramfs -d -k 4.4.0-87-generic
RemoveInitrd.imgFile

Now use dpkg in order to purge the kernel package for the same old kernel:

dpkg --purge linux-image-4.4.0-87-generic linux-image-extra-4.4.0-87-generic
RemoveoldKernelAndPackages

NOTE: The previous command will probably fail, as there probably is a depending linux-image-extra package installed together with a ‘generic’ kernel package. The output of dpkg tells the name of the package. Purge-it first.

Also, purge the respective header package and try also purging the common header package.

dpkg --purge linux-headers-4.4.0-87-generic
dpkg --purge linux-headers-4.4.0-87
RemoveHeaderPackages

Now it will remove the specified old kernel. Likewise, do the same manual process for old kernels until you free up some space on /boot partition. And once the /boot partition gets free, run the apt-get autoremove command and it will remove the rest of the old kernels automatically.

RunAutoremove

Finally, we will fix the package installation process that previously failed.

$ sudo apt-get -f install    ## Try to fix the broken dependency.

Related Articles

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.