Thursday, November 14, 2024
HomeTutorialsHow to Free Up Space in /boot Partition on Ubuntu Linux?

How to Free Up Space in /boot Partition on Ubuntu Linux?

If you use a separate /boot partition on Ubuntu, you might face the problem where your /boot partition becomes almost full or runs out of space. This can happen for various reasons, including old kernel files getting piled. To resolve this problem, you can free up some space from that partition.

In this tutorial, we’ll show you a few ways of clearing up the /boot partition on Ubuntu Linux. For the demonstration, we’ll be using the Ubuntu 22.04 LTS version.

Let’s get started!

Free Space in /boot Using the autoremove Command


The autoremove command in Linux is useful for removing installation files, unused dependencies, and packages you no longer need. These include old kernel packages that take up space in the /boot partition.

By using the autoremove command, you don’t need to add anything to the command. That is, simply running this command should take care of removing those unnecessary files and clearing up space. To do that, run the command in the below format:

sudo apt autoremove
apt autoremove command
apt autoremove command being run

When prompted, press “Y” and then press Enter to continue. Notice from the screenshot that the command automatically removes some packages from your device.

If this method doesn’t solve your problem, you can proceed with the next method.

Free Space in /boot Manually Using Terminal


Even if the autoremove command didn’t work out for you, you can manually go through the old kernel packages and remove them yourself. Let’s show you how.

Since we’re dealing with kernels here, we must know the current kernel version you’re using. If anything, you don’t want to mess around with that.

To check the currently active kernel, run this command:

uname -r
kernel version check
kernel version check

In our case, the current version is 6.2.0-32-generic. Now that you know that, you can safely remove other kernels that don’t match this version number.

You should now check and list the other kernels available. Do that with the following command:

ls -l /boot
check boot partition for old kernels
Check the /boot partition for old kernels

As you can see from the above screenshot, some kernel numbers don’t match the current one. Especially the names that contain “old” in them. You can remove these kernel files.

You can delete the old kernels one by one using this command syntax:

sudo rm /boot/kernel/file/name/with/correct/version

So, for example, if we want to delete vmlinuz-6.2.0-26-generic, then the command will be as follows:

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

So, following the above command, you can remove the unnecessary files one by one and clear up some space.

But this may take a while and a lot of command inputs if you have too many such files. In that case, you can use wildcards.

With wildcards, you can remove all files that have a pattern in their name with a single command.

Going back to the above example, here’s the format you need to follow to remove all kernel files with a similar pattern at once:

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

This will work for a single old version kernel. What if you have several versions on your device? To deal with that, you can write them separated with commas in the curly braces. For example, if there was another kernel version 6.2.0-28, then this is the format we would follow:

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

That should provide a clear idea to you on how to use the wildcards.

After removing the old kernels, you can also update the GRUB bootloader so that the old ones don’t appear. To update GRUB, run the below command:

sudo update-grub

Free Space in /boot Manually Using a GUI Tool


If you’re more comfortable with using GUI tools instead of the terminal, then this method is more suitable for you. You can either use the Synaptic Package Manager or another tool called Stacer. In this tutorial, we’ll be using Stacer.

First, you’ll need to install Stacer. Since it’s not available on Ubuntu officially, you need to add the PPA.

Add Stacer PPA with this command:

sudo add-apt-repository ppa:oguzhaninan/stacer
add stacer ppa
Add stacer PPA

When asked, press Enter to confirm. Next, you must update your system to make the changes take effect. To update your repository cache list, run this command:

sudo apt-get update
sudo apt update
sudo apt update to update software repository list

You’re now ready to install Stacer. Install Stacer by running this command:

sudo apt-get install stacer
install stacer
Installing Stacer

To launch the app, run the below command in your terminal:

stacer
run stacer
Launch Stacer

In the left sidebar, go to the Uninstaller tab.

stacer uninstaller tab
Stacer uninstaller tab

In the packages list, scroll down until you find the old kernel version.

old kernel in stacer
The old kernel in Stacer

Press the tick box to select the old kernel file. Then, uninstall it by pressing the “Uninstall Selected” button.

uninstall old kernel in stacer
Uninstall the old kernel in stacer

In the same way, you can also delete other files associated with your old kernel, such as header files. If you’re facing difficulty finding the target files, you can always use the search function.

search in stacer
Search in stacer

And in the same way, you can select the unnecessary files and uninstall them.

Free Space in /boot Using a Third-Party Script


In this last method, we’ll show you a bash script you can run to remove old kernel files and clear space in the /boot partition. Note that running a script may be unsafe if you don’t know what you’re doing. Only run scripts from sources you trust and if you can read and understand the script yourself.

Open a text editor. We will use the Nano text editor for this tutorial. In your editor, copy and paste the following script:

#!/bin/bash
# ryul99 - 2023-04-13
# Origin: BETLOG - 2018-03-31--19-48-34
# based on https://gist.github.com/jbgo/5016064

echo -ne "CAUTION::\\nThis script apt-get removes all but the currently operational kernel"

read -p "Continue? (y/n)?" answer
case ${answer:0:1} in
    y|Y )
        sudo -v
        current=`uname -r`
        available=`dpkg -l | grep -Po "linux-image.* | grep ii" | cut -d ' ' -f 1`
        remove=()
        for name in $available; do
            if [[ ${name/$current/} == ${name} ]]; then
                remove+=("$name")
            fi
        done
        sudo apt-get purge ${remove[@]}
        sudo apt-get autoremove
        echo FINISHED
    ;;
    * )
        echo ABORTING;
    ;;
esac

Once you’re done, save the file with Ctrl+O and exit the editor with Ctrl+X. Now change the file permission with this command:

chmod u+x script.sh
chmod script file
Make the script file executable

This will make the file into an executable file that you can run. Make sure to use the file name you used. Now run the script with this command:

./script.sh

The script will take care of any old kernel files and stay away from the currently active kernel. That should free space in the /boot partition on your Ubuntu system.

Final Words


If you’re running out of space in the /boot partition on Ubuntu, this tutorial will help you manage that space so that you don’t receive any warning in the future. Want to know how to free up more space by deleting files and directories on Linux? Check out our guide for that.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

You May Like It!

Trending Now