How to Safely Free Up Space in the Boot Partition on Ubuntu

Key Takeaways

  • To free up space in boot partition, you can use the “sudo apt autoremove” command or, with a specific package name, the “dpkg –purge” command. These commands can remove the old unused kernels to free space in the boot partition.
  • You can use a graphical application like Synaptic for managing and deleting the old kernels. Using this tool, you can mark multiple kernel packages for removal at once—resulting in more free space.
  • You can also permanently resize the boot partition to gain extra space using GParted.



Cleaning up the boot partition on an Ubuntu Linux system typically involves removing old or unused kernel files. Over time, as you update your Linux kernel, old kernel versions accumulate and take up space. To free up boot space, you can simply remove the old kernels or resize the boot partition at once.


What Is Boot Partition on Linux? (And Why You Should Clean It)

A boot partition on Linux is a small partition that contains the files needed to start the operating system. Mainly, it contains the kernel, the initrd, and the bootloader. The boot partition is usually mounted at /boot/. The boot partition is helpful for running multiple operating systems on a single machine or system. It allows each operating system to have its own bootloader.

You should clean your boot partition periodically to remove old and unused kernels. These old kernels take up a lot of space and cause problems when updating the system. This can also prevent further updates and even slow down the boot process. Some old kernels may contain bugs or vulnerabilities that can expose your Linux system to threats.


You can use tools like apt autoremove to remove old kernels, or manually delete the files from /boot/. Do not delete the current kernel or the files needed by the bootloader. Otherwise, your system will not boot.

Resizing the boot partition on Linux is a risky operation that can cause data loss, system corruption, or boot failure. Before you attempt to modify the boot partition, back up your data and check disk health.

Check Boot Partition Usage

Before freeing up space, you must check the boot partition usage. This will give you an idea of how low your system is on boot partition space. It will also help to identify the space that boot partition needs.

To check the boot partition usage you can run the df -h command. The -h option here displays the information in a more readable format—in gigabytes and megabytes:

df -h


showing boot partition space available

The output of this command will display the boot partition total size, used space, available space, and percentage usage.

Removing Old Kernels Using the Linux Terminal

When you regularly update your Ubuntu Linux system, it’s common for old kernel files to start accumulating over time. These accumulated older kernel versions can consume valuable disk space in the boot partition.

Deleting the old unused kernel files will release the space in the boot partition—making more room for other files or new kernels.

You can use the apt autoremove command to automatically remove the unused kernels. This command also removes the unused associated dependencies. Run the following command in the terminal:

sudo apt autoremove


command that automatically remove unused packages and files

This command will display the list of all packages that are going to be removed. You should press Y to proceed.

The
autoremove
method will only work if you have some space left in the /boot partition. If your /boot partition is full, you have to manually remove some old kernels first.

Before you move towards the manual deleting of old kernels—first check the name and version of the current running kernel. This ensures that you don’t accidentally remove the active kernel, which is important for your system’s stability.

You can find the currently running kernel version using the uname command. This will show the kernel version that you are using—and you should not delete:


uname -r

uname command to check running kernel

After knowing the current kernel you can use the ls command to list all the contents of the /boot directory in a detailed format:

ls -l /boot

This command shows you the files related to the Linux kernel, such as kernel images (vmlinuz), initial ramdisk images (initrd.img), and configuration files. You can delete the unused kernels that are either labeled as “old” or have a different version number than your current kernel.

command to list boot directory files and kernels


You can use the dpkg command to list all the installed packages that have “linux-image” in their package names. It specifically targets packages related to the Linux kernel, including different kernel versions and related modules.

dpkg -l | grep linux-image

This will show you something like this:

command that list all limux kernel installed in boot partition

The output indicates that multiple Linux kernel images are installed on the system. The ones labeled with the status “ii” are installed. Those marked as “rc” have been removed but still have residual configuration files. The kernels 5.15.0-25-generic and 6.2.0-26-generic are the older kernel versions. The newest version is 6.2.0-32-generic, and the one before that is 6.2.0-31-generic.


You may have noticed that the ls -l/boot command doesn’t display the kernels 5.15.0-25-generic and 6.2.0-26-generic. This is because both these kernels are already deleted and labeled as “rc” by the dpkg command.

It’s recommended to keep at least one or two previous kernels as backups. You can use the previous kernel to boot your system if the latest one fails. It also avoids data loss or any need to reinstall the operating system.

The kernel 6.2.0-32-generic is the current one, so we don’t want to delete it. To remove the older unused kernel, use a command with this format:

sudo apt remove linux-image-

In the above command, the linux-image- is the name of the package that contains the old kernel version. Here, you can specify the kernel you want to remove.


To delete the linux-image-5.15.0-25-generic kernel you can use the following command:

sudo apt remove linux-image-5.15.0-25-generic

command that removes kernel name image-5.15.0-25-generic

This will also remove any related packages, such as linux-modules or linux-headers. Confirm the action by pressing Y.

