How to remove unused kernels in linux

How to remove unused kernels in linux

This is a short post on how to remove unused kernels in linux. I’m adding it here because I use it regularly and keep forgetting the commands – it might help you too!

Removing kernels manually
firstly, check your running kernel version


uname -a

next, show all the installed kernels:


rpm -q kernel

You can now remove each kernel manually by typing in: yum remove kernel-3.10.0 etc

Removing old kernels automatically
Fortunately there is a quicker way to remove old kernels (this is the part that I always forget!) package-cleanup is available from the yum-utils package

Firstly, make sure you have yum-utils installed


yum install yum-utils

Once yum-utils is installed you can run the following command to remove all old kernels (and keep one)


package-cleanup --oldkernels --count=1

Configure yum to only keep one kernel instead of 5
There is a flag in the /etc/yum.conf file that you can set to change the number of kernels that centos will keep around – its set to 5 by default, but you can change it to 1 and it will only keep 1 – the flag is installonly_limit=5