How to Remove/Delete Old Kernel Images on Debian/Ubuntu Linux Systems

undefined
Suddenly, my Ubuntu update failed, and after checking my machine, I found that /boot partition is full, which caused this issue. I noticed that old kernel still exists in my system. In this mini post, your will find the answer for these question on Debian/Ubuntu Linux systems:
Why doesn’t Ubuntu remove old kernels automatically? How do I delete old unused kernel images to free disk space. How to remove unused old kernel images on Ubuntu Linux safely?
You need to delete and/or remove old kernels from system manually. Ubuntu and Debian based system keeps old kernel images so that system can be booted if newer kernel failed. The safest way to purge and remove old kernel is as follows. In this tutorial you will learn how to delete unused old kernel images on Ubuntu or Debian Linux to free disk space as well as the various state of linux-image package.

Prerequisites

  • Boot into the newly installed kernel “the one you’ll keep”, and run the following command:
    $ uname -r
    4.4.0-74-generic

    So, we’ll keep this kernel image and remove the others.

  • You must be root user or use sudo command, for me I prefer being root, so run the following command:
$ sudo su -
In this mini post we’ll remove old kernel images using two steps, Let’s start

Step 1: Some checks on your machine status

I need to show you the status of my machine first to see the effect of each method we’ll use .

Check 1: Disk Space Status

Run the following commands to see you free disk space:

# df -h
Filesystem Size Used Avail Use% Mounted on
udev 2.9G 0 2.9G 0% /dev
tmpfs 586M 9.0M 577M 2% /run
/dev/mapper/all_vg-root 453G 421G 9.3G 98% /
tmpfs 2.9G 97M 2.8G 4% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 2.9G 0 2.9G 0% /sys/fs/cgroup
/dev/sda1 1.9G 1.8G 0 100% /boot
cgmfs 100K 0 100K 0% /run/cgmanager/fs
tmpfs 586M 104K 586M 1% /run/user/1002
tmpfs 586M 0 586M 0% /run/user/0

As you see, my /boot partition has 0 available space, also / has 9.3 GB free disk space.

Check 2: List all installed Linux kernel images on my machine

We need to list the installed kernel on our machine, run the following commands:

# dpkg --list | egrep -i 'linux-image|linux-headers'

Actually, this command output 166 installed linux-image, linux-headers, and linux-image-extra, here’s a sample output:

ii linux-headers-3.13.0-37 3.13.0-37.64 all Header files related to Linux kernel version 3.13.0
ii linux-headers-3.13.0-37-generic 3.13.0-37.64 amd64 Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
ii linux-headers-3.13.0-39 3.13.0-39.66 all Header files related to Linux kernel version 3.13.0
ii linux-headers-3.13.0-39-generic 3.13.0-39.66 amd64 Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
....
ii linux-image-4.4.0-71-generic 4.4.0-71.92 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-4.4.0-72-generic 4.4.0-72.93 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-4.4.0-73-generic 4.4.0-73.94 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-4.4.0-74-generic 4.4.0-74.95 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
....
ii linux-image-extra-4.4.0-71-generic 4.4.0-71.92 amd64 Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
ii linux-image-extra-4.4.0-72-generic 4.4.0-72.93 amd64 Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
ii linux-image-extra-4.4.0-73-generic 4.4.0-73.94 amd64 Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
ii linux-image-extra-4.4.0-74-generic 4.4.0-74.95 amd64 Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
ii linux-image-generic 4.4.0.74.80 amd64 Generic Linux kernel image

Now, after we checked our machine status, it’s time to start removing old kernel images

Step 2: Remove Linux kerenl images for newer Ubuntu and Debian Linux systems

If you’re using a newer release of Debian “like Debian 8” or Ubuntu “like Ubuntu 16.04”, you can use the following command to automatically remove old kernel images. In this post I’m using Ubuntu 16.04.

# sudo apt --purge autoremove

On newer system all obsolete kernels and headers should automatically be flagged as no more needed, and thus can be purged with the following single command:

