How to Remove Old Kernels on Ubuntu 16.04, 18.04, and 19.04

Introduction

In Linux, the kernel is the core of the operating system. As new versions of Linux are released, your system may update to a newer kernel.

By default, modern Linux versions keep the current kernel, plus one older version. However, in some instances, Linux doesn’t remove old versions of the kernel. One common problem of having old kernels is having an extensive list of bootable kernels on the GRUB (boot) menu.

This guide will help you remove old and unused Linux kernels on your Ubuntu system.

Tutorial on how to remove old or unused kernels on Ubuntu 18.04.

Prerequisites

  • A system running Ubuntu 19.04, 18.04, 16.04
  • A terminal window / command line (Ctrl+Alt+T, search > terminal)
  • A user account with sudo privileges

Remove Old Kernel Ubuntu 18.04 and 19.04

Display a List of Kernel Versions

To view a list of all kernel versions installed,  entering the following:

sudo dpkg ––list | egrep –i ––color ‘linux-image|linux-headers’

The output shows a list of all files labeled linux-image or linux-header. Use this to get an approximate number of old kernels on your system.

You can also count the number of kernels with the command:

sudo dpkg ––list | egrep –i ––color ‘linux-image|linux-headers’ | wc –l

This command displays the total number of both linux-image and linux-headers.

Displaying the number of linux images and Linux headers on Ubuntu

Need to find out which Ubuntu Kernel version is running?

Remove All Old Kernels Using the Command Line

The apt package manager can automatically remove all old kernels. To do so, run the command:

sudo apt-get ––purge autoremove

The system scans for unused kernels and displays a summary of the files it wants to delete. It prompts you to confirm your choice to remove old kernels by pressing y and Enter to delete.

Note: If you still haven’t had the chance, check out the newest features and improvements from the Linux Kernel 5.0 or Linux Kernel 5.7 upgrades.

Manually Remove Individual Kernels from Command-Line

You can manually remove an individual kernel by naming it specifically.

1. First, list the kernels with their installation status:

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

The first two letters for each kernel indicate its status:

  • rc – already removed
  • ii – installed, eligible for removal
  • iU – downloaded and queued for installation
List kernels with installation status on Linux to find kernels available for removal.

2. Remove a kernel with the ii status:

sudo dpkg ––purge [kernel_version]

Make sure you type the exact name and number of the kernel you want to remove. For example:

sudo dpkg ––purge linux-image-5.3.0-28-generic

Use Graphical Tools to Remove Old Kernels in Ubuntu 18.04.

The command-line interface can, at times, be difficult to navigate when working with large sets of data. Using additional graphical tools can give you a better perspective and prevent you from making costly mistakes.

This section focuses on how to remove old kernels using Synaptic and Ubuntu Cleaner on Ubuntu 18.04. These tools can remove a wide variety of unneeded data, such as cached web browser histories, old kernels, and other unwanted software.

How to Remove an Old Kernel with Synaptic

Synaptic is a graphical front-end for the apt package manager, and it allows you to install, remove, and upgrade software packages.

Like most tools of this type, it’s necessary to locate the software packages you intend to remove, mark them for removal, and confirm your choice to complete the removal process.

Install Synaptic by entering the following command in your command line:

sudo apt install synaptic -y

The installation takes a few moments to complete. Launch the Synaptic interface from your terminal by typing:

sudo synaptic

You now have access to the Synaptic Package Manager graphical interface.

1. Click on the Sections tab.

Location of the Section tab in the Synaptic GUI.
  1. Locate the Kernel and modules option from the list.
  2. Click the Kernel you want to remove and select the Mark for Complete Removal option.
Select Kernel and Modules and select the kernels to remove completly.
  1. Synaptic informs you that dependent packages need to be removed as well. Luckily, Synaptic automatically marks them for you once you click the Mark option.
If necessary the systems infroms you that dependent packages need to be removed as well.
  1. Review the marked packages and click Apply once you are satisfied.
The kernels are marked and click the Apply button.
  1. To complete the removal process, click Apply in the pop-up Summary window.
List of changes for you to confirm.
  1. A progress bar allows you to control and review the removal process. Close the window manually once the system informs it has successfully removed the old kernels. Alternatively, check the box to auto close once the process is complete.
Progress bar for the removal of th old kernels. Process completed.

How to Remove an Old Kernel with Ubuntu Cleaner

Ubuntu Cleaner allows you to maintain your system at optimal levels by removing unnecessary browser caches, packages, and software installers.

This open-source software is not available from official repositories and needs to be retrieved as a PPA and then installed.

Note: The Personal Package Archives (PPA) is a specialized software repository intended for non-standard software and software updates.

Enter the following command in your terminal to add the Ubuntu Cleaner PPA:

sudo add-apt-repository ppa:gerardpuig/ppa

After adding the PPA, install Ubuntu Cleaner:

sudo apt install ubuntu-cleaner -y

Once the installations process runs its course, type the following command to access Ubuntu Cleaner:

sudo ubuntu-cleaner

Use the Ubuntu Cleaner graphical interface to remove the old kernels:

  1. Check the Old Kernel box.
  2. Select the kernels you want to remove completely.
  3. Click the Clean button.
Location of the options necessary to remove old kernels with the Ubuntu Cleaner.
  1. The Ubuntu Cleaner tool might inform you that other changes are necessary to complete the process. Click Continue if you agree.
Ubuntu Cleaner informs you that additional packages need to be removed before proceeding.
  1. The old kernel removal process is now in progress.
A progress bar infroms you how quickly the kernel removal is proceeding.
  1. Once the process is complete, you receive the Cool! Your system is clean! message as confirmation.
Ubuntu Cleaner informs you that the system has been cleaned.

How to Delete Old Unused Kernels in Ubuntu 16.04 and Older

By default, previous Linux versions didn’t include tools to manage old kernels.

1. To remove old Linux kernels, you first need to install the byobu tool:

sudo apt-get install byobu

2. Then, run the utility by entering the following:

sudo purge-old-kernels

The utility scans your system and removes all but the latest two kernels. If you want to keep more than the default two, use the ––keep option:

sudo purge-old-kernels ––keep 3 –qy

For more information on the byobu tool, enter the following:

man purge-old-kernels

Note: Do not remove the current Linux kernel in use. Doing so could render your system unable to boot. Use the uname -r command if you need your current kernel version.

Remove Old Kernels Using Ubuntu Software Center

In older versions of Ubuntu, you can remove kernels manually using the Software Center. This option only works if you’re running a graphical interface (GUI).

1. Open the Ubuntu Software Center.

2. Enable the search bar and search for linux-image.

3. In the lower-left corner, you should see a hyperlink for Show xx technical items. Click this link to display the list of Linux kernels.

4. Refer back to your current kernel, which needs to remain on the system. Select any of the older versions and click the Remove button.

5. Refresh the GRUB (boot) menu by entering the following into a terminal window:

sudo update-grub

Leave at least one previous kernel on your system. Your system was proven to be stable with that kernel. If you have difficulties after a software update, you can revert to the previous (working) Linux kernel.

Note: If you ever encounter a boot failure, visit our guide How to Use Grub Rescue to Fix Linux Boot Failure.

Conclusion

You should now know several methods to remove old kernels from Ubuntu 19.04, 18.04, & 16.04 or older. Purging kernels can help you recover wasted disk space in linux. It’s also considered a best practice for good system hygiene, to prune out old files and dependencies.

原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/224154.html

(0)
上一篇 2022年1月7日
下一篇 2022年1月7日

相关推荐

发表回复

登录后才能评论