When you install new Kernel, the old Kernel will not remove because It can help you to boot server with old kernel if you do any mistake while installing the new Kernel. Also, old kernel consume some spaces which can be useful for something else. So In this article we will provide you multiple methods to remove the old Kernel from your server.
In order to avoid any mistake first check the version of Kernel running on your system.
# uname -sr
List of Kernel
To list all installed kernels on your system, issue this command.
# dpkg -l | grep linux-image
rc linux-image-4.15.0-29-generic 4.15.0-29.31 amd64 Signed kernel image generic ii linux-image-4.15.0-43-generic 4.15.0-43.46 amd64 Signed kernel image generic ii linux-image-4.15.0-45-generic 4.15.0-45.48 amd64 Signed kernel image generic ii linux-image-generic 188.8.131.52.47 amd64 Generic Linux kernel image
Note: There are some status when you check the installed Kernel.
ii: means that the kernel/packages are installed and eligible for removal. rc: indicates that the kernel has already been removed. iU: is something like a warning tells to DON’T REMOVE. That means not installed but queued for install in apt.
Remove Old Kernels
Method 1: Run the commands below to remove a old Kernel along with its configuration files, then use update-grub2 command to update grub2 configuration.
Method 2: You can also use handy script called “byobu” to remove the old Kernel. This is combines of all the commands into a single script with useful options such as specifying number of kernels to keep on the system.
Install byobu package is very easy it is available in existing repository. Use apt command to install the package.
# sudo apt install byobu
Use mention command to remove the old Kernel. The mention command kept 2 Kernels and remove the remaining Kernel.
# sudo purge-old-kernels --keep 2
Method 3: We found an interesting script which helps you to remove the old Kernel. Please follow mention steps to remove old Kernel using script.
root@srv:~/kernel-purge# ./kernel-purge.sh Running kernel version is: 4.15.0-45 The following (unused) KERNEL packages will be removed: linux-headers-4.15.0-43 linux-headers-4.15.0-43-generic linux-image-4.15.0-43-generic linux-modules-4.15.0-43-generic linux-modules-extra-4.15.0-43-generic Do you want to continue [yN]? y Reading package lists… Done Building dependency tree Reading state information… Done The following packages will be REMOVED: linux-headers-4.15.0-43* linux-headers-4.15.0-43-generic* linux-image-4.15.0-43-generic* linux-modules-4.15.0-43-generic* linux-modules-extra-4.15.0-43-generic* 0 upgraded, 0 newly installed, 5 to remove and 0 not upgraded. After this operation, 334 MB disk space will be freed. (Reading database … 160005 files and directories currently installed.) Removing linux-headers-4.15.0-43-generic (4.15.0-43.46) … Removing linux-headers-4.15.0-43 (4.15.0-43.46) … Removing linux-modules-extra-4.15.0-43-generic (4.15.0-43.46) … Removing linux-image-4.15.0-43-generic (4.15.0-43.46) … I: /vmlinuz.old is now a symlink to boot/vmlinuz-4.15.0-45-generic I: /initrd.img.old is now a symlink to boot/initrd.img-4.15.0-45-generic /etc/kernel/postrm.d/initramfs-tools: update-initramfs: Deleting /boot/initrd.img-4.15.0-43-generic /etc/kernel/postrm.d/zz-update-grub: Sourcing file `/etc/default/grub' Generating grub configuration file … Found linux image: /boot/vmlinuz-4.15.0-45-generic Found initrd image: /boot/initrd.img-4.15.0-45-generic Found memtest86+ image: /boot/memtest86+.elf Found memtest86+ image: /boot/memtest86+.bin done Removing linux-modules-4.15.0-43-generic (4.15.0-43.46) … (Reading database … 124388 files and directories currently installed.) Purging configuration files for linux-modules-4.15.0-43-generic (4.15.0-43.46) … dpkg: warning: while removing linux-modules-4.15.0-43-generic, directory '/lib/modules/4.15.0-43-generic' not empty so not removed Purging configuration files for linux-modules-extra-4.15.0-43-generic (4.15.0-43.46) … Purging configuration files for linux-image-4.15.0-43-generic (4.15.0-43.46) … root@srv:~/kernel-purge#