CentOS 7 内核升级

less than 1 minute read

Step 1:查看当前内核版本

$ uname -sr
Linux 3.10.0-957.el7.x86_64

Step 2:启用 ELRepo 仓库

# 导入公钥
$ rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# 安装 ELRepo
$ yum install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm -y

Step 3:查询可用的内核包

$ yum --disablerepo="*" --enablerepo="elrepo-kernel" list available

Step 4:安装最新稳定版本

$ yum --enablerepo=elrepo-kernel install kernel-ml -y

Step 5:查看系统所有可用内核

$ awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (5.18.11-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-1160.el7.x86_64) 7 (Core)
2 : CentOS Linux (0-rescue-b29ae5e76e8444afb5a14882efc187c6) 7 (Core)

Step 6:设置 GRUB 默认的内核版本

  • 方式1:通过 grub2-set-default 命令设置

    $ grub2-set-default 0
    

    0 表示上面查询的可用内核的编号

  • 方式2:通过编辑 /etc/default/grub 文件设置

    设置 GRUB_DEFAULT=0 ,0 表示上面查询的可用内核的编号

    $ sed -i 's,GRUB_DEFAULT.*,GRUB_DEFAULT=0,' /etc/default/grub
    

Step 7:重新创建 grub 配置文件

$ grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.18.11-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-5.18.11-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-1160.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1160.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-b29ae5e76e8444afb5a14882efc187c6
Found initrd image: /boot/initramfs-0-rescue-b29ae5e76e8444afb5a14882efc187c6.img
done

Step 8:重启系统并确定内核版本

$ uname -sr
Linux 5.18.11-1.el7.elrepo.x86_64