How to remove kernel linux

How to easily remove old kernels in Ubuntu 20.04 LTS?

Have you tried sudo apt autoremove ? Please edit your question and add all the new information. Also indicate if you installed the kernels manually or something special.

Hmmm. Show us complete output. dpkg —list output includes kernels that have already been removed. The first column will indicate which packages are removed ( rc ) and which are installed ( ii ).

There are a couple of excellent kernel removal manager type scripts around. I used the server version from here, for a long time, but now use this one. Note that I can not use autoremove, as it misbehaves when one uses mainline kernels. The manual method becomes tedious when trying to clean up 100 kernels.

11 Answers 11

Here are the steps to remove unused kernels.

Check what current kernel You run:

uname -a Linux blackhole 5.6.13-050613-lowlatency #202005141310 SMP PREEMPT Thu May 14 13:17:41 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux 

I am running 5.6.13-050613-lowlatency

List all installed kernels in Your OS:

dpkg --list | egrep -i --color 'linux-image|linux-headers|linux-modules' | awk '< print $2 >' linux-headers-5.6.11-050611 linux-headers-5.6.11-050611-lowlatency linux-headers-5.6.13-050613 linux-headers-5.6.13-050613-lowlatency linux-image-unsigned-5.6.11-050611-lowlatency linux-image-unsigned-5.6.13-050613-lowlatency linux-modules-5.6.11-050611-lowlatency linux-modules-5.6.13-050613-lowlatency 

Uninstall kernels You don’t need:

sudo apt purge linux-headers-5.6.11-050611 linux-headers-5.6.11-050611-lowlatency linux-image-unsigned-5.6.11-050611-lowlatency linux-modules-5.6.11-050611-lowlatency 

dpkg —list | egrep -i —color ‘linux-image|linux-headers|linux-modules’ | awk ‘< print $2 >‘. Really? Doesn’t ls -1 /boot/vm* list your kernels including the current one?

@PaulBenson Your command does not list packages. So You can’t use these names in apt or dpkg to uninstall kernels.

Isn’t there an automated way to do that? I have more than 50 kernels installed in the list, many with un tag.

While this seems tedious, it appears to be the only viable way. I would have loved to do it automatically, but I am unable to find a nice script for that. My hacky workaround is to use sudo apt-get purge linux-*-*-5.* (feel free to remove one -* to catch some other modules and do the same with 4.* ) and manually check the output. This will get the desired packages, but might be too aggressive — i.e. check the output carefully to avoid a system suicide.

Sorry for the double comment, StackExchange won’t let me edit the comment after the 5 min mark. I have found this nice script: tecmint.com/remove-old-kernel-in-debian-and-ubuntu (i.e. purge-old-kernels as a script included in the package byobu ). This might help, but I have already purged all old kernels so can’t test it for now.

You can try out this script

#!/bin/bash # Run this script without any param for a dry run # Run the script with root and with exec param for removing old kernels after checking # the list printed in the dry run uname -a IN_USE=$(uname -a | awk '< print $3 >') echo "Your in use kernel is $IN_USE" OLD_KERNELS=$( dpkg --list | grep -v "$IN_USE" | grep -Ei 'linux-image|linux-headers|linux-modules' | awk '< print $2 >' ) echo "Old Kernels to be removed:" echo "$OLD_KERNELS" if [ "$1" == "exec" ]; then for PACKAGE in $OLD_KERNELS; do yes | apt purge "$PACKAGE" done else echo "If all looks good, run it again like this: sudo remove_old_kernels.sh exec" fi 

Run it like this for a dry run:

If all looks good, run it again like this:

sudo remove_old_kernels.sh exec 

Whenever I run this script, and it deletes an entry, I am getting stuck unable to press a selection on the menu about modifying grub files. Is there a way to go about fixing that? When I do manual delete of a kernel, I am able to make a selection in that menu to proceed.

sudo apt-get autoremove --purge 

Note that this command won’t remove older kernels only, but any package that is not needed as a dependency of other packages along with its configuration files.

Читайте также:  How to install ftp server in linux

It doesn’t work for 20.04 (upgraded from 18.04, and upgraded from 16.04). I can list the packages, but ubuntu doesn’t autoremove old kernels.

@gavioto20 strange, I also used Ubuntu 20.04 and this command did work for removing old kernels? It will only remove kernels that are not actively used.

An update to @alex Burdusel’s script would be the following:

