centos升级内核之后修改内核启动顺序

寡人最近在修复红帽的系统bug,有些是需要升级内核解决的(具体什么bug需要升级内核解决,还是看官网的信息),原来的系统版本和内核版本是

[root@t0 ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@t0 ~]# uname -r
3.10.0-327.el7.x86_64

升级到 3.10.0-957.27.2.el7.x86_64

正常情况下,有两个启动项,一个是“正常启动”,另一个是“救援模式启动”(rescue)。

如果启动项多于2个,说明当前系统有旧内核未删除。原因是CentOS更新后不会自动删除旧内核。

默认以新内核启动,可以在启动选项中临时选择,也可以修改配置永久指定。

首先查看你现在系统里面都有什么版本的内核

[root@t0 ~]# uname -r  #查看当前内核版本
3.10.0-957.27.2.el7.x86_64
[root@t0 ~]# 
[root@t0 ~]# cat /boot/grub2/grub.cfg |grep "menuentry"  #查看所有的内核版本
menuentry 'CentOS Linux (3.10.0-327.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.el7.x86_64-advanced-e40bd680-cabe-477a-acd0-00b97281f618' {
menuentry 'CentOS Linux (3.10.0-957.27.2.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.el7.x86_64-advanced-e40bd680-cabe-477a-acd0-00b97281f618' {
menuentry 'CentOS Linux (0-rescue-840af4f292644fe6b63eefc86c7f7b09) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-840af4f292644fe6b63eefc86c7f7b09-advanced-e40bd680-cabe-477a-acd0-00b97281f618' {
[root@t0 ~]# 
[root@t0 ~]# grub2-set-default 'CentOS Linux (3.10.0-327.el7.x86_64) 7 (Core)'  #修改内核版本为 3.10.0-327.el7.x86_64
[root@t0 ~]#
[root@t0 ~]# grub2-editenv list  #查看内核修改结果 saved_entry=CentOS Linux (3.10.0-957.27.2.el7.x86_64) 7 (Core)

官方建议是至少保持两个内核,所以升级一次内核以后,系统没什么问题,新内核能用的情况下就不要骚操作了,但是如果你坚持要删除旧内核的话

[root@t0 ~]# yum remove kernel-3.10.0-327.el7.x86_64  # 删除指定的无用内核
Loaded plugins: fastestmirror, langpacks
Resolving Dependencies
--> Running transaction check
---> Package kernel.x86_64 0:3.10.0-327.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================
 Package                           Arch                              Version                                      Repository                            Size
=============================================================================================================================================================
Removing:
 kernel                            x86_64                            3.10.0-327.el7                               @anaconda                            136 M

Transaction Summary
=============================================================================================================================================================
Remove  1 Package

Installed size: 136 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : kernel-3.10.0-327.el7.x86_64                                                                                                              1/1 
  Verifying  : kernel-3.10.0-327.el7.x86_64                                                                                                              1/1 

Removed:
  kernel.x86_64 0:3.10.0-327.el7                                                                                                                             

Complete!
[root@t0 ~]# 
[root@t0 ~]# rpm -qa |grep kernel-[0-9]  # 查看全部内核包
kernel-3.10.0-957.27.2.el7.x86_64
[root@t0 ~]# 
[root@t0 ~]# cat /boot/grub2/grub.cfg |grep "menuentry "
menuentry 'CentOS Linux (3.10.0-957.27.2.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.el7.x86_64-advanced-88ae7d3d-ffaf-4402-9d44-56b6845789e4' {
menuentry 'CentOS Linux (0-rescue-8405cda22c0b421db40478edcf9c1fb2) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-8405cda22c0b421db40478edcf9c1fb2-advanced-88ae7d3d-ffaf-4402-9d44-56b6845789e4' {
[root@CentOS-7 ~]# 

ps: GRUB_DEFAULT - 设定预设选项。有以下几种情况。

GRUB_DEFAULT=0 以位置来设定预设选单选项。第一笔选单选项为 0,第二笔为 1,以此类推。
GRUB_DEFAULT=”centos*** ” 在此例中,选单中的位置不会有影响。
例如:GRUB_DEFAULT=”Ubuntu, Linux 3.11.0-13-generic”。注意要完整全称。

GRUB_DEFAULT=saved 由储存预设的作业系统开机。可以用grub-reboot和grub-set-default指令来设定预设作业系统。预设作业系统不会由来自选单中的互动式选择而被设定。
grub-set-default 0 以位置来设定预设选单选项。第一笔选单选项为 0,第二笔为 1,以此类推。
grub-set-default “xxxx” 以名称来设定
grub-reboot 0 以位置来设定下次重启开机选项。第一笔选单选项为 0,第二笔为 1,以此类推。
grub-reboot “xxxx” 以选单名称来重启开机。