Ubuntu 18.04 remove all unused old kernels

ADVERTISEMENTS



Ubuntu 18.04 remove kernel that is not used

How do I remove all unused old kernels in Ubuntu Linux 18.04 LTS using the command line?Over time you get many Linux kernel packages installed on Ubuntu Linux 16.04 or 18.04 LTS server. All unused Linux kernel takes disk space on Ubuntu server, hence you must delete them from the system. This page shows you how to remove old kernels from an Ubuntu server install.

The procedure to delete all unused old kernels on Ubuntu Linux is as follows:

First, boot into a new kernel List all other older kernel using the dpkg command Note down system disk space usage by running the df -H command Delete all unused old kernels, run: sudo apt --purge autoremove Verify it

How to list all installed Linux kernel

Simply type the following dpkg command along with egrp command:

sudo dpkg --list | egrep -i --color 'linux-image|linux-headers'



sudo dpkg --list | egrep -i --color 'linux-image|linux-headers' | wc -l

57

One can count kernel image that are installed on your system using the wc command:Sample outputs:

Also note down the amount of disk space available on the file system by typing the following df command:

df -H



How to remove old kernel versions on Ubuntu 18.04 or above

Type the following apt command or apt-get command:

sudo apt --purge autoremove

OR

sudo apt-get --purge autoremove



The above commands only work on newer systems as all old Linux kernels and headers automatically flagged as no more needed, and thus can be purged.

Dealing with Ubuntu 16.04 LTS and older

First, get list of currently running Linux kernel, run:

v="$(uname -r | awk -F '-virtual' '{ print $1}')"

echo "$v"

Sample outputs:

4.15.0-65-generic

Next, I am going to create a list that tells not to delete currently running Linux kernel as follows:

i="linux-headers-virtual|linux-image-virtual|linux-headers-generic-hwe-|linux-image-generic-hwe-|linux-headers-${v}|linux-image-$(uname -r)|linux-image-generic|linux-headers-generic"

echo "$i"

Sample outputs (the following kernel I am going to keep on my systems):

linux-headers-virtual|linux-image-virtual|linux-headers-generic-hwe-|linux-image-generic-hwe-|linux-headers-4.15.0-65-generic|linux-image-4.15.0-65-generic|linux-image-generic|linux-headers-generic

Here is a list of the kernel that needs to be deleted:

dpkg --list | egrep -i 'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i"

Sample outputs:

linux-headers-4.15.0- 45 linux-headers-4.15.0- 45 -generic linux-headers-4.15.0- 64 linux-headers-4.15.0- 64 -generic linux-headers-4.15.0- 65 linux-headers-4.4.0- 165 linux-headers-4.4.0- 165 -generic linux-image-4.10.0- 27 -generic linux-image-4.10.0- 33 -generic linux-image-4.10.0- 35 -generic linux-image-4.10.0- 37 -generic linux-image-4.10.0- 38 -generic linux-image-4.10.0- 40 -generic linux-image-4.10.0- 42 -generic linux-image-4.13.0- 26 -generic linux-image-4.13.0- 31 -generic linux-image-4.13.0- 36 -generic linux-image-4.13.0- 37 -generic linux-image-4.13.0- 38 -generic linux-image-4.13.0- 39 -generic linux-image-4.13.0- 41 -generic linux-image-4.13.0- 43 -generic linux-image-4.13.0- 45 -generic linux-image-4.15.0- 30 -generic linux-image-4.15.0- 32 -generic linux-image-4.15.0- 33 -generic linux-image-4.15.0- 34 -generic linux-image-4.15.0- 36 -generic linux-image-4.15.0- 38 -generic linux-image-4.15.0- 39 -generic linux-image-4.15.0- 42 -generic linux-image-4.15.0- 43 -generic linux-image-4.15.0- 45 -generic linux-image-4.15.0- 64 -generic linux-image-4.4.0- 165 -generic linux-headers-4.15.0-45 linux-headers-4.15.0-45-generic linux-headers-4.15.0-64 linux-headers-4.15.0-64-generic linux-headers-4.15.0-65 linux-headers-4.4.0-165 linux-headers-4.4.0-165-generic linux-image-4.10.0-27-generic linux-image-4.10.0-33-generic linux-image-4.10.0-35-generic linux-image-4.10.0-37-generic linux-image-4.10.0-38-generic linux-image-4.10.0-40-generic linux-image-4.10.0-42-generic linux-image-4.13.0-26-generic linux-image-4.13.0-31-generic linux-image-4.13.0-36-generic linux-image-4.13.0-37-generic linux-image-4.13.0-38-generic linux-image-4.13.0-39-generic linux-image-4.13.0-41-generic linux-image-4.13.0-43-generic linux-image-4.13.0-45-generic linux-image-4.15.0-30-generic linux-image-4.15.0-32-generic linux-image-4.15.0-33-generic linux-image-4.15.0-34-generic linux-image-4.15.0-36-generic linux-image-4.15.0-38-generic linux-image-4.15.0-39-generic linux-image-4.15.0-42-generic linux-image-4.15.0-43-generic linux-image-4.15.0-45-generic linux-image-4.15.0-64-generic linux-image-4.4.0-165-generic

How to delete old kernels in Ubuntu

Finally, I am going to delete all unwated and older Linux kernels on my Ubuntu Linux 16.04 LTS server, run:

$ sudo -i

# apt-get --purge remove $(dpkg --list | egrep -i 'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i")



Result

Again run the df command:

df -H

Sample outputs:

Filesystem Size Used Avail Use% Mounted on udev 1.1G 0 1.1G 0 % /dev tmpfs 210M 6.0M 204M 3 % /run /dev/vda1 42G 6.7G 35G 17 % / tmpfs 1.1G 0 1.1G 0 % /dev/shm tmpfs 5.3M 0 5.3M 0 % /run/lock tmpfs 1.1G 0 1.1G 0 % /sys/fs/cgroup tmpfs 103k 0 103k 0 % /var/lib/lxd/shmounts tmpfs 103k 0 103k 0 % /var/lib/lxd/devlxd tmpfs 210M 0 210M 0 % /run/user/ 0 Filesystem Size Used Avail Use% Mounted on udev 1.1G 0 1.1G 0% /dev tmpfs 210M 6.0M 204M 3% /run /dev/vda1 42G 6.7G 35G 17% / tmpfs 1.1G 0 1.1G 0% /dev/shm tmpfs 5.3M 0 5.3M 0% /run/lock tmpfs 1.1G 0 1.1G 0% /sys/fs/cgroup tmpfs 103k 0 103k 0% /var/lib/lxd/shmounts tmpfs 103k 0 103k 0% /var/lib/lxd/devlxd tmpfs 210M 0 210M 0% /run/user/0

Now I have used 6.7G disk space as compared 9.3G before deleting kernels. In other words, I gained 2G disk free space.

Conclusion

And there you have it. You successfully removed old kernel and header packages from the Ubuntu Linux system. On really older Ubuntu system, we had a command named purge-old-kernels but t is now deprecated. Hence, you must use the apt/apt-get.