1. Overview

On a Linux machine, /boot is an important partition that contains all the files required to start the system. These files include initrd (Initial RAM Disk) files, kernel files, configuration files, and more. If this partition contains a low amount of free space, it can affect the system in many ways:

  • startup failure
  • software issues
  • system malfunctioning
  • data corruption

To avoid such problems, we can clear up more space in the /boot partition to keep it working smoothly.

In this tutorial, we’ll take a look at some useful methods that can help us safely free up more space in /boot.

2. Using apt

The apt command works as a package manager on Debian-based distributions such as Ubuntu. It enables the installation, upgrade, and removal of software packages in the system.

We can also use apt to safely clear up more space in the /boot partition:

$ sudo apt autoremove

The apt command with the autoremove subcommand removes all the unnecessary dependencies from the system. It also cleans up different files:

  • temporary files
  • APT package repository caches
  • old kernels from the /boot partition

As a result, /boot can regain a significant amount of free space after the operation.

Notably, old kernels might be lost after the cleanup process. So, it’s a best practice to back them up beforehand to ensure that they’re safe in case anything goes wrong.

3. Using dpkg

The dpkg command acts as a lower-level package manager on Debian-based distributions such as Ubuntu. We can use this command instead of the apt command to manage software packages on the system.

We can also use dpkg to safely free up more space in the /boot partition by manually removing old kernels from that partition.

3.1. Checking Current Kernel

Before starting the process, we’ll check the kernel release that we’re currently using. This ensures that we don’t accidentally delete the active kernel during the process:

$ uname -r
6.3.9-32-generic

The uname command with the -r option, short for –kernel-release, prints the currently running kernel release to the terminal.

3.2. Listing Kernels

The first step is to combine the dpkg command and the grep command to list all known kernel release packages from the dpkg database:

$ dpkg -l | grep linux-image
rc  linux-image-6.3.9-28-generic              6.3.9-28.28~22.04.1
		    amd64        Signed kernel image generic
rc  linux-image-6.3.9-29-generic              6.3.9-29.29~22.04.1
		    amd64        Signed kernel image generic
ii  linux-image-6.3.9-30-generic              6.3.9-30.30~22.04.1
		    amd64        Signed kernel image generic
ii  linux-image-6.3.9-31-generic              6.3.9-31.31~22.04.1
		    amd64        Signed kernel image generic
ii  linux-image-6.3.9-32-generic              6.3.9-32.32~22.04.1
		    amd64        Signed kernel image generic

Here, dpkg with the -l option, short for –list, prints all known packages from the dpkg database. Then, grep filters the output to only those packages that have linux-image in their names. Since only kernel release packages have linux-image in their names, they’re the ones that remain in the output.

As we can see, the packages contain two types of labels:

  • rc label indicates that the corresponding kernel doesn’t exist on the system, but its configuration files are still present on the device
  • ii label suggests that the corresponding kernel is installed on the system

The /boot partition stores both the installed kernels and the configuration files of the removed kernels. So, we can remove the unnecessary ones among them to safely clear up more space in /boot.

3.3. Removing Unnecessary Kernels

Among the kernels in the output, we’re currently using the linux-image-6.3.9-32-generic kernel on the system, as we noted earlier. So, we’ll keep it along with the linux-image-6.3.9-31-generic kernel as a backup.

The other three kernels are probably no longer required on the system. Hence, we’ll use the dpkg command to completely remove them from the machine by deleting their configuration files:

$ sudo dpkg --purge linux-image-6.3.9-28-generic
$ sudo dpkg --purge linux-image-6.3.9-29-generic
$ sudo dpkg --purge linux-image-6.3.9-30-generic

The dpkg command with the –purge option completely removes the specified kernel from the device.

Again, we might lose these configuration files after the process. So, we’ll back them up before the operation to prevent them from getting lost or corrupted.

4. Using Manual Removal

The basic rm command is a straightforward but manual way to remove old kernels from the /boot partition and safely clear up more space in that partition.

First, we navigate to the /boot partition:

$ cd boot

Then, we list the contents of that partition:

$ ls -1
...
vmlinuz-6.3.9-28-generic
vmlinuz-6.3.9-29-generic
vmlinuz-6.3.9-30-generic
vmlinuz-6.3.9-31-generic
vmlinuz-6.3.9-32-generic
...

The -1 option lists each file on a separate line.

Visibly, the output contains all the existing kernel files on the system. Among these files, we can assume we probably don’t need some of the first entries. So, we’ll remove them from the machine. Before that, we should back them up on another partition or storage to keep them unaffected.

Next, let’s use the rm command to remove those kernels from the system:

$ sudo rm vmlinuz-6.3.9-28-generic
$ sudo rm vmlinuz-6.3.9-29-generic
$ sudo rm vmlinuz-6.3.9-30-generic

After running the above commands, the specified kernels will be removed from the /boot partition.

5. Conclusion

In this article, we went through three useful methods that help safely free up more space in the /boot partition.

This operation enables us to keep /boot and the system working smoothly. No matter which method we go for, we might be able to safely clear up a substantial amount of space in the /boot partition.

Comments are closed on this article!