如何在Debian和Ubuntu中删除旧的未使用的内核

在上一篇文章中,我们已经解释了如何在CentOS / RHEL / Fedora中删除旧的未使用的内核 在本文中,我们将解释如何在Debian和Ubuntu系统中删除旧的未使用的内核,但在进一步移动之前,您可能需要安装最新版本才能充分利用:安全修复程序,新内核函数,更新的驱动程序等多得多。

要在Ubuntu和Debian中将内核升级到最新版本,请遵循以下指导:

  1. 如何将内核升级到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映像及其配置文件,然后更新grub2配置,最后重新启动系统。

$ sudo apt remove --purge linux-image-4.4.0-21-generic
$ sudo update-grub2
$ sudo reboot
在Ubuntu中删除旧内核
[sudo] password for youcl: 
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-kernel的程序,用于从系统中删除旧的内核和头文件包。

$ sudo apt install byobu

然后删除这样的旧内核(以下命令允许将2个内核保存在系统上)。

$ sudo purge-old-kernels --keep 2

您可能还想阅读以下有关Linux内核的相关文章。

  1. 如何在Linux中加载和卸载内核模块
  2. 如何以持久和不持续的方式更改内核运行时参数

在本文中,我们描述了如何在Ubuntu和Debian系统上删除旧的未使用的内核映像。 您可以通过下面的反馈分享任何想法。

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