为什么apt-get autoremove无法删除我的旧内核?

根据Ubuntu帮助维基:的说法:
您可以在终端中使用简单的autoremove命令删除旧内核:
sudo apt-get autoremove --purge 由于某种原因,这对我不起作用,内核正在堆积。
 apt list --installed | rg linux-image

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

linux-image-5.4.0-31-generic/focal-updates,focal-security,now 5.4.0-31.35 amd64 [installed]
linux-image-5.4.0-40-generic/focal-updates,focal-security,now 5.4.0-40.44 amd64 [installed]
linux-image-5.4.0-42-generic/focal-updates,focal-security,now 5.4.0-42.46 amd64 [installed]
linux-image-5.4.0-45-generic/focal-updates,focal-security,now 5.4.0-45.49 amd64 [installed]
linux-image-5.4.0-47-generic/focal-updates,focal-security,now 5.4.0-47.51 amd64 [installed]
linux-image-5.4.0-48-generic/focal-updates,focal-security,now 5.4.0-48.52 amd64 [installed]
linux-image-5.4.0-52-generic/focal-updates,focal-security,now 5.4.0-52.57 amd64 [installed]
linux-image-5.4.0-54-generic/focal-updates,now 5.4.0-54.60 amd64 [installed]
linux-image-5.4.0-56-generic/now 5.4.0-56.62 amd64 [installed,local]
linux-image-5.4.0-58-generic/focal-updates,focal-security,now 5.4.0-58.64 amd64 [installed]
linux-image-5.4.0-60-generic/focal-updates,focal-security,now 5.4.0-60.67 amd64 [installed]
linux-image-5.4.0-62-generic/focal-updates,focal-security,now 5.4.0-62.70 amd64 [installed]
linux-image-5.4.0-64-generic/focal-updates,now 5.4.0-64.72 amd64 [installed]
linux-image-5.4.0-65-generic/focal-updates,focal-security,now 5.4.0-65.73 amd64 [installed]
linux-image-5.4.0-66-generic/focal-updates,focal-security,now 5.4.0-66.74 amd64 [installed,automatic]
linux-image-generic/now 5.4.0.66.69 amd64 [installed,upgradable to: 5.4.0.67.70]

我正在一台新建的机器上运行Kubuntu 20.04.2,使用了几个月,之前多年使用Mac,所以我从未有意识地尝试过调整内核的任何内容。

我通过Discover商店禁用了unattended-upgrades,因为静默的Nvidia更新导致显示器冻结。不过,每次更新后我会运行sudo apt-get autoremove

在发布问题之前,我寻找了答案,并发现大多数答案都是很多年前的,尽管this previous question的一个答案让我检查了我的/etc/apt/apt.conf.d/01autoremove-kernels文件,它似乎正确地只关注最近的两个内核:

// DO NOT EDIT! File autogenerated by /etc/kernel/postinst.d/apt-auto-removal
APT::NeverAutoRemove
{
   "^linux-.*-5\.4\.0-65-generic$";
   "^linux-.*-5\.4\.0-66-generic$";
   "^kfreebsd-.*-5\.4\.0-65-generic$";
   "^kfreebsd-.*-5\.4\.0-66-generic$";
   "^gnumach-.*-5\.4\.0-65-generic$";
   "^gnumach-.*-5\.4\.0-66-generic$";
   "^.*-modules-5\.4\.0-65-generic$";
   "^.*-modules-5\.4\.0-66-generic$";
   "^.*-kernel-5\.4\.0-65-generic$";
   "^.*-kernel-5\.4\.0-66-generic$";
};

所以,我超级困惑,正在寻找一种在不破坏任何东西的情况下清理系统的方法,然后将保留的内核数量限制在2-3个。
谢谢您的阅读,我会非常感激您能提供的任何帮助!
编辑:根据@user535733的要求,这里有一些额外的输出:
sudo apt update
Hit:1 http://us.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease                                                                                              
Hit:3 http://security.ubuntu.com/ubuntu focal-security InRelease                                                                      
Hit:4 http://us.archive.ubuntu.com/ubuntu focal-backports InRelease                                                                   
Ign:5 https://apt.fury.io/flirc  InRelease                                                
Hit:6 http://ppa.launchpad.net/appimagelauncher-team/stable/ubuntu focal InRelease
Hit:7 http://download.opensuse.org/repositories/home:/ungoogled_chromium/Ubuntu_Focal  InRelease
Ign:8 https://apt.fury.io/flirc  Release                                                       
Ign:9 https://apt.fury.io/flirc  Packages                                
Ign:10 https://apt.fury.io/flirc  Translation-en                         
Hit:11 http://ppa.launchpad.net/jonaski/strawberry/ubuntu focal InRelease
Ign:12 https://apt.fury.io/flirc  Translation-en_US                      
Get:9 https://apt.fury.io/flirc  Packages                                
Ign:10 https://apt.fury.io/flirc  Translation-en                         
Hit:13 http://ppa.launchpad.net/micahflee/ppa/ubuntu focal InRelease
Ign:12 https://apt.fury.io/flirc  Translation-en_US                      
Ign:10 https://apt.fury.io/flirc  Translation-en                         
Ign:12 https://apt.fury.io/flirc  Translation-en_US                      
Hit:14 http://ppa.launchpad.net/mkusb/ppa/ubuntu focal InRelease
Ign:10 https://apt.fury.io/flirc  Translation-en
Ign:12 https://apt.fury.io/flirc  Translation-en_US
Ign:10 https://apt.fury.io/flirc  Translation-en
Ign:12 https://apt.fury.io/flirc  Translation-en_US
Ign:10 https://apt.fury.io/flirc  Translation-en
Ign:12 https://apt.fury.io/flirc  Translation-en_US
Ign:10 https://apt.fury.io/flirc  Translation-en
Ign:12 https://apt.fury.io/flirc  Translation-en_US
Fetched 6,518 B in 2s (3,153 B/s)
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.

sudo apt upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

sudo apt autoremove
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

uname -r
5.4.0-67-generic

编辑2:在更新以包括额外输出后,又添加了一个内核(linux-image-5.4.0-67-generic)。我注意到,linux-image-5.4.0-66-generic和linux-image-5.4.0-67-generic都被标记为自动安装,而所有之前的版本都被标记为手动安装。根据时间推断,我猜测通过Discover商店中的“全部更新”按钮进行更新似乎导致内核被标记为手动安装(连同头文件和模块一起)。看起来只有通过命令行安装的两个版本被正确标记为自动安装(但由于它们是最新版本,所以没有被自动删除)。
我想答案是对通过Discover商店安装的版本使用apt-mark auto命令,并从此处开始使用命令行。感谢所有帮助过我的人!

请使用 apt-mark showmanual 命令并在 Linux 上使用 grep 进行搜索。抱歉,我正在使用手机回复。 - nobody
这里有一个非常好的内核删除管理工具链接。我已经使用它多年了,而且只用它。 - Doug Smythies
@nobody 谢谢!apt-mark showmanual 列出了内核被手动安装的情况。我不知道为什么会这样。这是一个标准安装,一开始我使用 Discover 商店进行更新,但几个月后我转换到命令行界面以便在更新过程中获取更多信息。 - buntubunny
请编辑您的问题,以显示完整的 sudo apt updatesudo apt upgradesudo apt autoremoveuname -r 的输出。 - user535733
1这个回答解决了你的问题吗?为什么apt-get autoremove不会删除我的旧内核? - nobody
1这个回答解决了你的问题吗?如何删除旧的内核版本以清理引导菜单? - Elder Geek
1个回答

所以,@nobody建议使用apt-mark showmanual,这对我来说是新的。

由于这是我第一次接触linux,我在最初的几个月里一直使用Kubuntu的Discover Store更新所有内容。

看起来通过Kubuntu的Discover store的'update all'按钮安装的所有内核,以及相关的模块和头文件,都被标记为手动安装。

幸运的是,命令行更新正确地将新内核、头文件和模块标记为自动安装。

使用从apt-mark showmanual | grep linux-中列出的错误标记软件包的列表,我能够轻松地用sudo apt-mark auto $ (cat path/to/listofpackages.txt)更正所有65个软件包的状态,感谢这里发布的建议

现在,sudo apt-get autoremove正在检测这些软件包,我会确保以后坚持使用cli更新! 再次感谢所有帮助我的人!

sudo apt-get autoremove
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  linux-headers-5.4.0-40 linux-headers-5.4.0-40-generic linux-headers-5.4.0-42 linux-headers-5.4.0-42-generic linux-headers-5.4.0-45
  linux-headers-5.4.0-45-generic linux-headers-5.4.0-47 linux-headers-5.4.0-47-generic linux-headers-5.4.0-48 linux-headers-5.4.0-48-generic
  linux-headers-5.4.0-52 linux-headers-5.4.0-52-generic linux-headers-5.4.0-54 linux-headers-5.4.0-54-generic linux-headers-5.4.0-56
  linux-headers-5.4.0-56-generic linux-headers-5.4.0-58 linux-headers-5.4.0-58-generic linux-headers-5.4.0-60 linux-headers-5.4.0-60-generic
  linux-headers-5.4.0-62 linux-headers-5.4.0-62-generic linux-headers-5.4.0-64 linux-headers-5.4.0-64-generic linux-headers-5.4.0-65
  linux-headers-5.4.0-65-generic linux-image-5.4.0-40-generic linux-image-5.4.0-42-generic linux-image-5.4.0-45-generic linux-image-5.4.0-47-generic
  linux-image-5.4.0-48-generic linux-image-5.4.0-52-generic linux-image-5.4.0-54-generic linux-image-5.4.0-56-generic linux-image-5.4.0-58-generic
  linux-image-5.4.0-60-generic linux-image-5.4.0-62-generic linux-image-5.4.0-64-generic linux-image-5.4.0-65-generic linux-modules-5.4.0-40-generic
  linux-modules-5.4.0-42-generic linux-modules-5.4.0-45-generic linux-modules-5.4.0-47-generic linux-modules-5.4.0-48-generic linux-modules-5.4.0-52-generic
  linux-modules-5.4.0-54-generic linux-modules-5.4.0-56-generic linux-modules-5.4.0-58-generic linux-modules-5.4.0-60-generic linux-modules-5.4.0-62-generic
  linux-modules-5.4.0-64-generic linux-modules-5.4.0-65-generic linux-modules-extra-5.4.0-40-generic linux-modules-extra-5.4.0-42-generic
  linux-modules-extra-5.4.0-45-generic linux-modules-extra-5.4.0-47-generic linux-modules-extra-5.4.0-48-generic linux-modules-extra-5.4.0-52-generic
  linux-modules-extra-5.4.0-54-generic linux-modules-extra-5.4.0-56-generic linux-modules-extra-5.4.0-58-generic linux-modules-extra-5.4.0-60-generic
  linux-modules-extra-5.4.0-62-generic linux-modules-extra-5.4.0-64-generic linux-modules-extra-5.4.0-65-generic
0 upgraded, 0 newly installed, 65 to remove and 0 not upgraded.
After this operation, 4,682 MB disk space will be freed.
Do you want to continue? [Y/n]