11

I have old Linux kernel versions that I don't use so I was trying to remove them.

List of installed kernels from dpkg --list | grep linux-image

linux-image-5.4.0-26-generic (5.4.0-26.30)   
linux-image-5.4.0-33-generic (5.4.0-33.37)
linux-image-5.4.0-37-generic (5.4.0-37.41)
5
  • 2
    Does sudo apt autoremove help? Which kernels are you asking about? – Pilot6 Jun 24 '20 at 12:31
  • 1
    Have you tried sudo apt autoremove? Please edit your question and add all the new information. Also indicate if you installed the kernels manually or something special. – user68186 Jun 24 '20 at 12:33
  • 1
    These are regular Ubuntu kernels. autoremove should remove them. – Pilot6 Jun 24 '20 at 12:48
  • Hmmm. Show us complete output. dpkg --list output includes kernels that have already been removed. The first column will indicate which packages are removed (rc) and which are installed (ii). – user535733 Jun 24 '20 at 13:17
  • There are a couple of excellent kernel removal manager type scripts around. I used the server version from here, for a long time, but now use this one. Note that I can not use autoremove, as it misbehaves when one uses mainline kernels. The manual method becomes tedious when trying to clean up 100 kernels. – Doug Smythies Jun 24 '20 at 13:48
18

Here are the steps to remove unused kernels.

Check what current kernel You run:

uname -a
Linux blackhole 5.6.13-050613-lowlatency #202005141310 SMP PREEMPT Thu May 14 13:17:41 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

I am running 5.6.13-050613-lowlatency

List all installed kernels in Your OS:

dpkg --list | egrep -i --color 'linux-image|linux-headers|linux-modules' | awk '{ print $2 }'
linux-headers-5.6.11-050611
linux-headers-5.6.11-050611-lowlatency
linux-headers-5.6.13-050613
linux-headers-5.6.13-050613-lowlatency
linux-image-unsigned-5.6.11-050611-lowlatency
linux-image-unsigned-5.6.13-050613-lowlatency
linux-modules-5.6.11-050611-lowlatency
linux-modules-5.6.13-050613-lowlatency

Uninstall kernels You don't need:

sudo apt purge linux-headers-5.6.11-050611  linux-headers-5.6.11-050611-lowlatency linux-image-unsigned-5.6.11-050611-lowlatency linux-modules-5.6.11-050611-lowlatency
6
  • dpkg --list | egrep -i --color 'linux-image|linux-headers|linux-modules' | awk '{ print $2 }'. Really? Doesn't ls -1 /boot/vm* list your kernels including the current one? – Paul Benson Jun 24 '20 at 14:31
  • @PaulBenson Your command does not list packages. So You can't use these names in apt or dpkg to uninstall kernels. – Michal Przybylowicz Jun 24 '20 at 16:32
  • 3
    Isn't there an automated way to do that? I have more than 50 kernels installed in the list, many with un tag. – staticdev Jul 27 '20 at 15:01
  • While this seems tedious, it appears to be the only viable way. I would have loved to do it automatically, but I am unable to find a nice script for that. My hacky workaround is to use sudo apt-get purge linux-*-*-5.* (feel free to remove one -* to catch some other modules and do the same with 4.*) and manually check the output. This will get the desired packages, but might be too aggressive - i.e. check the output carefully to avoid a system suicide. – Betaminos Apr 13 at 7:51
  • Sorry for the double comment, StackExchange won't let me edit the comment after the 5 min mark. I have found this nice script: tecmint.com/remove-old-kernel-in-debian-and-ubuntu (i.e. purge-old-kernels as a script included in the package byobu). This might help, but I have already purged all old kernels so can't test it for now. – Betaminos Apr 13 at 8:09
5

Just use this:

sudo apt-get autoremove --purge
3
  • 5
    Note that this command won't remove older kernels only, but any package that is not needed as a dependency of other packages along with its configuration files. – BeastOfCaerbannog Jan 8 at 12:07
  • @BeastOfCaerbannog, sure, thanks for the clarification. – Ihor Romanyshyn Jan 11 at 10:24
  • 4
    It doesn't work for 20.04 (upgraded from 18.04, and upgraded from 16.04). I can list the packages, but ubuntu doesn't autoremove old kernels. – gavioto20 Jan 12 at 17:31
2

To easily remove older versions kernels, e.g. kernels starting from 4.0 and so on.

sudo apt-get purge linux-image-4.*
2
  • Are you sure, that linux-image-4.* will remove "old" linux-image-5.4.xxx kernel images? – Simon Sudler Aug 30 '20 at 9:34
  • 3
    Ofcourse not, how do you expect that to remove kernels with a starting 5. You can update command accordingly. I just gave an example. For linux-image-5.4.xxx use sudo apt-get purge linux-image-5.4.* and so on. – shivam singh Sep 4 '20 at 7:14
2

You can try out this script

remove_old_kernels.sh

#!/bin/bash
# Run this script without any param for a dry run
# Run the script with root and with exec param for removing old kernels after checking
# the list printed in the dry run

uname -a
IN_USE=$(uname -a | awk '{ print $3 }')
echo "Your in use kernel is $IN_USE"

OLD_KERNELS=$(
    dpkg --list |
        grep -v "$IN_USE" |
        grep -Ei 'linux-image|linux-headers|linux-modules' |
        awk '{ print $2 }'
)
echo "Old Kernels to be removed:"
echo "$OLD_KERNELS"

if [ "$1" == "exec" ]; then
    for PACKAGE in $OLD_KERNELS; do
        yes | apt purge "$PACKAGE"
    done
fi

Run it like this for a dry run:

remove_old_kernels.sh

If all looks good, run it again like this:

sudo remove_old_kernels.sh exec
-3

I wanted to delete 5.4.0-26 kernel, and tried apt, but apt is getting bad at doing things, unlike in 18.04, where apt purge of the kernel version used to delete all the related packages.

But apt-get still does it great.

sudo apt-get purge <older_linux_version_number>

Example: sudo apt-get purge 5.4.0-26

and if you are so very sure of deleting the old kernel and its related packages, then say yes, using -y

sudo apt-get purge <older_linux_version_number> -y

Example: sudo apt-get purge 5.4.0-26 -y

you can delete multiple such old kernels in one go, just by passing multiple kernel versions separated by space.

sudo apt-get purge <older_linux_version1> <older_linux_version2> <older_linux_version3> -y

Example: sudo apt-get purge 5.4.0-26 5.4.0-28 5.4.0-32

6
  • 1
    This a bad way of using a pattern to remove packages. It will remove all packages containing the pattern. Running it with -y is a potential suicide. – Pilot6 Aug 7 '20 at 9:00
  • you see, -y is a legitimate option for apt-get, and anything could be potentially suicidal, say 'rm' is suicidal if you don't know what you are removing, and rm with '-rf' could be deadly, lest you know what you are deleting. So, forget about -y being suicidal, but yes, removing the linux kernel packages related to the pattern is very much valid, the only vulnerability i can see of, is that if there are any non-linux kernel related packages with the same version pattern name, it deletes those too, but apt did this in older release of ubuntu. This is not something first. – Sundeep471 Aug 8 '20 at 1:18
  • 1
    The problem that you don't specify that you want to remove kernels, but any package containing specific numbers. You never know what will be removed. And also the statement that apt is bad in doing this is wrong. – Pilot6 Aug 8 '20 at 5:02
  • Perfect, and that can easily be fixed with "apt-get purge linux-<version>" – Sundeep471 Aug 8 '20 at 18:46
  • You can use apt as well. – Pilot6 Aug 8 '20 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.