4

How can I automatically delete all other kernels which I'm not using?

So, here's my situation:

  • Using different versions of Ubuntu / CentOS
  • Automatic updates were on -> Linux downloaded many new kernel versions
  • Now I want to delete all other kernel versions, except the one I'm currently using

Code:

#find out current kernel version: uname -r #check which versions are downloaded to computer dpkg --get-selections | grep linux-image #delete desired kernel sudo apt-get purge [KERNEL] #autoremove sudo apt-get autoremove 

There can be easily more than 15 kernels in one linux, and I have more than 50 linuxes which I have to go through.

1

4 Answers 4

2

On Ubuntu you can use purge-old-kernels to purge old kernels , to install it:

For ubuntu 16.04

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F430BBA5 sudo add-apt-repository ppa:byobu/ppa sudo apt-get update sudo apt-get install byobu 

Also you can install it without adding the ppa: sudo apt-get install byobu

For Ubuntu versions lower than 16.04

sudo add-apt-repository ppa:bikeshed/ppa sudo apt-get update sudo apt-get install bikeshed 

Run the following command to keeping the latest 2 kernel :

sudo purge-old-kernels 

You can specify the number n of kernel to keep:

sudo purge-old-kernels --keep n 

n=1:

sudo purge-old-kernels --keep 1 

On centOS install yum-utils package :

yum install yum-utils 

To keep the latest n kernels , run:

package-cleanup --oldkernels --count=n 

n=1:

package-cleanup --oldkernels --count=1 
1
  • 1
    The Centos no longer works with Centos 8 onwards. Gives the error message package-cleanup has to be executed with one of the options: --dupes, --leaves, --orphans, --problems or --cleandupes Refer my answer below for a working solution for Centos 8 Commented Mar 19, 2021 at 19:42
2

For Centos 8 onwards you can use the command:

dnf remove --oldinstallonly 

This removes all old versions of the kernel. Use when you want to have only the latest release version of the kernel installed.

My original answer at: https://unix.stackexchange.com/a/628434/145515

1

Be aware, this command removes all kernel images and headers of major release 4, but not the running one.

apt purge $(dpkg-query -W -f='${binary:Package}\n' 'linux-image-4*' 'linux-headers-4*' | grep -v $(uname -r)) 
0

You can try the below command. Before you execute apt-get purge, just double check the output of your grep to not contain your current kernel version

dpkg --list | grep linux-image | awk '{print $2}' | grep -v linux-image-`uname -r` | xargs apt-get purge 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.