如何删除 Debian 和 Ubuntu 中旧的未使用的内核
在上一篇文章中,我们解释了如何删除 CentOS/RHEL/Fedora 中旧的未使用的内核。在本文中,我们将解释如何删除 Debian 和 Ubuntu 系统中旧的未使用的内核,但在进一步操作之前,您可能需要安装最新版本以便利用:安全修复、新内核功能、更新的驱动程序等多得多。
要将 Ubuntu 和 Debian 中的内核升级到最新版本,请遵循以下指南:
- 如何在 Ubuntu 中将内核升级到最新版本
重要:建议保留至少一两个旧内核,以备更新出现问题时使用。
要查明系统上运行的 Linux 内核的当前版本,请使用以下命令。
uname -sr
Linux 4.12.0-041200-generic
要列出系统上所有已安装的内核,请发出此命令。
dpkg -l | grep linux-image | awk '{print$2}'
linux-image-4.12.0-041200-generic
linux-image-4.8.0-22-generic
linux-image-extra-4.8.0-22-generic
linux-image-generic
删除 Debian 和 Ubuntu 上旧的未使用的内核
运行以下命令删除特定的 linux-image 及其配置文件,然后更新 grub2 配置,最后重新启动系统。
sudo apt remove --purge linux-image-4.4.0-21-generic
sudo update-grub2
sudo reboot
[sudo] password for tecmint:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
linux-generic linux-headers-4.8.0-59 linux-headers-4.8.0-59-generic linux-headers-generic linux-image-4.8.0-59-generic linux-image-extra-4.8.0-59-generic linux-image-generic
Suggested packages:
fdutils linux-doc-4.8.0 | linux-source-4.8.0 linux-tools
Recommended packages:
thermald
The following packages will be REMOVED:
linux-image-4.8.0-22-generic* linux-image-extra-4.8.0-22-generic*
The following NEW packages will be installed:
linux-headers-4.8.0-59 linux-headers-4.8.0-59-generic linux-image-4.8.0-59-generic linux-image-extra-4.8.0-59-generic
The following packages will be upgraded:
linux-generic linux-headers-generic linux-image-generic
3 upgraded, 4 newly installed, 2 to remove and 182 not upgraded.
Need to get 72.0 MB of archives.
After this operation, 81.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us.archive.ubuntu.com/ubuntu yakkety-updates/main amd64 linux-headers-4.8.0-59 all 4.8.0-59.64 [10.2 MB]
Get:2 http://us.archive.ubuntu.com/ubuntu yakkety-updates/main amd64 linux-headers-4.8.0-59-generic amd64 4.8.0-59.64 [811 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu yakkety-updates/main amd64 linux-generic amd64 4.8.0.59.72 [1,782 B]
Get:4 http://us.archive.ubuntu.com/ubuntu yakkety-updates/main amd64 linux-headers-generic amd64 4.8.0.59.72 [2,320 B]
Get:5 http://us.archive.ubuntu.com/ubuntu yakkety-updates/main amd64 linux-image-4.8.0-59-generic amd64 4.8.0-59.64 [23.6 MB]
Get:6 http://us.archive.ubuntu.com/ubuntu yakkety-updates/main amd64 linux-image-extra-4.8.0-59-generic amd64 4.8.0-59.64 [37.4 MB]
Get:7 http://us.archive.ubuntu.com/ubuntu yakkety-updates/main amd64 linux-image-generic amd64 4.8.0.59.72 [2,348 B]
Fetched 72.0 MB in 7min 12s (167 kB/s)
Selecting previously unselected package linux-headers-4.8.0-59.
(Reading database ... 104895 files and directories currently installed.)
Preparing to unpack .../0-linux-headers-4.8.0-59_4.8.0-59.64_all.deb ...
Unpacking linux-headers-4.8.0-59 (4.8.0-59.64) ...
Selecting previously unselected package linux-headers-4.8.0-59-generic.
Preparing to unpack .../1-linux-headers-4.8.0-59-generic_4.8.0-59.64_amd64.deb ...
Unpacking linux-headers-4.8.0-59-generic (4.8.0-59.64) ...
Preparing to unpack .../2-linux-generic_4.8.0.59.72_amd64.deb ...
Unpacking linux-generic (4.8.0.59.72) over (4.8.0.22.31) ...
Preparing to unpack .../3-linux-headers-generic_4.8.0.59.72_amd64.deb ...
Unpacking linux-headers-generic (4.8.0.59.72) over (4.8.0.22.31) ...
Selecting previously unselected package linux-image-4.8.0-59-generic.
Preparing to unpack .../4-linux-image-4.8.0-59-generic_4.8.0-59.64_amd64.deb ...
Done.
Removing linux-image-4.8.0-22-generic (4.8.0-22.24) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 4.8.0-22-generic /boot/vmlinuz-4.8.0-22-generic
update-initramfs: Deleting /boot/initrd.img-4.8.0-22-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 4.8.0-22-generic /boot/vmlinuz-4.8.0-22-generic
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.12.0-041200-generic
Found initrd image: /boot/initrd.img-4.12.0-041200-generic
Found linux image: /boot/vmlinuz-4.8.0-59-generic
done
...
虽然这种方法工作得很好,但使用一个名为“byobu”的方便脚本更可靠、更高效,该脚本将上述所有命令组合到一个程序中,并提供有用的选项,例如指定要保留的内核数量在系统上。
安装byobu脚本包,它提供了一个名为purge-old-kernels的程序,用于从系统中删除旧内核和头包。
sudo apt install byobu
然后像这样删除旧内核(下面的命令允许在系统上保留 2 个内核)。
sudo purge-old-kernels --keep 2
您可能还想阅读以下有关 Linux 内核的相关文章。
- 如何在 Linux 中加载和卸载内核模块
- 如何以持久和非持久方式更改内核运行时参数
在本文中,我们描述了如何删除 Ubuntu 和 Debian 系统上旧的未使用的内核映像。您可以通过下面的反馈分享任何想法。