Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Free up space on /boot disk (ubuntu)

Free disk space when /boot is full (Ubuntu)

TL;DR

dpkg -l linux-image*
uname -r
sudo apt-get remove linux-image-2.6.32-{21,37,38,39,40,41,42,43,44}-server
sudo apt-get autoremove

This morning, I noticed New Relic that the /boot disk was almost full on several of our servers. It turns out that when the old kernel images are left on the disk after a kernel update. The solution is to delete old kernels that we don't need anymore.

1. Find packages to delete.

You can safely delete all but the latest package that matches the pattern linux-image-2.6.32-[0-9][0-9]-server. Leave linux-image-2.6.32-45-server on your system because you will need it to reboot!

$ dpkg -l linux-image*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                          Version                                       Description
+++-=============================================-=============================================-==========================================================================================================
un  linux-image                                   <none>                                        (no description available)
un  linux-image-2.6                               <none>                                        (no description available)
ii  linux-image-2.6.32-21-server                  2.6.32-21.32                                  Linux kernel image for version 2.6.32 on x86_64
ii  linux-image-2.6.32-37-server                  2.6.32-37.81                                  Linux kernel image for version 2.6.32 on x86_64
ii  linux-image-2.6.32-38-server                  2.6.32-38.83                                  Linux kernel image for version 2.6.32 on x86_64
ii  linux-image-2.6.32-39-server                  2.6.32-39.86                                  Linux kernel image for version 2.6.32 on x86_64
ii  linux-image-2.6.32-40-server                  2.6.32-40.87                                  Linux kernel image for version 2.6.32 on x86_64
ii  linux-image-2.6.32-41-server                  2.6.32-41.91                                  Linux kernel image for version 2.6.32 on x86_64
ii  linux-image-2.6.32-42-server                  2.6.32-42.96                                  Linux kernel image for version 2.6.32 on x86_64
ii  linux-image-2.6.32-43-server                  2.6.32-43.97                                  Linux kernel image for version 2.6.32 on x86_64
ii  linux-image-2.6.32-44-server                  2.6.32-44.98                                  Linux kernel image for version 2.6.32 on x86_64
ii  linux-image-2.6.32-45-server                  2.6.32-45.104                                 Linux kernel image for version 2.6.32 on x86_64
ii  linux-image-server                            2.6.32.45.52                                  Linux kernel image on Server Equipment.

2. Identify the kernel version you are currenlty running

DON'T delete this kernel image! And if it's not the latest, don't delete the latest one either.

$ uname -r
2.6.32-45-server

3. Delete the old kernels.

Removing the old kernels is the same as removing any other package. I'm using shell expansion for the version numbers to save typing. It will prompt you with a list of packages that will be removed, so you can double check the list before continuing.

sudo apt-get remove linux-image-2.6.32-{21,37,38,39,40,41,42,43,44}-server

4. Remove dependencies.

There are some dependencies left on the system after removing the old kernels. Fortunately, you can easily clean these up, too.

sudo apt-get autoremove
@jbgo

This comment has been minimized.

Copy link
Owner Author

jbgo commented Feb 22, 2013

Also, here's how you can check disk usage on all partitions with the df command:

$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/hostname-root
                       19G  7.4G   11G  42% /
none                  4.0G  184K  4.0G   1% /dev
none                  4.0G     0  4.0G   0% /dev/shm
none                  4.0G   48K  4.0G   1% /var/run
none                  4.0G     0  4.0G   0% /var/lock
none                  4.0G     0  4.0G   0% /lib/init/rw
/dev/sda1             228M   36M  181M  17% /boot
@jbgo

This comment has been minimized.

Copy link
Owner Author

jbgo commented Mar 22, 2013

To see which kernel version you are currently running:

$ cat /proc/version_signature
Ubuntu 2.6.32-45.104-server 2.6.32.60+drm33.26
@piyushpandita92

This comment has been minimized.

Copy link

piyushpandita92 commented Jul 23, 2015

does one need to go into the src in usr, and then clear/purge the header files separately?
i had to do that. does t usually have to be this way or just purging the old kernels in the boot directory gets rid of the kernel headers as well?

@piyushpandita92

This comment has been minimized.

Copy link

piyushpandita92 commented Jul 23, 2015

does one need to go into the src in usr, and then clear/purge the header files separately?
i had to do that. does it usually have to be this way or just purging the old kernels in the boot directory gets rid of the kernel headers as well?

@isnotgold

This comment has been minimized.

@MeDeVILe

This comment has been minimized.

Copy link

MeDeVILe commented May 20, 2016

Thank you. This is the what I needed and it is explained so everybody can learn not just copy/paste.

@obadaro

This comment has been minimized.

Copy link

obadaro commented Jun 30, 2017

@hemanthsunny

This comment has been minimized.

Copy link

hemanthsunny commented Jul 11, 2017

Thank you for solving the issue. It helped me and I'm looking for exactly what described here.

@keenlogic

This comment has been minimized.

Copy link

keenlogic commented Jul 13, 2017

When I try this, it is giving me a message "Package 'linux-image-3.8.0-31-generic' is not installed, so not removed"
This happens for every kernel image that is listed from the dpkg -l output

@jstennant

This comment has been minimized.

Copy link

jstennant commented Jul 23, 2017

@keenlogic, I had the same problem "Package 'linux-image-x.y.z-j-generic' is not installed, so not removed"

I believe what is happening is that when you run the "dpkg -l linux-image*" command, it's still listing those "already removed" packages because there are still things depending on them. I guess this means that they haven't actually been removed and are still taking up disk space because I would still get disk space related errors when running other commands.

What I had to do was remove the dependencies manually first, so for each "x.y.z-j" image listed, I would run the following commands in this order:
sudo dpkg --purge linux-signed-image-x.y.z-j-generic sudo dpkg --purge linux-image-extra-x.y.z-j-generic sudo dpkg --purge linux-image-x.y.z-j-generic

Some of those commands would fail for certain packages claiming that they were already removed, usually the third command.

Then another:
dpkg -l linux-image*
would show that the image was no longer listed.

I was careful not to remove my current version and the latest version as suggested in the post.

No doubt you can batch up the commands for multiple images in one go, I just went through them one by one manually to see what was happening at each point, didn't take long.
sudo apt-get autoremove
command now runs happily

@Theshedman

This comment has been minimized.

Copy link

Theshedman commented Nov 4, 2017

jbgo your solution worked perfectly for me. Thanks man.

@gustavz

This comment has been minimized.

Copy link

gustavz commented Mar 23, 2018

Filesystem      Size  Used Avail Use% Mounted on
/dev/root        28G   25G  1.7G  94% /
devtmpfs        7.7G     0  7.7G   0% /dev
tmpfs           7.7G   15M  7.7G   1% /dev/shm
tmpfs           7.7G   14M  7.7G   1% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           7.7G     0  7.7G   0% /sys/fs/cgroup
tmpfs           786M   68K  786M   1% /run/user/1001
/dev/mmcblk1p1   31G  9.4G   21G  32% /media/nvidia/9016-4EF8

does this look normal for a nvidia jetson tx2?
i ran out of disk space after a failed installaiton, now i am purging a lot of packages to get disk space.
How can i delete the tmp files created through the failed installation

@BETLOG

This comment has been minimized.

Copy link

BETLOG commented Mar 31, 2018

I just wrote this. My bash is a little rusty though

#!/bin/bash
# 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.*generic"`
        remove=()
        for name in $available; do
            if [[ ${name/$current/} == ${name} ]]; then
                remove+=("$name")
            fi
        done
        sudo apt-get remove ${remove[@]}
        sudo apt-get autoremove
        echo FINISHED
    ;;
    * )
        echo ABORTING;
    ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.