Similarly, you can also remove the other older kernel using:

sudo apt remove linux-image-6.2.0-26-generic

command to remove the kernel inux-image-6.2.0-26-generic


The old kernel packages removed using the apt remove command still appear in the dpkg -l list. This is because the apt remove command is more cautious about not removing packages that might be required by other packages. So, if it detects that the package is already in a “removed” state, it may not attempt to remove it again.

packages not removed after using sudo auto remove

This is why, in the above case, running apt remove didn’t remove the package from the dpkg -l list—as it was already marked as “rc”.

You can use the dpkg purge command to completely remove the package and its related configuration files from your system. When you run dpkg --purge, it doesn’t check for dependencies. It simply removes the package you specify:

sudo dpkg --purge linux-image-5.15.0-25-generic


purge command to remove kernel permanetly

This command explicitly purges the package linux-image-5.15.0-25-generic, which means it removes the linux-image package and its associated dependencies. Now the package will no longer appear in the dpkg -l list.

Similarly, you can also completely remove the 6.2.0-26-generic kernel package using the below command:

sudo dpkg --purge linux-image-6.2.0-26-generic

Now the 6.2.0-26 package is also removed from the dpkg -l list.

removing the linux-image-6.2.0-26-generic using purge command


You can also use the rm command to remove the old kernels from the boot partition.

sudo rm /boot/vmlinuz-6.2.0-26-generic

To remove multiple files with the same naming pattern, you can use the below command:

