Ubuntu-Server-24.04-LTS版本操作系统如何关闭自动更新,并移除不必要的内核
目录
- 1. 问题背景
- 2. 关闭自动更新
- 3. 移除安装失败的内核
1. 问题背景
正在虚拟机安装slurm集群的时候,其中一个节点的安装报错,具体如下:
root@u24u04s2:~# apt install -y munge libmunge-dev libmunge2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:libcommon-sense-perl libio-pty-perl libipc-run-perl libjson-perl libjson-xs-perl libpq5 libtypes-serialiser-perl linux-image-6.8.0-35-generic linux-modules-6.8.0-35-genericlinux-modules-extra-6.8.0-35-generic linux-tools-6.8.0-35 linux-tools-6.8.0-35-generic ssl-cert
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:libmunge-dev libmunge2 munge
0 upgraded, 3 newly installed, 0 to remove and 172 not upgraded.
1 not fully installed or removed.
Need to get 153 kB of archives.
After this operation, 532 kB of additional disk space will be used.
Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/universe amd64 libmunge2 amd64 0.5.15-4build1 [14.7 kB]
Get:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/universe amd64 munge amd64 0.5.15-4build1 [102 kB]
Get:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/universe amd64 libmunge-dev amd64 0.5.15-4build1 [35.6 kB]
Fetched 153 kB in 6s (24.2 kB/s)
Selecting previously unselected package libmunge2:amd64.
(Reading database ... 149614 files and directories currently installed.)
Preparing to unpack .../libmunge2_0.5.15-4build1_amd64.deb ...
Unpacking libmunge2:amd64 (0.5.15-4build1) ...
Selecting previously unselected package munge.
Preparing to unpack .../munge_0.5.15-4build1_amd64.deb ...
Unpacking munge (0.5.15-4build1) ...
Selecting previously unselected package libmunge-dev.
Preparing to unpack .../libmunge-dev_0.5.15-4build1_amd64.deb ...
Unpacking libmunge-dev (0.5.15-4build1) ...
Setting up libmunge2:amd64 (0.5.15-4build1) ...
Setting up linux-image-6.8.0-71-generic (6.8.0-71.71) ...
Setting up munge (0.5.15-4build1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/munge.service → /usr/lib/systemd/system/munge.service.
Setting up libmunge-dev (0.5.15-4build1) ...
Processing triggers for libc-bin (2.39-0ubuntu8.5) ...
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for linux-image-6.8.0-71-generic (6.8.0-71.71) ...
/etc/kernel/postinst.d/initramfs-tools:
update-initramfs: Generating /boot/initrd.img-6.8.0-71-generic
cpio: write error: No space left on device
E: mkinitramfs failure uncompressed cpio 2
update-initramfs: failed for /boot/initrd.img-6.8.0-71-generic with 1.
run-parts: /etc/kernel/postinst.d/initramfs-tools exited with return code 1
dpkg: error processing package linux-image-6.8.0-71-generic (--configure):installed linux-image-6.8.0-71-generic package post-installation script subprocess returned error exit status 1
Errors were encountered while processing:linux-image-6.8.0-71-generic
needrestart is being skipped since dpkg has failed
E: Sub-process /usr/bin/dpkg returned an error code (1)
上述报错提示更新了新版本的内核,但是cpio
命令提示写入错误,原因是磁盘空间不足了,所以无法更新内存文件系统镜像文件。提示需要重启。
检查文件系统使用率,并执行重启操作系统的操作:
root@u24u04s2:~# df -hTP
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 392M 1.2M 391M 1% /run
/dev/mapper/vg0-lv--root xfs 9.7G 5.0G 4.7G 52% /
tmpfs tmpfs 2.0G 1.1M 2.0G 1% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/vda2 ext4 265M 212M 33M 87% /boot
tmpfs tmpfs 392M 16K 392M 1% /run/user/1000
tmpfs tmpfs 392M 16K 392M 1% /run/user/0
root@u24u04s2:~# cd /boot/
root@u24u04s2:/boot# ls
config-6.8.0-35-generic grub initrd.img-6.8.0-64-generic System.map-6.8.0-35-generic vmlinuz vmlinuz-6.8.0-71-generic
config-6.8.0-64-generic initrd.img initrd.img.old System.map-6.8.0-64-generic vmlinuz-6.8.0-35-generic vmlinuz.old
config-6.8.0-71-generic initrd.img-6.8.0-35-generic lost+found System.map-6.8.0-71-generic vmlinuz-6.8.0-64-generic
root@u24u04s2:/boot# systemctl reboot
root@u24u04s2:/boot# Connection to u24u04s2 closed by remote host.
Connection to u24u04s2 closed.
重启完成之后,检查内核状态,仍然是旧版的内核,但是新版的不完全内核却占用了一定的/boot分区的存储空间。
albertqee@ZBG7W:PostgreSQL$ ssh -l root u24u04s2
Welcome to Ubuntu 24.04 LTS (GNU/Linux 6.8.0-64-generic x86_64)* Documentation: https://help.ubuntu.com* Management: https://landscape.canonical.com* Support: https://ubuntu.com/proSystem information as of Fri Aug 1 03:35:16 PM UTC 2025System load: 0.08 Processes: 152Usage of /: 51.3% of 9.64GB Users logged in: 0Memory usage: 6% IPv4 address for enp1s0: 192.168.122.126Swap usage: 0%Expanded Security Maintenance for Applications is not enabled.176 updates can be applied immediately.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradableEnable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro statusLast login: Fri Aug 1 14:21:07 2025 from 192.168.122.1
root@u24u04s2:~# uname -r
6.8.0-64-generic
root@u24u04s2:~#
由于这种更新自动执行,建议生产环境在部署完成操作系统,并且调优了内核参数之后,尽量保持内核版本和参数的稳定。
2. 关闭自动更新
为此,就需要关闭Ubuntu-Server-LTS版本系统的自动更新。要关闭自动更新也很简单,因为更新过程是通过apt
命令来完成的,所以在apt
相关的配置文件中,有自动更新的记录,具体如下:
root@u24u04s2:~# ls /etc/apt/apt.conf.d/
01autoremove 10periodic 20apt-esm-hook.conf 20auto-upgrades 20snapd.conf 50apt-file.conf 50unattended-upgrades 99needrestart
01-vendor-ubuntu 15update-stamp 20archive 20packagekit 50appstream 50command-not-found 70debconf 99update-notifier
root@u24u04s2:~# cat /etc/apt/apt.conf.d/10periodic
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "0";
APT::Periodic::AutocleanInterval "0";
root@u24u04s2:~#
上述的/etc/apt/apt.conf.d/10periodic文件中,就记录自动更新的相关配置,配置内容中的字段1表示开启自动更新;字段0表示关闭自动更新。
所以只需要将上述配置文件中的字段1改为0即可关闭自动更新。
具体如下:
root@u24u04s2:~# vim /etc/apt/apt.conf.d/10periodic
root@u24u04s2:~# cat /etc/apt/apt.conf.d/10periodic
APT::Periodic::Update-Package-Lists "0";
APT::Periodic::Download-Upgradeable-Packages "0";
APT::Periodic::AutocleanInterval "0";
root@u24u04s2:~#
上述就可以关闭Ubuntu操作系统的自动更新了。
3. 移除安装失败的内核
虽然重启完成,但是由于存在安装不完全的内核,所以此时安装软件包的时候,仍然会报错,为了解决这个问题,就需要卸载不需要的内核。
可以使用apt purge linux-image-x.x.x-x-generic
的形式删除指定的内核版本;也可以使用apt autoremove --purge
命令自动删除老旧的内核,然后自动完成新内核的更新操作。
此处使用第二种方式:
root@u24u04s2:~# dpkg -l | egrep linux-image
ii linux-image-6.8.0-35-generic 6.8.0-35.35+1 amd64 Signed kernel image generic
ii linux-image-6.8.0-64-generic 6.8.0-64.67 amd64 Signed kernel image generic
iF linux-image-6.8.0-71-generic 6.8.0-71.71 amd64 Signed kernel image generic
ii linux-image-generic 6.8.0-71.71 amd64 Generic Linux kernel image
root@u24u04s2:~# apt autoremove --purge
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:libcommon-sense-perl* libio-pty-perl* libipc-run-perl* libjson-perl* libjson-xs-perl* libpq5* libtypes-serialiser-perl* linux-image-6.8.0-35-generic* linux-modules-6.8.0-35-generic*linux-modules-extra-6.8.0-35-generic* linux-tools-6.8.0-35* linux-tools-6.8.0-35-generic* ssl-cert*
0 upgraded, 0 newly installed, 13 to remove and 172 not upgraded.
1 not fully installed or removed.
After this operation, 186 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 149676 files and directories currently installed.)
Removing libjson-xs-perl (4.030-2build3) ...
Removing libtypes-serialiser-perl (1.01-1) ...
Removing libcommon-sense-perl:amd64 (3.75-3build3) ...
Removing libipc-run-perl (20231003.0-1) ...
Removing libio-pty-perl (1:1.20-1build2) ...
Removing libjson-perl (4.10000-1) ...
Removing libpq5:amd64 (17.5-1.pgdg24.04+1) ...
Removing linux-image-6.8.0-35-generic (6.8.0-35.35+1) ...
/etc/kernel/postrm.d/initramfs-tools:
update-initramfs: Deleting /boot/initrd.img-6.8.0-35-generic
/etc/kernel/postrm.d/zz-update-grub:
Sourcing file `/etc/default/grub'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-6.8.0-71-generic
Found linux image: /boot/vmlinuz-6.8.0-64-generic
Found initrd image: /boot/initrd.img-6.8.0-64-generic
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
Adding boot menu entry for UEFI Firmware Settings ...
done
Removing linux-modules-extra-6.8.0-35-generic (6.8.0-35.35) ...
Removing linux-modules-6.8.0-35-generic (6.8.0-35.35) ...
Removing linux-tools-6.8.0-35-generic (6.8.0-35.35) ...
Removing linux-tools-6.8.0-35 (6.8.0-35.35) ...
Removing ssl-cert (1.1.2ubuntu1) ...
Setting up linux-image-6.8.0-71-generic (6.8.0-71.71) ...
I: /boot/initrd.img is now a symlink to initrd.img-6.8.0-71-generic
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for libc-bin (2.39-0ubuntu8.5) ...
(Reading database ... 141804 files and directories currently installed.)
Purging configuration files for linux-modules-6.8.0-35-generic (6.8.0-35.35) ...
dpkg: warning: while removing linux-modules-6.8.0-35-generic, directory '/lib/modules/6.8.0-35-generic' not empty so not removed
Purging configuration files for ssl-cert (1.1.2ubuntu1) ...
Purging configuration files for linux-image-6.8.0-35-generic (6.8.0-35.35+1) ...
Purging configuration files for linux-modules-extra-6.8.0-35-generic (6.8.0-35.35) ...
Processing triggers for linux-image-6.8.0-71-generic (6.8.0-71.71) ...
/etc/kernel/postinst.d/initramfs-tools:
update-initramfs: Generating /boot/initrd.img-6.8.0-71-generic
/etc/kernel/postinst.d/zz-update-grub:
Sourcing file `/etc/default/grub'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-6.8.0-71-generic
Found initrd image: /boot/initrd.img-6.8.0-71-generic
Found linux image: /boot/vmlinuz-6.8.0-64-generic
Found initrd image: /boot/initrd.img-6.8.0-64-generic
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
Adding boot menu entry for UEFI Firmware Settings ...
done
root@u24u04s2:~#
上述执行完成之后,重新检查/boot/分区:
root@u24u04s2:~# ls /boot
config-6.8.0-64-generic grub initrd.img-6.8.0-71-generic lost+found System.map-6.8.0-71-generic vmlinuz-6.8.0-64-generic vmlinuz.old
config-6.8.0-71-generic initrd.img-6.8.0-64-generic initrd.img.old System.map-6.8.0-64-generic vmlinuz vmlinuz-6.8.0-71-generic
root@u24u04s2:~#
删除了老旧的内核版本,并且执行了新内核的自动更新操作。重启操作系统之后,检查更新情况:
root@u24u04s2:~# systemctl reboot
root@u24u04s2:~# Connection to u24u04s2 closed by remote host.
Connection to u24u04s2 closed.
albertqee@ZBG7W:PostgreSQL$ ssh -l root u24u04s2
Welcome to Ubuntu 24.04 LTS (GNU/Linux 6.8.0-71-generic x86_64)* Documentation: https://help.ubuntu.com* Management: https://landscape.canonical.com* Support: https://ubuntu.com/proSystem information as of Fri Aug 1 04:00:04 PM UTC 2025System load: 0.0 Processes: 180Usage of /: 49.6% of 9.64GB Users logged in: 1Memory usage: 7% IPv4 address for enp1s0: 192.168.122.126Swap usage: 0%Expanded Security Maintenance for Applications is not enabled.176 updates can be applied immediately.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradableEnable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro statusLast login: Fri Aug 1 15:35:35 2025 from 192.168.122.1
root@u24u04s2:~# uname -r
6.8.0-71-generic
root@u24u04s2:~#
内核已经更新完成了。
如果是手动删除指定的内核,则需要在删除万内核文件之后,再执行命令update-grub
命令更新GRUB引导程序和配置文件。