ubuntu修改grub引导切换到指定内核的方法

在很多集群的方案需求和设计中,都需要保持集群的服务器内核版本一致,有时由于误升级导致个别机器内核升级到新版本,所以我们需要对这些系统进行系统内核版本的切换。今天我们就来看看如何切换系统内核版本,当前前提是系统中安装了多个内核版本。
我们可以使用uanme命令查看当前内核的版本

uname -r
5.15.0-128-generic

可以看到我们的内核版本是5.15.0-128
我们再通过命令查看下内核启动的顺序

grep menuentry /boot/grub/grub.cfg
f [ 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-7ad28d39-cc1b-4da2-a9f2-bc5084d3d311' {
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-7ad28d39-cc1b-4da2-a9f2-bc5084d3d311' {
menuentry 'Ubuntu, with Linux 5.15.0-128-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-128-generic-advanced-7ad28d39-cc1b-4da2-a9f2-bc5084d3d311' {
menuentry 'Ubuntu, with Linux 5.15.0-128-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-128-generic-recovery-7ad28d39-cc1b-4da2-a9f2-bc5084d3d311' {
menuentry 'Ubuntu, with Linux 5.15.0-72-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-72-generic-advanced-7ad28d39-cc1b-4da2-a9f2-bc5084d3d311' {
menuentry 'Ubuntu, with Linux 5.15.0-72-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-72-generic-recovery-7ad28d39-cc1b-4da2-a9f2-bc5084d3d311' {

202507111032195192762788.png每一条menuentry就是一条内核记录,并且内核启动这个从上到下的顺序,如果我们要切换到5.15.0-72-generic这个版本的内核,需要做如下操作

sudo vim /etc/default/grub
GRUB_DEFAULT=0
#改成
GRUB_DEFAULT="1> 2"

202507111033178838999588.png

注意我们选择内核不要选择recovery mode这行的,另外从上到下的顺序是从0开始的,所以我们选择的是这行的内核。

menuentry 'Ubuntu, with Linux 5.15.0-72-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-72-generic-advanced-7ad28d39-cc1b-4da2-a9f2-bc5084d3d311'

然后保存退出,更新grub引导

sudo update-grub
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/50-cloudimg-settings.cfg'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.15.0-128-generic
Found initrd image: /boot/initrd.img-5.15.0-128-generic
Found linux image: /boot/vmlinuz-5.15.0-72-generic
Found initrd image: /boot/initrd.img-5.15.0-72-generic
Warning: os-prober will be executed to detect other bootable partitions.
Its output will be used to detect bootable binaries on them and create new boot entries.
done

然后重启

sudo reboot

再次打印内核版本

uname -r
5.15.0-72-generic

发现已经切换到指定内核的版本了。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/1083

评论列表

0%