如何轻松删除 Ubuntu 20.04 LTS 中的旧内核?

MeH*_*SaN 13 kernel software-uninstall

我有我不使用的旧 Linux 内核版本,所以我试图删除它们。

安装的内核列表来自 dpkg --list | grep linux-image

linux-image-5.4.0-26-generic (5.4.0-26.30)   
linux-image-5.4.0-33-generic (5.4.0-33.37)
linux-image-5.4.0-37-generic (5.4.0-37.41)
Run Code Online (Sandbox Code Playgroud)

Ale*_*sel 40

你可以试试这个脚本

删除_旧_内核.sh

#!/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 Code Online (Sandbox Code Playgroud)

像这样运行它进行试运行:

remove_old_kernels.sh
Run Code Online (Sandbox Code Playgroud)

如果一切看起来都不错,请像这样再次运行:

sudo remove_old_kernels.sh exec
Run Code Online (Sandbox Code Playgroud)


Mic*_*icz 22

以下是删除未使用内核的步骤。

检查当前运行的内核:

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
Run Code Online (Sandbox Code Playgroud)

我在跑步 5.6.13-050613-lowlatency

列出您的操作系统中所有已安装的内核:

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
Run Code Online (Sandbox Code Playgroud)

卸载内核您不需要:

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
Run Code Online (Sandbox Code Playgroud)

  • 没有自动的方法来做到这一点吗?我在列表中安装了 50 多个内核,其中许多带有 `un` 标签。 (3认同)

小智 13

只是进一步考虑米哈尔的回答。我不想每次都输入要删除的内核,所以我决定使用文件。

将当前拥有的所有内核写入一个文件。

dpkg --list | egrep -i --color 'linux-image|linux-headers|linux-modules' | awk '{ print $2 }' > kernels.txt
Run Code Online (Sandbox Code Playgroud)

使用 grep 从文件中过滤出当前使用的内核。

grep -v $(uname -r) kernels.txt > kernels_to_delete.txt
Run Code Online (Sandbox Code Playgroud)

验证您当前的内核不存在于删除列表中。不要跳过这个。确保您不会错误地删除所有内核。

grep $(uname -r) kernels_to_delete.txt
Run Code Online (Sandbox Code Playgroud)

一次性删除所有未使用的内核。

cat kernels_to_delete.txt | xargs sudo apt purge -y
Run Code Online (Sandbox Code Playgroud)


eit*_*tch 8

@alex Burdusel 脚本的更新如下:

#!/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 "${IN_USE%%-generic}" |
        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 "${IN_USE%%-generic}" |
    grep -v "${IN_USE}"
)
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
Run Code Online (Sandbox Code Playgroud)

这解决了它尝试删除以下包的问题:

linux-headers-generic
linux-image-generic
linux-headers-5.17.5-76051705 # if 5.17.5-76051705-generic is the current kernel
Run Code Online (Sandbox Code Playgroud)

该脚本被修改为一次清除所有包,并删除 /lib/modules/ 中任何剩余的模块目录


小智 7

只需使用这个:

sudo apt-get autoremove --purge
Run Code Online (Sandbox Code Playgroud)

  • 它不适用于 20.04(从 18.04 升级,从 16.04 升级)。我可以列出软件包,但 ubuntu 不会自动删除旧内核。 (16认同)
  • 请注意,此命令不会仅删除较旧的内核,还会删除不需要作为其他包及其配置文件的依赖项的任何包。 (7认同)

shi*_*ngh 5

轻松删除旧版本内核,例如从 4.0 开始的内核等。

sudo apt-get purge linux-image-4.*
Run Code Online (Sandbox Code Playgroud)

  • 当然不是,您如何期望删除以 5 开头的内核。您可以相应地更新命令。我只是举了一个例子。对于 linux-image-5.4.xxx 使用 `sudo apt-get purge linux-image-5.4.*` 等等。 (3认同)