Skip to content

Instantly share code, notes, and snippets.

@spyesx
Last active December 30, 2023 16:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save spyesx/b4b1a8c4470f2893cac4e025fa6d332d to your computer and use it in GitHub Desktop.
Save spyesx/b4b1a8c4470f2893cac4e025fa6d332d to your computer and use it in GitHub Desktop.
Clean up old linux kernels

Remove old linux kernels

Script way

# Dry run to check
bash remove-old-kernels.sh

# Run
bash remove-old-kernels.sh exec

Debian way

# Which kernel are you using?
uname -r

# Which kernels are installed ?
dpkg --list | egrep 'linux-image|linux-headers'
  • ii – indicates packages that are currently installed
  • iU – package has been unpacked and will be used next reboot
  • rc – package already removed, but the configuration files are still present

autoremove alone remove older kernels only, but any package that is not needed as a dependency of other packages along with its configuration files.

# Will autoremove every packages that qualify for it
apt-get autoremove --purge

# Remove all configuration files that are still present despite the removed package
dpkg --list | egrep 'linux-image|linux-headers' | grep rc | awk '{print $2}' | xargs apt purge -y

# Remove and purge one particular package
# Be careful not to remove your current kernel !
yes | apt purge "linux-image-XXXXX"

autoremove will only remove packages that are automatically installed. If you ever updated or added a kernel package manually autoremove will not remove it. If you ever "held" a kernel version autoremove will not remove it. If you're wondering why Ubuntu is filling up your boot partition with kernels you no longer use it's likely one of these two reasons.

# Unhold all packages
dpkg --get-selections | grep hold | awk '{ print $1, "install" }' | dpkg --set-selections

# Mark all "manually installed" kernel packages as "automatically installed"
for f in $(apt-mark showmanual | grep linux-); do
    apt-mark auto $f
done

# Remove all packages that are no longer needed
apt-get -y autoremove --purge

Raspberry Pi

/boot/firmware could get full after an Ubuntu (for RPi) upgrade. Solution above still apply but there is also linux-firmware-* to consider.

# list "firmware" packages
dpkg --list | grep "firmware"
# Show where files are installed. To confirm they are well in /boot/firmware
package=linux-firmware-raspi
dpkg -L $package

# or to see which $file belongs to wich package (could be a long execution time)
dpkg -S $file

Find heavy files in /boot/firmware:

find /boot/firmware -size +8M -exec ls -lgd {} \;

Sometime you will find some *.bak files. If everything works well, then remove them.

rm -rf /boot/firmware/*.bak

Best, do not install Ubuntu on a RPi. This is a bad distribution.

More documentation

Dive into Ubuntu community documentation to remove old kernel

#!/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
# From:
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment