User Tools

Site Tools


ubuntu:packages:remove_kernels

Ubuntu - Packages - Removed Unused Kernels

Ubuntu does not remove kernels when they install a new one, however the /boot partition is usually relatively small.

So after only a few kernel upgrades, you can get No Space Left On Device errors when trying to upgrade.

To determine which kernels can be removed run the following:

sudo dpkg --get-selections | grep 'linux-image.*-[0-9].*' | awk '{print $1}' | egrep -v "$(uname -r)"| while read n; do echo $n; done

To delete all these old kernels run the following.

NOTE: This will only keep the very latest kernel and will delete all others, so may not be what you want:

sudo dpkg --get-selections | grep 'linux-image.*-[0-9].*' | awk '{print $1}' | egrep -v "$(uname -r)"| while read n; do apt-get -y remove $n; done

Another Method

Either remove them manually, or use this one liner to automatically remove them all.

export KERNEL="$(uname -r | grep -Po '([0-9\.\-]*[0-9])?')"; dpkg --get-selections | grep -E "linux-(header|image).*" | grep -iw install | sort | grep -v "$KERNEL" | grep -v "lts" | sed 's/install//g' | xargs dpkg -P

Here's the command by command explanation:

export KERNEL="$(uname -r | grep -Po '([0-9\.\-]*[0-9])?')"

The first portion sets the current kernel number in a variable KERNEL. It only takes the number, and greps out any additions like -generic or -server.

sudo dpkg --get-selections 

The second portion first prints out all available packages.

grep -E "linux-(header|image).*"

The third portion greps for all packages with either linux-header or linux-image in the name.

grep -iw install

The fourth portion greps out only installed packages.

sort

The fifth portion sorts the output.

grep -v "$KERNEL" | grep -v "lts"

The sixth portion filters out the current kernel and the LTS kernel package. Removing those will cause problems.

sed 's/install//g'

The seventh part strips off the install part.

xargs dpkg -P

The last part actually removes the packages. xargs send all the package names to dpkg. Then dpkg -P purges the packages. That means, removing them and removing their configs.

ubuntu/packages/remove_kernels.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki