So many kernel, how to remove most of them?

I wanted to know how many kernel i have to free some space ( more than a year and half now on UM) and with all the updates saying “removing kernel” i wanted to know how many kernels i had ( so the software updater didn’t really work at this level see the list below)

I did a dpkg --list | grep linux-image in terminal and the list below appears :fearful: (command found here https://www.ostechnix.com/check-installed-linux-kernels/ )


rc linux-image-4.10.0-27-generic 4.10.0-27.30~16.04.2 amd64 Linux kernel image for version 4.10.0 on 64 bit x86 SMP
rc linux-image-4.10.0-28-generic 4.10.0-28.32~16.04.2 amd64 Linux kernel image for version 4.10.0 on 64 bit x86 SMP
rc linux-image-4.10.0-30-generic 4.10.0-30.34~16.04.1 amd64 Linux kernel image for version 4.10.0 on 64 bit x86 SMP
rc linux-image-4.10.0-32-generic 4.10.0-32.36~16.04.1 amd64 Linux kernel image for version 4.10.0 on 64 bit x86 SMP
rc linux-image-4.10.0-33-generic 4.10.0-33.37~16.04.1 amd64 Linux kernel image for version 4.10.0 on 64 bit x86 SMP
rc linux-image-4.10.0-35-generic 4.10.0-35.39~16.04.1 amd64 Linux kernel image for version 4.10.0 on 64 bit x86 SMP
rc linux-image-4.10.0-37-generic 4.10.0-37.41~16.04.1 amd64 Linux kernel image for version 4.10.0 on 64 bit x86 SMP
rc linux-image-4.10.0-38-generic 4.10.0-38.42~16.04.1 amd64 Linux kernel image for version 4.10.0 on 64 bit x86 SMP
rc linux-image-4.10.0-40-generic 4.10.0-40.44~16.04.1 amd64 Linux kernel image for version 4.10.0 on 64 bit x86 SMP
rc linux-image-4.10.0-42-generic 4.10.0-42.46~16.04.1 amd64 Linux kernel image for version 4.10.0 on 64 bit x86 SMP
rc linux-image-4.13.0-26-generic 4.13.0-26.29~16.04.2 amd64 Linux kernel image for version 4.13.0 on 64 bit x86 SMP
rc linux-image-4.13.0-31-generic 4.13.0-31.34~16.04.1 amd64 Linux kernel image for version 4.13.0 on 64 bit x86 SMP
rc linux-image-4.13.0-32-generic 4.13.0-32.35~16.04.1 amd64 Linux kernel image for version 4.13.0 on 64 bit x86 SMP
rc linux-image-4.13.0-36-generic 4.13.0-36.40~16.04.1 amd64 Linux kernel image for version 4.13.0 on 64 bit x86 SMP
rc linux-image-4.13.0-37-generic 4.13.0-37.42~16.04.1 amd64 Linux kernel image for version 4.13.0 on 64 bit x86 SMP
rc linux-image-4.13.0-38-generic 4.13.0-38.43~16.04.1 amd64 Linux kernel image for version 4.13.0 on 64 bit x86 SMP
rc linux-image-4.13.0-39-generic 4.13.0-39.44~16.04.1 amd64 Linux kernel image for version 4.13.0 on 64 bit x86 SMP
rc linux-image-4.13.0-41-generic 4.13.0-41.46~16.04.1 amd64 Linux kernel image for version 4.13.0 on 64 bit x86 SMP
rc linux-image-4.13.0-43-generic 4.13.0-43.48~16.04.1 amd64 Linux kernel image for version 4.13.0 on 64 bit x86 SMP
ii linux-image-4.13.0-45-generic 4.13.0-45.50~16.04.1 amd64 Linux kernel image for version 4.13.0 on 64 bit x86 SMP
ii linux-image-4.15.0-24-generic 4.15.0-24.26~16.04.1 amd64 Signed kernel image generic
ii linux-image-4.15.0-29-generic 4.15.0-29.31~16.04.1 amd64 Signed kernel image generic
rc linux-image-4.8.0-36-generic 4.8.0-36.36~16.04.1 amd64 Linux kernel image for version 4.8.0 on 64 bit x86 SMP
rc linux-image-4.8.0-58-generic 4.8.0-58.63~16.04.1 amd64 Linux kernel image for version 4.8.0 on 64 bit x86 SMP
rc linux-image-extra-4.10.0-27-generic 4.10.0-27.30~16.04.2 amd64 Linux kernel extra modules for version 4.10.0 on 64 bit x86 SMP
rc linux-image-extra-4.10.0-28-generic 4.10.0-28.32~16.04.2 amd64 Linux kernel extra modules for version 4.10.0 on 64 bit x86 SMP
rc linux-image-extra-4.10.0-30-generic 4.10.0-30.34~16.04.1 amd64 Linux kernel extra modules for version 4.10.0 on 64 bit x86 SMP
rc linux-image-extra-4.10.0-32-generic 4.10.0-32.36~16.04.1 amd64 Linux kernel extra modules for version 4.10.0 on 64 bit x86 SMP
rc linux-image-extra-4.10.0-33-generic 4.10.0-33.37~16.04.1 amd64 Linux kernel extra modules for version 4.10.0 on 64 bit x86 SMP
rc linux-image-extra-4.10.0-35-generic 4.10.0-35.39~16.04.1 amd64 Linux kernel extra modules for version 4.10.0 on 64 bit x86 SMP
rc linux-image-extra-4.10.0-37-generic 4.10.0-37.41~16.04.1 amd64 Linux kernel extra modules for version 4.10.0 on 64 bit x86 SMP
rc linux-image-extra-4.10.0-38-generic 4.10.0-38.42~16.04.1 amd64 Linux kernel extra modules for version 4.10.0 on 64 bit x86 SMP
rc linux-image-extra-4.10.0-40-generic 4.10.0-40.44~16.04.1 amd64 Linux kernel extra modules for version 4.10.0 on 64 bit x86 SMP
rc linux-image-extra-4.10.0-42-generic 4.10.0-42.46~16.04.1 amd64 Linux kernel extra modules for version 4.10.0 on 64 bit x86 SMP
rc linux-image-extra-4.13.0-26-generic 4.13.0-26.29~16.04.2 amd64 Linux kernel extra modules for version 4.13.0 on 64 bit x86 SMP
rc linux-image-extra-4.13.0-31-generic 4.13.0-31.34~16.04.1 amd64 Linux kernel extra modules for version 4.13.0 on 64 bit x86 SMP
rc linux-image-extra-4.13.0-32-generic 4.13.0-32.35~16.04.1 amd64 Linux kernel extra modules for version 4.13.0 on 64 bit x86 SMP
rc linux-image-extra-4.13.0-36-generic 4.13.0-36.40~16.04.1 amd64 Linux kernel extra modules for version 4.13.0 on 64 bit x86 SMP
rc linux-image-extra-4.13.0-37-generic 4.13.0-37.42~16.04.1 amd64 Linux kernel extra modules for version 4.13.0 on 64 bit x86 SMP
rc linux-image-extra-4.13.0-38-generic 4.13.0-38.43~16.04.1 amd64 Linux kernel extra modules for version 4.13.0 on 64 bit x86 SMP
rc linux-image-extra-4.13.0-39-generic 4.13.0-39.44~16.04.1 amd64 Linux kernel extra modules for version 4.13.0 on 64 bit x86 SMP
rc linux-image-extra-4.13.0-41-generic 4.13.0-41.46~16.04.1 amd64 Linux kernel extra modules for version 4.13.0 on 64 bit x86 SMP
rc linux-image-extra-4.13.0-43-generic 4.13.0-43.48~16.04.1 amd64 Linux kernel extra modules for version 4.13.0 on 64 bit x86 SMP
ii linux-image-extra-4.13.0-45-generic 4.13.0-45.50~16.04.1 amd64 Linux kernel extra modules for version 4.13.0 on 64 bit x86 SMP
rc linux-image-extra-4.8.0-36-generic 4.8.0-36.36~16.04.1 amd64 Linux kernel extra modules for version 4.8.0 on 64 bit x86 SMP
rc linux-image-extra-4.8.0-58-generic 4.8.0-58.63~16.04.1 amd64 Linux kernel extra modules for version 4.8.0 on 64 bit x86 SMP
ii linux-image-generic-hwe-16.04 4.15.0.29.51 amd64 Generic Linux kernel image


So i wanted to know wich kernel i am currently using and did a uname -r and got
4.15.0-29-generic

Not knowing the differences between rc and ii and before doing something wrong…

I would like to remove all the possible kernels
But keep the last 3 or 4 as well as the one my system is using right now (just in case of problems and need to boot back on a previous kernel)

How i do that? (I am not a master of the terminal so something i can copy and past in it ? if possible)

Thanks for reading

The second letter at start of each line of output is the status. ‘i’ means installed. ‘c’ means configuration files remain (but kernel has been removed).

So, most of the items you list here are kernels already removed, but the configuration files are still there.

(Your command would not list any old ‘linux-signed-image’ files and and ‘linux-header’ files which might also be installed, so don’t forget those.)

To remove with Synaptic Package Manager always select ‘mark for complete removal’ as the removal option. This removes the configuration files also.

To remove old configuration files that remain behind, you can use a terminal command like this:

sudo apt-get purge linux-image-4.4.0-104-generic

and the config. files for this kernel will be removed even if the kernel itself is already gone.

2 Likes

Woww very good explainations,
Problem solved, thanks a lot!

1 Like

There was an old tool called “Ubuntu Tweak” which I am still able to use in 16.04. I know many of the Ubuntu diehards don’t like this tool but I find it very useful and easy to clean cache, cruft, and old kernels.

I installed it, but found nothing about kernel management/?

Go to the Janitor section. Its there.

I found ubuntu tweak master here.

Got some errors trying to compile it.

andy7_~/Downloads/ubuntu-tweak-master$ sudo python setup.py install
[sudo] password for andy:
Traceback (most recent call last):
File “setup.py”, line 2, in
from setuptools import setup, find_packages
ImportError: No module named setuptools

Mine is installed via deb in 16.04, not sure if it works with 18.04.

It doesn’t work in 18.04.

A lot of stuff has changed and some things are broken or buggy. :frowning:

!This will KEEP ONLY your current kernel!

#Check current kernel
$ uname -r

#Run this to list all packages:
$ dpkg -l|grep linux-.*-4|grep -v $(uname -r|cut -d’-’ -f1)|awk ‘{print $2}’

#Run this to remove them:
$ dpkg -l|grep linux-.*-4|grep -v $(uname -r|cut -d’-’ -f1)|awk ‘{print $2}’|xargs sudo dpkg -P

Enjoy

1 Like

The easiest way to remove kernels that will keep the current and the one before it (so two).

shell> sudo apt autoremove

or to auto say yes:

shell> sudo apt autoremove -y

That does not work when kernels are installed manually, correct ?
(tested it and that did not work for me)

$ sudo apt autoremove
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
$ dpkg -l|grep linux.*-4
ii  linux-headers-4.13.0-041300           4.13.0-041300.201709031731          all          Header files related to Linux kernel version 4.13.0
ii  linux-headers-4.13.0-041300-generic   4.13.0-041300.201709031731          amd64        Linux kernel headers for version 4.13.0 on 64 bit x86 SMP
ii  linux-headers-4.13.14-041314          4.13.14-041314.201711180632         all          Header files related to Linux kernel version 4.13.14
ii  linux-headers-4.13.14-041314-generic  4.13.14-041314.201711180632         amd64        Linux kernel headers for version 4.13.14 on 64 bit x86 SMP
ii  linux-headers-4.13.15-041315          4.13.15-041315.201711211030         all          Header files related to Linux kernel version 4.13.15
ii  linux-headers-4.13.15-041315-generic  4.13.15-041315.201711211030         amd64        Linux kernel headers for version 4.13.15 on 64 bit x86 SMP
ii  linux-headers-4.13.16-041316          4.13.16-041316.201711240901         all          Header files related to Linux kernel version 4.13.16
ii  linux-headers-4.13.16-041316-generic  4.13.16-041316.201711240901         amd64        Linux kernel headers for version 4.13.16 on 64 bit x86 SMP
ii  linux-image-4.13.0-041300-generic     4.13.0-041300.201709031731          amd64        Linux kernel image for version 4.13.0 on 64 bit x86 SMP
ii  linux-image-4.13.14-041314-generic    4.13.14-041314.201711180632         amd64        Linux kernel image for version 4.13.14 on 64 bit x86 SMP
ii  linux-image-4.13.15-041315-generic    4.13.15-041315.201711211030         amd64        Linux kernel image for version 4.13.15 on 64 bit x86 SMP
ii  linux-image-4.13.16-041316-generic    4.13.16-041316.201711240901         amd64        Linux kernel image for version 4.13.16 on 64 bit x86 SMP
$

Correct. Autoremove only removes kernels installed with standard updating and not manual installs.

I am not sure why you are manually installing so many kernels, but to address that issue you may use the following tool:

http://manpages.ubuntu.com/manpages/bionic/man1/purge-old-kernels.1.html

The above tool is deprecated but will get you started with more information

Ex:

sudo purge-old-kernels --keep 3 -qy

Will keep three kernels

Because i experience a kernel bug with 4.15 so i had to revert to 4.13 see: Installing 18.04 when systemd-udevd consumes 100% of CPU

If you have another option I’m all ears (please post other thread, i don’t want to hijack this one )

Well that is unfortunate you are dealing with a kernel bug.

A custom bash script that scans the installed kernels and removes all but say the current and last one wouldn’t be hard to program.

You are not hijacking this one at all,
So feel free to continue :smile:
and this conversation might help as well :wink:

@Patrice: Thanks i think i will :smile: