Kali/Ubuntu Remove Old Kernels

Published by Torry Crass on

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!

Leave a Comment

%d bloggers like this: