前言

系统为Ubuntu 22.04.2 LTS

内核升级参考Azure-如何升级Ubuntu内核

安装apt源中的LTS内核

使用apt search linux-imageapt search linux-headers查找apt源中可用的image和headers版本号

root@VM-16-8-ubuntu:~# apt search linux-headers | grep amd64

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
...
linux-headers-5.15.0-73-generic/jammy-updates,jammy-security 5.15.0-73.80 amd64
linux-headers-5.15.0-73-lowlatency/jammy-updates,jammy-security 5.15.0-73.80 amd64
linux-headers-5.15.0-75-generic/jammy-updates,jammy-security 5.15.0-75.82 amd64
linux-headers-5.15.0-75-lowlatency/jammy-updates,jammy-security 5.15.0-75.82 amd64
linux-headers-5.15.0-76-generic/jammy-updates,jammy-security,now 5.15.0-76.83 amd64 [installed]
linux-headers-5.15.0-76-lowlatency/jammy-updates,jammy-security 5.15.0-76.83 amd64
...
root@VM-16-8-ubuntu:~# apt search linux-image | grep amd64

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
...
linux-image-unsigned-5.15.0-72-generic/jammy-updates,jammy-security 5.15.0-72.79 amd64
linux-image-unsigned-5.15.0-72-lowlatency/jammy-updates,jammy-security 5.15.0-72.79 amd64
linux-image-unsigned-5.15.0-73-generic/jammy-updates,jammy-security 5.15.0-73.80 amd64
linux-image-unsigned-5.15.0-73-lowlatency/jammy-updates,jammy-security 5.15.0-73.80 amd64
linux-image-unsigned-5.15.0-75-generic/jammy-updates,jammy-security 5.15.0-75.82 amd64
linux-image-unsigned-5.15.0-75-lowlatency/jammy-updates,jammy-security 5.15.0-75.82 amd64
linux-image-unsigned-5.15.0-76-generic/jammy-updates,jammy-security,now 5.15.0-76.83 amd64 [installed]
linux-image-unsigned-5.15.0-76-lowlatency/jammy-updates,jammy-security 5.15.0-76.83 amd64
...

使用apt-get install linux-image-unsigned-5.15.0-76-generic linux-headers-5.15.0-76-generic安装,可以安装其它的版本,但需要注意image和headers的版本号需要保持一致

此时使用dpkg --get-selections | grep linux命令查看已经安装的内核,可以看到已经安装了5.15和6.4两个内核

root@VM-16-8-ubuntu:~# dpkg --get-selections | grep linux
binutils-x86-64-linux-gnu                       install
console-setup-linux                             install
libselinux1:amd64                               install
libselinux1-dev:amd64                           install
linux-base                                      install
linux-firmware                                  install
linux-headers-5.15.0-76                         install
linux-headers-5.15.0-76-generic                 install
linux-headers-6.4.0-060400                      install
linux-headers-6.4.0-060400-generic              install
linux-image-unsigned-5.15.0-76-generic          install
linux-image-unsigned-6.4.0-060400-generic       install
linux-libc-dev:amd64                            install
linux-modules-5.15.0-76-generic                 install
linux-modules-6.4.0-060400-generic              install
util-linux                                      install

修改内核启动顺序

安装更高版本的内核时,不需要手动修改内核启动顺序;已有高版本内核,更换为低版本内核则需要手动指定启动的内核

查看启动项

使用grep 'menuentry' /boot/grub/grub.cfg

root@VM-16-8-ubuntu:~# grep 'menuentry' /boot/grub/grub.cfg
if [ x"${feature_menuentry_id}" = xy ]; then
  menuentry_id_option="--id"
  menuentry_id_option=""
export menuentry_id_option
menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-7bccaefa-b039-4ff6-bd32-22dde0066c0b' {
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-7bccaefa-b039-4ff6-bd32-22dde0066c0b' {
        menuentry 'Ubuntu, with Linux 6.4.0-060400-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-6.4.0-060400-generic-advanced-7bccaefa-b039-4ff6-bd32-22dde0066c0b' {
        menuentry 'Ubuntu, with Linux 6.4.0-060400-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-6.4.0-060400-generic-recovery-7bccaefa-b039-4ff6-bd32-22dde0066c0b' {
        menuentry 'Ubuntu, with Linux 5.15.0-76-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-76-generic-advanced-7bccaefa-b039-4ff6-bd32-22dde0066c0b' {
        menuentry 'Ubuntu, with Linux 5.15.0-76-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-76-generic-recovery-7bccaefa-b039-4ff6-bd32-22dde0066c0b' {

修改启动项

使用vim /etc/default/grub编辑启动项,将GRUB_DEFAULT所在的行修改为GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 5.15.0-76-generic"

其中Advanced options for Ubuntu是submenu名,Ubuntu, with Linux 5.15.0-76-generic是menuentry名
然后执行sudo update-grub更新grub

重启服务器

重启服务器后查看内核,发现已经内核已变为5.15LTS

root@VM-16-8-ubuntu:~# uname -a
Linux VM-16-8-ubuntu 5.15.0-76-generic #83-Ubuntu SMP Thu Jun 15 19:16:32 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

清理不需要的内核

清理内核

使用apt purge命令将不需要的内核以及相关的配置文件删除

apt purge linux-headers-6.4.0-060400 \
linux-headers-6.4.0-060400-generic \
linux-image-unsigned-6.4.0-060400-generic \
linux-modules-6.4.0-060400-generic
root@VM-16-8-ubuntu:~# apt purge linux-headers-6.4.0-060400 linux-headers-6.4.0-060400-generic linux-image-unsigned-6.4.0-060400-generic linux-modules-6.4.0-060400-generic
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  linux-headers-6.4.0-060400* linux-headers-6.4.0-060400-generic* linux-image-unsigned-6.4.0-060400-generic* linux-modules-6.4.0-060400-generic*
0 upgraded, 0 newly installed, 4 to remove and 0 not upgraded.
After this operation, 693 MB disk space will be freed.
Do you want to continue? [Y/n] Y
...

再次查看,不需要的6.4内核已被删除

root@VM-16-8-ubuntu:~# dpkg --get-selections | grep linux
binutils-x86-64-linux-gnu                       install
console-setup-linux                             install
libselinux1:amd64                               install
libselinux1-dev:amd64                           install
linux-base                                      install
linux-firmware                                  install
linux-headers-5.15.0-76                         install
linux-headers-5.15.0-76-generic                 install
linux-image-unsigned-5.15.0-76-generic          install
linux-libc-dev:amd64                            install
linux-modules-5.15.0-76-generic                 install
util-linux                                      install

复原grub

再次使用vim /etc/default/grub编辑启动项,将GRUB_DEFAULT所在的行修改为GRUB_DEFAULT=0,然后执行sudo update-grub更新grub

这样之后通过apt更新lts内核时,启动内核也会相应地更新

最后更新于 2023-08-04