Kali/Ubuntu Remove Old Kernels

Published by Torry Crass on

UPDATE 20 SEP 2020: As it goes, this remains a problem as autoremove is simply not a well rounded solution for this. Yes, autoremove will remove old kernels, but it also removes other software and libraries that can still be in use. It doesn’t seem to perform all the proper dependency checks.

Likely it is best to download and use the old ‘purge-old-kernels’ script. I forked a copy from algodelinux that can be found here: https://github.com/torrycrass/purge-old-kernels

UPDATE 17 JUN 2019: Ubuntu has change the purge-old-kernels script to essentially no longer function citing that apt autoremove now functions as expected (per the note in the code below). So if you are/were using this script, it may not work as expected anymore. I do not yet know how this affects Kali or anyone who does not want to “autoremove” everything.

# apt autoremove supposedly works correctly now.
#
# Thus, we're deprecating the functionality previously found
# in purge-old-kernels.
#
# If you still have old kernels lying around after this, please
# file a bug against apt: https://bugs.launchpad.net/ubuntu/+source/apt/+filebug

sudo apt-get $@ autoremove

On with the show…


First, I should point out that upgrading Kali is often not recommended. I predict that if you do so regularly it will burn you, maybe not right away, but eventually it will burn you. Still, sometimes you may need or want to upgrade so we push boldly forward.

Over the years of using Kali I generally run into an upgrade problem once every 6 months or so. In some instances the problems brought on by upgrade are bad enough that I have to copy data off, smoke the whole system, and start over. One of the most common however is the boot partition filling up with old kernel data.

In many cases using a built-in apt directive “autoremove” is sufficient.

apt autoremove

(or in older systems use apt-get instead of apt)
(for ubuntu you will need to prepend commands with sudo)

However, with Kali, I’ve found this to cause problems as well so with Kali specifically, I avoid autoremove when I can. This lead me to find a program called byobu that will manage your kernel footprint much more easily than other options. You can install it as follows:

apt install byobu

Now it’s as simple as telling it how many kernel versions you’d like to keep. I recommend at least two. Given a recent kernel upgrade problem that left several of my systems unbootable because I failed to keep prior kernels, heed this advice.

purge-old-kernels --keep 2

If for some reason you cannot install the byobu script package or you really want to do this the old school way, another option is to slice, dice, remove, and purge your old kernel image and header packages. Below you’ll find (courtesy of:
https://askubuntu.com/questions/2793/how-do-i-remove-old-kernel-versions-to-clean-up-the-boot-menu) the command to remove all but the current kernel files using built in linux tools.

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 -y purge

Wishing you bootable systems post update!


0 Comments

Leave a Reply