# sudo apt --purge autoremove
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following packages will be REMOVED:
 libgles1-mesa* linux-headers-4.4.0-58* linux-headers-4.4.0-58-generic* linux-headers-4.4.0-59* linux-headers-4.4.0-59-generic*
 linux-headers-4.4.0-62* linux-headers-4.4.0-62-generic* linux-headers-4.4.0-63* linux-headers-4.4.0-63-generic*
 linux-headers-4.4.0-64* linux-headers-4.4.0-64-generic* linux-headers-4.4.0-65* linux-headers-4.4.0-65-generic*
 linux-headers-4.4.0-66* linux-headers-4.4.0-66-generic* linux-headers-4.4.0-67* linux-headers-4.4.0-67-generic*
 linux-headers-4.4.0-69* linux-headers-4.4.0-69-generic* linux-headers-4.4.0-70* linux-headers-4.4.0-70-generic*
 linux-headers-4.4.0-71* linux-headers-4.4.0-71-generic* linux-headers-4.4.0-72* linux-headers-4.4.0-72-generic*
 linux-image-4.4.0-58-generic* linux-image-4.4.0-59-generic* linux-image-4.4.0-62-generic* linux-image-4.4.0-63-generic*
 linux-image-4.4.0-64-generic* linux-image-4.4.0-65-generic* linux-image-4.4.0-66-generic* linux-image-4.4.0-67-generic*
 linux-image-4.4.0-69-generic* linux-image-4.4.0-70-generic* linux-image-4.4.0-71-generic* linux-image-4.4.0-72-generic*
 linux-image-extra-4.4.0-58-generic* linux-image-extra-4.4.0-59-generic* linux-image-extra-4.4.0-62-generic*
 linux-image-extra-4.4.0-63-generic* linux-image-extra-4.4.0-64-generic* linux-image-extra-4.4.0-65-generic*
 linux-image-extra-4.4.0-66-generic* linux-image-extra-4.4.0-67-generic* linux-image-extra-4.4.0-69-generic*
 linux-image-extra-4.4.0-70-generic* linux-image-extra-4.4.0-71-generic* linux-image-extra-4.4.0-72-generic* snap-confine*
 ubuntu-core-launcher*
0 upgraded, 0 newly installed, 51 to remove and 26 not upgraded.
After this operation, 3,560 MB disk space will be freed.
Do you want to continue? [Y/n] y
As you see, this command will remove some kerenl images, headers, and extra packages from our system and will free about 3.5GB of disk space. After pressing the enter key, wait for some time, here’s a sample output:

(Reading database ... 1435549 files and directories currently installed.)
Removing libgles1-mesa:amd64 (12.0.6-0ubuntu0.16.04.1) ...
Removing linux-headers-4.4.0-58-generic (4.4.0-58.79) ...
Removing linux-headers-4.4.0-58 (4.4.0-58.79) ...
Removing linux-headers-4.4.0-59-generic (4.4.0-59.80) ...
Removing linux-headers-4.4.0-59 (4.4.0-59.80) ...
Removing linux-headers-4.4.0-62-generic (4.4.0-62.83) ...
Removing linux-headers-4.4.0-62 (4.4.0-62.83) ...
Removing linux-headers-4.4.0-63-generic (4.4.0-63.84) ...
Removing linux-headers-4.4.0-63 (4.4.0-63.84) ...
Removing linux-headers-4.4.0-64-generic (4.4.0-64.85) ...
Removing linux-headers-4.4.0-64 (4.4.0-64.85) ...
Removing linux-headers-4.4.0-65-generic (4.4.0-65.86) ...
Removing linux-headers-4.4.0-65 (4.4.0-65.86) ...
Removing linux-headers-4.4.0-66-generic (4.4.0-66.87) ...
Removing linux-headers-4.4.0-66 (4.4.0-66.87) ...
Removing linux-headers-4.4.0-67-generic (4.4.0-67.88) ...
Removing linux-headers-4.4.0-67 (4.4.0-67.88) ...
Removing linux-headers-4.4.0-69-generic (4.4.0-69.90) ...
Removing linux-headers-4.4.0-69 (4.4.0-69.90) ...

..............
Purging configuration files for linux-image-4.4.0-72-generic (4.4.0-72.93) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 4.4.0-72-generic /boot/vmlinuz-4.4.0-72-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 4.4.0-72-generic /boot/vmlinuz-4.4.0-72-generic
Removing snap-confine (2.23.6) ...
Removing ubuntu-core-launcher (2.23.6) ...
Processing triggers for libc-bin (2.23-0ubuntu7) ...

Progress: [ 34%] [########################################.........................................................................]

Now, we re-check our machine again, run previously two check commands again on your machine:

# dpkg --list | egrep -i 'linux-image|linux-headers'

This command will output now 118 different Linux kernel packages “previously it’s output was 166”.

Now df command output will be

# df -h
Filesystem Size Used Avail Use% Mounted on
udev 2.9G 0 2.9G 0% /dev
tmpfs 586M 9.0M 577M 2% /run
/dev/mapper/all_vg-root 453G 417G 14G 97% /
tmpfs 2.9G 127M 2.8G 5% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 2.9G 0 2.9G 0% /sys/fs/cgroup
/dev/sda1 1.9G 1.2G 553M 69% /boot
cgmfs 100K 0 100K 0% /run/cgmanager/fs
tmpfs 586M 104K 586M 1% /run/user/1002
tmpfs 586M 0 586M 0% /run/user/0

Hooray, we freed 550 MB from /boot, it enough now to resume our work, and also freed about 5 GB from /

Hint:

1. Note that the autoremove is used to remove packages that were automatically installed to satisfy dependencies 
for other packages and are now no longer needed. This may remove rest of unwanted packages too.

Step 3: Delete unwanted and unused kernel images manually

The following command is valid for all Debian/Ubuntu releases, it needs that you know the kernel images you\ll remove, run the following command to list the remaining Linux kernel on you system and exclude your current/latest kernel image:

# dpkg -l | tail -n +6 | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r)

Sample outputs:

pi linux-image-3.13.0-37-generic 3.13.0-37.64 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
pi linux-image-3.13.0-39-generic 3.13.0-39.66 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-40-generic 3.13.0-40.69 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-43-generic 3.13.0-43.72 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-44-generic 3.13.0-44.73 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-45-generic 3.13.0-45.74 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-46-generic 3.13.0-46.79 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-48-generic 3.13.0-48.80 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-49-generic 3.13.0-49.83 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-51-generic 3.13.0-51.84 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-52-generic 3.13.0-52.86 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-53-generic 3.13.0-53.89 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-54-generic 3.13.0-54.91 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-55-generic 3.13.0-55.94 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-57-generic 3.13.0-57.95 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
rc linux-image-3.13.0-58-generic 3.13.0-58.97 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-59-generic 3.13.0-59.98 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-61-generic 3.13.0-61.100 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-62-generic 3.13.0-62.102 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-63-generic 3.13.0-63.103 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-87-generic 3.13.0-87.133 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-88-generic 3.13.0-88.135 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-89-generic 3.13.0-89.136 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-91-generic 3.13.0-91.138 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-3.13.0-92-generic 3.13.0-92.139 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
pi linux-image-3.13.0-96-generic 3.13.0-96.143 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
pi linux-image-3.13.0-97-generic 3.13.0-97.144 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-4.4.0-73-generic 4.4.0-73.94 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP

As you seen our current/latest kerenl “4.4.0-74” is excluded from the above command:

You can remove kernel images by using the following apt-get command syntax:

# apt-get --purge remove linux-image-3.13.0-37-generic linux-image-3.13.0-39-generic linux-image-3.13.0-40-generic linux-image-3.13.0-43-generic linux-image-3.13.0-44-generic linux-image-3.13.0-45-generic linux-image-3.13.0-46-generic linux-image-3.13.0-48-generic linux-image-3.13.0-49-generic linux-image-3.13.0-51-generic linux-image-3.13.0-52-generic linux-image-3.13.0-53-generic

The purge removes packages and any configuration files are deleted too, here’s sample outputs:

Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following packages will be REMOVED:
 linux-image-3.13.0-37-generic* linux-image-3.13.0-39-generic* linux-image-3.13.0-40-generic* linux-image-3.13.0-43-generic*
 linux-image-3.13.0-44-generic* linux-image-3.13.0-45-generic* linux-image-3.13.0-46-generic* linux-image-3.13.0-48-generic*
 linux-image-3.13.0-49-generic* linux-image-3.13.0-51-generic* linux-image-3.13.0-52-generic* linux-image-3.13.0-53-generic*
 linux-image-extra-3.13.0-37-generic* linux-image-extra-3.13.0-39-generic* linux-image-extra-3.13.0-40-generic*
 linux-image-extra-3.13.0-43-generic* linux-image-extra-3.13.0-44-generic* linux-image-extra-3.13.0-45-generic*
 linux-image-extra-3.13.0-46-generic* linux-image-extra-3.13.0-48-generic* linux-image-extra-3.13.0-49-generic*
 linux-image-extra-3.13.0-51-generic* linux-image-extra-3.13.0-52-generic* linux-image-extra-3.13.0-53-generic*
0 upgraded, 0 newly installed, 24 to remove and 26 not upgraded.
After this operation, 2,330 MB disk space will be freed.
Do you want to continue? [Y/n] y

.......

Purging configuration files for linux-image-extra-3.13.0-53-generic (3.13.0-53.89) ...
Removing linux-image-3.13.0-53-generic (3.13.0-53.89) ...
Examining /etc/kernel/prerm.d.
run-parts: executing /etc/kernel/prerm.d/dkms 3.13.0-53-generic /boot/vmlinuz-3.13.0-53-generic
dkms: removing: bcmwl 6.30.223.271+bdcom (3.13.0-53-generic) (x86_64)

-------- Uninstall Beginning --------
Module: bcmwl
Version: 6.30.223.271+bdcom
Kernel: 3.13.0-53-generic (x86_64)
-------------------------------------

Status: Before uninstall, this module version was ACTIVE on this kernel.

wl.ko:
 - Uninstallation
 - Deleting from: /lib/modules/3.13.0-53-generic/updates/dkms/
 - Original module
 - No original module was found for this module on this kernel.
 - Use the dkms install command to reinstall any previous module version.

depmod....

DKMS: uninstall completed.
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-53-generic /boot/vmlinuz-3.13.0-53-generic
update-initramfs: Deleting /boot/initrd.img-3.13.0-53-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-53-generic /boot/vmlinuz-3.13.0-53-generic
Generating grub configuration file ...
Warning: Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.
Found linux image: /boot/vmlinuz-4.4.0-74-generic
Found initrd image: /boot/initrd.img-4.4.0-74-generic
Found linux image: /boot/vmlinuz-4.4.0-73-generic
Found initrd image: /boot/initrd.img-4.4.0-73-generic
Found linux image: /boot/vmlinuz-3.13.0-97-generic
Found initrd image: /boot/initrd.img-3.13.0-97-generic
Found linux image: /boot/vmlinuz-3.13.0-96-generic
Found initrd image: /boot/initrd.img-3.13.0-96-generic
Found linux image: /boot/vmlinuz-3.13.0-92-generic
Found initrd image: /boot/initrd.img-3.13.0-92-generic
Found linux image: /boot/vmlinuz-3.13.0-91-generic
Found initrd image: /boot/initrd.img-3.13.0-91-generic
Found linux image: /boot/vmlinuz-3.13.0-89-generic
Found initrd image: /boot/initrd.img-3.13.0-89-generic
Found linux image: /boot/vmlinuz-3.13.0-88-generic
Found initrd image: /boot/initrd.img-3.13.0-88-generic
Found linux image: /boot/vmlinuz-3.13.0-87-generic
Found initrd image: /boot/initrd.img-3.13.0-87-generic
Found linux image: /boot/vmlinuz-3.13.0-63-generic
Found initrd image: /boot/initrd.img-3.13.0-63-generic
Found linux image: /boot/vmlinuz-3.13.0-62-generic
Found initrd image: /boot/initrd.img-3.13.0-62-generic
Found linux image: /boot/vmlinuz-3.13.0-61-generic
Found initrd image: /boot/initrd.img-3.13.0-61-generic
Found linux image: /boot/vmlinuz-3.13.0-59-generic
Found initrd image: /boot/initrd.img-3.13.0-59-generic
Found linux image: /boot/vmlinuz-3.13.0-57-generic
Found initrd image: /boot/initrd.img-3.13.0-57-generic
Found linux image: /boot/vmlinuz-3.13.0-55-generic
Found initrd image: /boot/initrd.img-3.13.0-55-generic
Found linux image: /boot/vmlinuz-3.13.0-54-generic
Found initrd image: /boot/initrd.img-3.13.0-54-generic
Found memtest86+ image: /memtest86+.elf
Found memtest86+ image: /memtest86+.bin
done
Purging configuration files for linux-image-3.13.0-53-generic (3.13.0-53.89) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-53-generic /boot/vmlinuz-3.13.0-53-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-53-generic /boot/vmlinuz-3.13.0-53-generic

Now, check the disk space, run df command, it’s output will be:

# df -h
Filesystem Size Used Avail Use% Mounted on
udev 2.9G 0 2.9G 0% /dev
tmpfs 586M 9.0M 577M 2% /run
/dev/mapper/all_vg-root 453G 415G 16G 97% /
tmpfs 2.9G 130M 2.8G 5% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 2.9G 0 2.9G 0% /sys/fs/cgroup
/dev/sda1 1.9G 687M 1.1G 40% /boot
cgmfs 100K 0 100K 0% /run/cgmanager/fs
tmpfs 586M 104K 586M 1% /run/user/1002
tmpfs 586M 0 586M 0% /run/user/0

Hooray, the manual method freed another 500 MB from /boot, it enough now to resume our work, and also freed another 2 GB from /

Hint:

1. You should use the two removal method we used her in order.

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

Your email address will not be published. Required fields are marked *