#!/bin/bash -e # Run this script without any arguments for a dry run # Run the script with root and with exec arguments for removing old kernels and modules after checking # the list printed in the dry run uname -a IN_USE=$(uname -a | awk '< print $3 >') echo "Your in use kernel is $IN_USE" OLD_KERNELS=$( dpkg --get-selections | grep -v "linux-headers-generic" | grep -v "linux-image-generic" | grep -v "linux-image-generic" | grep -v "$" | grep -Ei 'linux-image|linux-headers|linux-modules' | awk '< print $1 >' ) echo "Old Kernels to be removed:" echo "$OLD_KERNELS" OLD_MODULES=$( ls /lib/modules | grep -v "$" | grep -v "$" ) echo "Old Modules to be removed:" echo "$OLD_MODULES" if [ "$1" == "exec" ]; then apt-get purge $OLD_KERNELS for module in $OLD_MODULES ; do rm -rf /lib/modules/$module/ done fi 

This solves the issue that it tries to delete the following packages:

linux-headers-generic linux-image-generic linux-headers-5.17.5-76051705 # if 5.17.5-76051705-generic is the current kernel 

This script was modified to purge all packages at once, and also delete any remaining module directories in /lib/modules/

To easily remove older versions kernels, e.g. kernels starting from 4.0 and so on.

sudo apt-get purge linux-image-4.* 

Ofcourse not, how do you expect that to remove kernels with a starting 5. You can update command accordingly. I just gave an example. For linux-image-5.4.xxx use sudo apt-get purge linux-image-5.4.* and so on.

Just taking Michal’s answer a little further. I didn’t want to type the kernels to delete everytime so I decided to use files instead.

Write all the current kernels you have on a file.

dpkg --list | egrep -i --color 'linux-image|linux-headers|linux-modules' | awk '< print $2 >' > kernels.txt 

Filter your currently used kernel out of the file using grep.

grep -v $(uname -r) kernels.txt > kernels_to_delete.txt 

Verify your current kernel is not present in the delete list. Don’t skip this. Ensures you don’t mistakenly delete all the kernels.

grep $(uname -r) kernels_to_delete.txt 

Delete all the unused kernels in one go.

cat kernels_to_delete.txt | xargs sudo apt purge -y 

autoremove will only remove packages that are automatically installed. If you ever updated or added a kernel package manually autoremove will not remove it. If you ever «held» a kernel version autoremove will not remove it. If you’re wondering why Ubuntu is filling up your boot partition with kernels you no longer use it’s likely one of these two reasons.

# Unhold all packages dpkg --get-selections | grep hold | awk '< print $1, "install" >' | dpkg --set-selections # Mark all "manually installed" kernel packages as "automatically installed" for f in $(apt-mark showmanual | grep linux-); do apt-mark auto $f done # Remove all packages that are no longer needed apt-get -y autoremove --purge 

I derived this script from previous answers. It should reserve the current kernel and one previous kernel. As stated in previous answers, execute once without any arguments for a dry run. Then run as root with 1 argument as ‘exec’ to actually remove the old kernels.

#!/bin/bash -e IN_USE=$(uname -a | awk '< print $3 >') echo "Your in use kernel is $IN_USE" CUR_KERNELS=$(dpkg --get-selections | grep linux-image | grep install | awk '') echo echo "Current Kernels are:" echo "$CUR_KERNELS" OLD_KERNELS=$(dpkg --get-selections | grep linux | grep deinstall | awk '') echo echo "Old Kernels to be removed:" echo "$OLD_KERNELS" if [ "$1" == "exec" ]; then apt-get purge $OLD_KERNELS fi 

As for easy solution, use a utility called linux-purge that is made just for the purpose, and that is supposed to work by any release of Ubuntu later than 12.04. The utility is implemented using Bash.

Читайте также:  Браузер для слабого ноутбука линукс

Here is my answer with more details to another similar question.

Further improvement to the script:

This version filters out the newer kernels. In earlier versions, the newer kernels will also get purged.

This script defaults to «dry-run» but if the $1 is anything other than «dry-run» or «exec» then the usage help text is displayed.

#!/bin/bash -e # Function to print usage instructions print_usage() < echo "Usage: $0 [dry-run|exec]" echo " dry-run: List old kernels and modules without removing them (default)" echo " exec: Remove the listed old kernels and modules (requires root privileges)" ># Function to compare kernel version numbers # Returns 1 if version1 is greater than version2, 0 if equal, and -1 if lesser compare_versions() < local version1=($) local version2=($) for i in ; do if [[ $ -gt $ ]]; then return 1 elif [[ $ -lt $ ]]; then return -1 fi done return 0 > # Check for valid input arguments if [[ $# -gt 1 ]] || < [[ $# -eq 1 ]] && [[ "$1" != "dry-run" ]] && [[ "$1" != "exec" ]]; >; then print_usage exit 1 fi # Display current running kernel uname -a IN_USE=$(uname -a | awk '< print $3 >') echo "Your in-use kernel is $IN_USE" # Find old kernels OLD_KERNELS=$( dpkg --get-selections | grep -v "linux-headers-generic" | grep -v "linux-image-generic" | grep -Ei 'linux-image|linux-headers|linux-modules' | awk '< print $1 >' | grep -v "$" ) # Filter out newer kernels FILTERED_KERNELS="" for kernel in $OLD_KERNELS; do kernel_version=$(echo "$kernel" | grep -oP '(?<=linux-image-|linux-headers-|linux-modules-)3+(\.6+)' || true) if [[ ! -z "$kernel_version" ]]; then compare_versions "$kernel_version" "$IN_USE" if [[ $? -eq -1 ]]; then FILTERED_KERNELS+="$kernel"$'\n' fi else FILTERED_KERNELS+="$kernel"$'\n' fi done OLD_KERNELS="$FILTERED_KERNELS" # Find old modules OLD_MODULES=$( ls /lib/modules | grep -v "$" | while read -r module; do module_version=$(echo "$module" | grep -oP '1+(\.5+)' || true) if [[ ! -z "$module_version" ]]; then compare_versions "$module_version" "$IN_USE" if [[ $? -eq -1 ]]; then echo "$module" fi else echo "$module" fi done ) # Display old kernels and modules echo "Old Kernels to be removed:" echo "$OLD_KERNELS" echo "Old Modules to be removed:" echo "$OLD_MODULES" # Remove old kernels and modules if "exec" argument is passed if [ "$1" == "exec" ]; then # Check for root privileges if [ "$(id -u)" != "0" ]; then echo "Error:This operation requires root privileges. Please run the script as root or use 'sudo'." exit 1 fi # Remove Old Kernel apt-get purge $OLD_KERNELS # Remove Old Modules for module in $OLD_MODULES ; do rm -rf /lib/modules/$module/ done fi 

Источник

How to Remove Old Kernels on Ubuntu

A Linux kernel is the core of the operating system. As new versions of Linux are released, your system updates the kernel.

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

Читайте также:  Linux для терминалов оплаты

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

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

  • A system running Ubuntu (tested on Ubuntu 20.04 and 22.04).
  • A terminal window/command line (Ctrl+Alt+T)
  • A user account with sudo privileges

Removing Old Kernels on Ubuntu

Unused old kernels on Ubuntu take up disk space. Check for old kernel packages with the following command:

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

dpkg list linux-headers and linux-images terminal output

The output shows a list of installed kernels with the following flags:

  • ii - Marked for installation ( i ) and currently installed ( i ). Packages with these flags are safe to remove.
  • rc - Removed ( r ) and configuration files present ( c ). The package is removed, and configuration files require purging.
  • iU - Marked for installation ( i ) and unpacked ( U ) to install at the next reboot. Do not remove packages with this flag.

Do not remove any packages that have the current kernel version. To see the current version, use the uname command with the -r flag:

uname -r terminal output

There are several ways to remove the old kernel packages from the system.

Using apt autoremove Command

Use the apt command with the autoremove option to automatically remove all old kernels and unrequired packages on the system. Run the following command in the terminal:

sudo apt autoremove --purge

The command keeps only the latest and one prior version on the system.

Note: Alternatively, use the apt-get command for the same results. Read about the difference between apt vs. apt-get.

Using apt remove Command

The apt remove command removes a specified package without removing the dependencies. Use the following commands:

sudo apt remove --purge linux-headers-[version]
sudo apt remove --purge linux-image-[version]

sudo apt remove --purge linux image kernel terminal output

Press Y to confirm the deletion and wait for the process to complete.

Using GUI

A GUI and graphical tools providee a simple overview and help prevent costly mistakes. This section focuses on how to remove old kernels using Synaptic. The program is a graphical front-end for the apt package manager.

Follow the steps below to install Synaptic and remove old kernels.

Install and Run Synaptic

1. Install Synaptic with the following:

sudo apt install synaptic -y

The installation takes a few moments to complete.

2. Launch the Synaptic interface from the terminal by typing:

The Synaptic Package Manager graphical interface opens in a new window.

Remove Old Kernels

1. Click the Sections button in the left menu.

Location of the Section tab in the Synaptic GUI.

2. Locate the Kernel and modules option on the list.

3. Right-click the kernel to remove in the left menu and select the Mark for Complete Removal option.

Select Kernel and Modules and select the kernels to remove completly.

Synaptic lists additional dependency packages for the kernel.

If necessary the systems infroms you that dependent packages need to be removed as well.

4. Click the Mark option to mark the dependencies for removal.

5. Review the marked packages and click Apply to mark the packages for removal.

The kernels are marked and click the Apply button.

6. Click Apply in the pop-up Summary window to start the removal.

List of changes for you to confirm.

7. A progress bar and the details window show the removal process. Check the box to close the window automatically after removal.

Progress bar for the removal of th old kernels. Process completed.

Alternatively, close the window manually once the removal completes.

You know several methods to remove old kernels from Ubuntu 20.04 or 22.04. Purging old kernels can help you recover wasted disk space in Linux. It’s also considered a best practice to prune out old files and dependencies.

Источник

Оцените статью
Adblock
detector