2024年1月17日

Linux修改内核启动项

作者 高劲松

方案一:
1、得到当前系统已安装的所有内核版本
[root@localhost ~]# grep ^menuentry /boot/grub2/grub.cfg | cut -d “‘” -f2
CentOS Linux (3.10.0-1160.105.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1160.71.1.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-95fe5b65356541a7a6f7898b63b2b0d2) 7 (Core)

2 设置期望默认启动的内核版本
[root@localhost ~]# grub2-set-default ‘CentOS Linux (3.10.0-1160.71.1.el7.x86_64) 7 (Core)’

3 列出当前默认的启动内核版本(此步可省略)
[root@localhost ~]# grub2-editenv list
saved_entry=CentOS Linux (3.10.0-1160.71.1.el7.x86_64) 7 (Core)

4 重新生成grub信息
[root@localhost ~]# grub2-mkconfig -o /boot/grub2/grub.cfg

5 重启系统

方案二:
Linux的grub2启动菜单默认项修改
1、vi /etc/default/grub
将GRUB_DEFAULT=save修改为 GRUB_DEFAULT=2 (第一项从0开始计算,2代表默认启动项为第三个)
2、执行如下命令生效。
grub2-mkconfig -o /boot/grub2/grub.cfg
3、重启。

三、相关实验脚本

[root@openstack ~]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=”$(sed ‘s, release .*$,,g’ /etc/system-release)”
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT=”console”
GRUB_CMDLINE_LINUX=”crashkernel=auto spectre_v2=retpoline rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet”
GRUB_DISABLE_RECOVERY=”true”
[root@openstack ~]# grub2-editenv list
saved_entry=CentOS Linux (3.10.0-1160.105.1.el7.x86_64) 7 (Core)
[root@openstack ~]# grep ^menuentry /boot/grub2/grub.cfg | cut -d “‘” -f2
CentOS Linux (3.10.0-1160.105.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1160.71.1.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-95fe5b65356541a7a6f7898b63b2b0d2) 7 (Core)
[root@openstack ~]#
[root@openstack ~]# cp /boot/grub2/grub.cfg /boot/grub2/grubcfg-bak
[root@openstack ~]# grub2-set-default ‘CentOS Linux (3.10.0-1160.105.1.el7.x86_64) 7 (Core)’
[root@openstack ~]# grub2-editenv list
saved_entry=CentOS Linux (3.10.0-1160.105.1.el7.x86_64) 7 (Core)
[root@openstack ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file …
Found linux image: /boot/vmlinuz-3.10.0-1160.105.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1160.105.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-1160.71.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1160.71.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-95fe5b65356541a7a6f7898b63b2b0d2
Found initrd image: /boot/initramfs-0-rescue-95fe5b65356541a7a6f7898b63b2b0d2.img
done
[root@openstack ~]#