250723-Linux-内核更新-禁用内核更新-配置指定内核启动

锁定内核

1
2
sudo apt-mark hold linux-generic linux-image-generic linux-headers-generic
sudo apt-mark hold linux-image-generic linux-headers-generic

内核脚本库

1.显示系统全部内核

1
sudo dpkg --get-selections | grep linux

2.完全删除系统内核

1
2
sudo apt-get remove --purge linux-modules-5.4.0-42-generic
sudo apt-get remove --purge linux-modules-5.15.0-71-generic -y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sudo apt-get remove --purge  -y linux-headers-5.15.0-139-generic
sudo apt-get remove --purge -y linux-hwe-5.15-headers-5.15.0-139
sudo apt-get remove --purge -y linux-image-5.15.0-139-generic
sudo apt-get remove --purge -y linux-modules-5.15.0-139-generic
sudo apt-get remove --purge -y linux-modules-extra-5.15.0-139-generic

sudo dpkg --get-selections | grep linux
sudo apt-get remove --purge -y linux-modules-extra-5.15.0-139-generic linux-modules-5.15.0-139-generic linux-image-5.15.0-139-generic linux-headers-5.15.0-139-generic linux-hwe-5.15-headers-5.15.0-139
sudo update-grub

sudo apt-get remove --purge -y linux-modules-5.15.0-60-generic -y
sudo apt-get remove --purge -y linux-modules-5.15.0-46-generic -y

sudo apt-get remove --purge -y linux-image-5.15.0-60-generic -y
sudo apt-get remove --purge -y linux-image-5.15.0-46-generic -y

sudo apt-get remove --purge -y linux-image-unsigned-5.15.0-46-generic -y
sudo apt-get remove --purge -y linux-modules-5.15.0-46-generic -y

3.再次用以下命令查看是否删除干净

1
dpkg --get-selections | grep linux

Ubuntu设置开机默认内核及删除多余内核
Ubuntu一更新就会启动新的内核,进入系统后总会发现驱动不好用了。所以可以对内核进行下设置,使得主机进入我们想要的内核版本,也可以删除掉多余的内核。

Ubuntu设置开机默认内核
#不删除内核情况下,更改要进入的内核

1
sudo gedit /etc/default/grub

(1)找到GRUB_DEFAULT = “0”
grub菜单如下:

1
2
3
4
5
6
7
8
- Ubuntu
- Advanced options for Ubuntu
* Ubuntu, with Linux 5.13.0-30-generic
* Ubuntu, with Linux 5.13.0-30-generic (recovery mode)
* Ubuntu, with Linux 5.11.0-27-generic
* Ubuntu, with Linux 5.11.0-27-generic (recovery mode)
- Windows 10
- System setup

(注意:每层菜单的索引都是从0开始)
GRUB菜单中的 Ubuntu为0,
Advanced options for Ubuntu为1,Windows 10为2,System setup为3。
Advanced options for Ubuntu菜单中的Ubuntu, with Linux 5.13.0-30-generic选项为0,
Ubuntu, with Linux 5.13.0-30-generic (recovery mode)为1,依次类推……

(2)想要进入5.11.0-27内核,则更改GRUB_DEFAULT = “1> 2”
‘>’ 符号来指定有一个子菜单(注意符号 > 和数字 2之间有空格)。

(3)更新GRUB配置文件

1
sudo update-grub

Ubuntu删除多余内核
#查看已安装的内核

1
dpkg --get-selections | grep linux-image

#找出系统已经安装的内核版本

1
dpkg --get-selections|grep linux

#卸载不需要的内核

1
2
sudo apt-get remove 内核名称
sudo apt autoremove

#修改 /boot/grub/grub.cfg文件,删除启动项

1
sudo gedit /boot/grub/grub.cfg

Ubuntu关闭自动更新
#关闭自动更新

1
sudo apt-mark hold linux-image-5.11.0-27-generic

验证内核

1
uname -r

选择内核启动

  1. 如果您的系统中有多个内核版本,您可以在启动时选择要启动的内核版本。查看当前系统中内核的启动顺序:

    bash

    1
    grep 'menuentry' /boot/grub/grub.cfg

    找到您想要设置为默认启动的内核条目,然后修改 /etc/default/grub 文件中的 GRUB_DEFAULT
    配置项,将其设置为对应的内核启动项。例如,如果您想要设置 Ubuntu, with Linux 5.8.0-43-generic 为默认启动项,您可以这样设置:

    bash

    1
    sudo sed -i 's/GRUB_DEFAULT=0/GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 5.8.0-43-generic"/g' /etc/default/grub

    然后再次更新 GRUB 并重启:

    bash

    1
    2
    sudo update-grub
    sudo reboot

请按照这些步骤操作,您应该能够成功将 Ubuntu 20.04 的内核版本调整到 5.8.0-43-generic。如果在操作过程中遇到任何问题,可以参考相关博客文章获取更详细的指导。