sudo rm /boot/*-6.2.0-{26}-*

This command will remove all those files in the boot partition that have names matching the pattern -6.2.0-{26}.

You can also specify multiple naming patterns to delete all of them at once. The comma (,) in between the command specifies multiple values within a brace expansion.

sudo rm /boot/*-6.2.0-{26,31}-*

The above command deletes the files in the /boot directory with names that match the pattern -6.2.0-{26} or -6.2.0-{31}.

Now update the GRUB bootloader configuration to avoid displaying the older kernels:

sudo update-grub


Update GRUB bootloader

Run the autoremove command to clean up any remaining packages that are no longer needed. This command will also remove the old initramfs and header files associated with the deleted kernels:

sudo apt autoremove

command that automatically remove unused packages and files

Removing Old Unused Kernel Files Using Synaptic

Removing old kernels using a terminal can be difficult and risky. It requires typing complex commands and knowing the exact names of the kernel packages. You can easily remove old kernels using a GUI-based package manager tool called Synaptic.


Using Synaptic, you can selectively remove the older unused kernels. You should always keep the current kernel and the previous one as a backup for the system. This will improve your system performance and save disk space.

First, you need to install Synaptic on your system. To install Synaptic, use the following command:

sudo apt install synaptic

command to install synaptic

Launch Synaptic from the application menu or by typing sudo synaptic in a terminal window.

Launch Synaptic from Application Menu


In the Synaptic window, click on the “Sections” button. Now select “Kernel and Modules”. This will display all the installed kernel versions on your system.

Finding unused Kernel and modules from section window

You can also use the “Search” option to find older kernels. Type linux-image and click “Search”. This will display all the installed kernel packages on your system partitions.

search for unused kernel manually


Right-click on any old kernel package that you want to remove (except the current one and the previous one) and choose “Mark for Complete Removal”. You can run the uname -r command in a terminal window to find the current kernel version.

Mart any old kernel for removal

Now, click on “Apply” to start deleting the selected kernel version.

deleting selected kernel by clicking the apply button

You will see a summary of the actions about to be taken. Confirm the removal of the selected packages by clicking the “Apply” button.


Summary of the processes which are going to be done.

The selected kernel package files and their associated dependencies will be deleted. It will free up some disk space on the boot partition.

unused kernel started for deleting

Resizing the Boot Partition on Ubuntu to Free Up Space

Both methods discussed earlier depend on the removal of unused kernels or packages. But removing old kernels is not a permanent solution. Your Ubuntu system boot partition could still fill up thanks to the packages you install. So if you still aren’t comfortable with the amount of room you’ve freed up, resize the boot partition to avoid full directory error issues.


You can easily resize the boot partition using the GParted tool on Ubuntu. GParted is a graphical partition editor that can easily resize, move, copy, and manipulate partitions on your hard drive.

Before resizing your boot partition, make sure to
back up your important data
. This is a high-risk task that involves the change of the boot partition boundary, which can result in booting problems.

Begin by creating a bootable USB drive containing the GParted tool ISO file. GParted is a portable version of the Ubuntu system that can manage partitions. First download the GParted ISO file required for creating the bootable drive.

GParted ISO file download page


After downloading the GParted ISO file, you can create a bootable drive using a tool like Rufus. Using this tool, you can write the ISO file onto a USB drive—making it bootable and ready for use.

Related: How to Create a Bootable Linux USB Flash Drive, the Easy Way

Open your Ubuntu system and run the command df -h to determine the current size of the boot partition.

showing boot partition space available

Launching GParted on a PC

To use GParted, you have to change the boot order in your system’s BIOS settings. This means that you need to tell your system to boot from the USB drive or CD/DVD that contains GParted (instead of your internal hard drive).


You can enter the BIOS after restarting the system. You need to press a specific key to access the BIOS mode on your system. The key may be different depending on the system model and manufacturer. It is usually one of these keys: F1, F2, F10, F12, DEL, or ESC. You can also check your system manual or on-screen instructions for which key to press.

Related: How to Boot Your Computer From a Disc or USB Drive

Open the “Boot” or “Boot Order” menu inside the BIOS or UEFI settings.

Change the boot order to prioritize the USB drive. Move the GParted USB drive to the top of the list or select it as the first boot device. You can also press “Enter” to directly boot GParted from the USB drive.

Once you have changed the boot order and saved the changes, you need to exit the BIOS mode and restart your system. This will boot your system from the USB drive or CD/DVD that contains GParted.


Launching GParted as a Virtual Machine

If you are using Ubuntu as a virtual machine (VM), you need to create a bootable GParted VM. While setting the GParted virtual machine, do not create a virtual hard disk for it. GParted is a live Linux distribution that entirely runs in RAM, so it doesn’t require a virtual hard disk.

In the GParted virtual machine settings, set the optical drive as the primary boot device before the hard disk.

Select optical as first drive to boot GParted

Promoting the optical disk option to the top means setting the virtual optical drive as the first boot device. This ensures that when you start the GParted VM, the GParted ISO or Live CD should be recognized as the primary boot source.


After setting up the GParted VM, you need to attach the virtual hard disk (VDI) containing your Ubuntu installation to GParted. The VDI file represents your Ubuntu virtual machine’s storage. Selecting Ubuntu VDI in the SATA controller settings of the GParted VM makes the Ubuntu virtual disk available to GParted for editing.

You can find the Ubuntu VDI at the following location:

C:\Users\(Username)\VirtualBox VMs\Ubuntu

Select the Ubuntu VDI in GParted SATA settings

Using GParted

After you boot the GParted from the USB drive, you will see the GNOME Partition Editor Window. By default, the GParted Live option is selected. Press Enter to launch GParted.


Boot GParted with default settings

Press the enter key to continue with default settings and follow any on-screen instructions.

GParted language and keymap selection

The GParted window will open showing all partitions on your system.

Gparted front window


Identify a non-system partition (/dev/sda3) and unmount it before proceeding. This will detach the partition from file system and can be safely modified. If you see a Gray “Mount” option, it means the partition is already unmounted.

Unmount the non-system partition

First, shrink the non-system partition to get some unallocated space. After that, you can increase the boot partition size by allocating this space to the boot partition. Right-click on the non-system or root partition and select “Resize/Move”.​​​​​​

Resizing Non-System Partition

To increase the size of a boot partition, unallocated space must be available right next to the boot partition.


A dialog box will appear where you can drag the left edge of the partition to shrink it and create some unallocated space next to the boot partition. You can also specify the size in the “New Size” field. Click “Resize/Move” to apply the changes.

Adjust Root Partition Size

A warning message will prompt, showing that your system might fail to boot after moving the partition. Hit “OK” to continue.

Warning Message asking for final permission to resize partition


A new unallocated partition will be created right next to the boot partition.

unallocated partition

Next, you can resize the boot partition after assigning it the unallocated space. Right-click the boot partition and select the “Resize/Move” option. Inside the dialog box, drag the right edge of the partition to expand it and fill the unallocated space. Click “Resize/Move” to apply the changes.

Resizing Boot Partition


Save your change by clicking the green checkmark icon and selecting “Apply” from the new menu.

apply the changes

Now wait for the operation to complete. GParted will shrink the dev/sda3 partition by 500 MiB and allocate this to the boot partition.

boot partition resizing start

When the boot partition is resized, a completed operation message will appear.

Boot partition resized complete


You can verify the new boot partition size from GParted.

Boot Partition Size Increased

Reboot your Ubuntu system. Make sure you select the default Ubuntu system as your boot option. For virtual machines, boot from the default Ubuntu virtual machine.

Reboot Ubuntu System

After rebooting, run the command df -h to confirm the new size of the boot partition. In our example, the boot partition has been expanded from 500 MiB to 1011 MiB.


verify the boot partition using df command

Actively Manage Linux Partitions

Linux partitions are sections of a disk that store files and data for the Linux operating system. Before you have to face the “disk full error” on your system, it’s better to manage your Linux partitions actively. Active monitoring includes keeping your system up to date, regularly purging old kernels, and monitoring disk space usage.

Like the GParted tool, you can also use the fdisk command to actively manage Linux partitions. The fdisk command will give you precise control over disk partitions. You can view existing partitions, create new ones, and set their file system types. You can also change partition attributes and manage the layout of disk drives efficiently.


أضف تعليق