Kernel Version and Disk Space Management

Have you received a system message stating that your /boot directory space is running low or out of space during a kernel upgrade using your system’s package manager? Or maybe you want to avoid seeing this message by abiding by good housekeeping rules on your system? The following chore can help you accomplish these goals.

From the command line, choosing to purge the below instance will handle all associated files in your /boot directory, eliminating the need to manually track down elements and remove them. This will likely leave remnants in locations on your system that you are not very familiar with, making this a much better way of manually purging old kernels:

sudo apt-get purge linux-image-X.X.X-X
Example: sudo apt-get purge linux-image-4.4.0-131

Once the command completes, you will notice the kernel package and associated files will be removed from the /boot directory. To keep your GrUB menu congruent with the kernels you wish to curate for whatever reason, run this command next:

sudo update-grub

The output from running this command should reflect the kernels you decided to keep, which will look similar to this:

Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.4.0-141-generic
Found initrd image: /boot/initrd.img-4.4.0-141-generic
Found linux image: /boot/vmlinuz-4.4.0-139-generic
Found initrd image: /boot/initrd.img-4.4.0-139-generic
Found memtest86+ image: /memtest86+.elf
Found memtest86+ image: /memtest86+.bin
done

Since this type of system management is a task that you will probably want to run at a regular frequency (30, 45, 60 days?) to keep your /boot directory well maintained, you could always create a shell script that can be scheduled as a cron job to run automatically in the background. Considerations for this type of solution may include running this cron job with elevated privileges to forgo the ‘sudo’ password dilemma of running the commands, tacking on a ‘-y‘ where applicable to answer “Yes” to the system prompts, and adding a ‘&‘ at the end of commands to ensure they run as a background process:

#!/bin/bash

apt-get -y purge linux-image-X.X.X-X &

update-grub &

There are of course no shortage of tools that can maintain kernel versions, and do their job well, but in case you find yourself in a pinch, this tutorial is at least one method to straighten things out.