Linux 升级内核

适用以下版本

  • Centos 7 内核升级
  • Centos 8 或 Rocky Linux 8 内核升级

环境息息

  • Centos7 3.10.0-1062.9.1.el7.x86_64
  • Rocky Linux 8

升级步骤

安装 centos-kernel

如果是 Centos 7 ,参考以下命令安装 centos-kernel

elrepo-kernel 中已经没有 Centos 7 相关的内核安装包

yum install centos-release

安装 elrepo 源

如果是 Centos 8 或 Rocky Linux 8,参考以下命令安装 elrepo

# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# rpm --import https://www.elrepo.org/RPM-GPG-KEY-v2-elrepo.org

# yum install https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm

# yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
Last metadata expiration check: 0:00:14 ago on Fri 28 Feb 2025 11:42:09 AM HKT.
Available Packages
bpftool.x86_64 5.4.290-1.el8.elrepo elrepo-kernel
kernel-lt.x86_64 5.4.290-1.el8.elrepo elrepo-kernel
kernel-lt-core.x86_64 5.4.290-1.el8.elrepo elrepo-kernel
kernel-lt-devel.x86_64 5.4.290-1.el8.elrepo elrepo-kernel
kernel-lt-doc.noarch 5.4.290-1.el8.elrepo elrepo-kernel
kernel-lt-headers.x86_64 5.4.290-1.el8.elrepo elrepo-kernel
kernel-lt-modules.x86_64 5.4.290-1.el8.elrepo elrepo-kernel
kernel-lt-modules-extra.x86_64 5.4.290-1.el8.elrepo elrepo-kernel
kernel-lt-tools.x86_64 5.4.290-1.el8.elrepo elrepo-kernel
kernel-lt-tools-libs.x86_64 5.4.290-1.el8.elrepo elrepo-kernel
kernel-lt-tools-libs-devel.x86_64 5.4.290-1.el8.elrepo elrepo-kernel
kernel-ml.x86_64 6.13.5-1.el8.elrepo elrepo-kernel
kernel-ml-core.x86_64 6.13.5-1.el8.elrepo elrepo-kernel
kernel-ml-devel.x86_64 6.13.5-1.el8.elrepo elrepo-kernel
kernel-ml-doc.noarch 6.13.5-1.el8.elrepo elrepo-kernel
kernel-ml-headers.x86_64 6.13.5-1.el8.elrepo elrepo-kernel
kernel-ml-modules.x86_64 6.13.5-1.el8.elrepo elrepo-kernel
kernel-ml-modules-extra.x86_64 6.13.5-1.el8.elrepo elrepo-kernel
kernel-ml-tools.x86_64 6.13.5-1.el8.elrepo elrepo-kernel
kernel-ml-tools-libs.x86_64 6.13.5-1.el8.elrepo elrepo-kernel
kernel-ml-tools-libs-devel.x86_64 6.13.5-1.el8.elrepo elrepo-kernel
perf.x86_64 6.13.5-1.el8.elrepo elrepo-kernel
python3-perf.x86_64 6.13.5-1.el8.elrepo elrepo-kernel

安装新版本内核

Centos 7 查看可用的内核版本

5.15.80 的内核在 centos-kernel-experimental 仓库中可用

# yum --enablerepo=centos-kernel list available | grep -i kern
* elrepo-kernel: elrepo.in.ssimn.org
abrt-addon-kerneloops.x86_64 2.1.11-60.el7.centos base
bpftool.x86_64 5.4.225-200.el7 centos-kernel
kernel.x86_64 5.4.225-200.el7 centos-kernel
kernel-abi-whitelists.noarch 3.10.0-1160.119.1.el7 updates
kernel-core.x86_64 5.4.225-200.el7 centos-kernel
kernel-cross-headers.x86_64 5.4.225-200.el7 centos-kernel
kernel-debug.x86_64 5.4.115-200.el7 centos-kernel
kernel-debug-core.x86_64 5.4.115-200.el7 centos-kernel
kernel-debug-devel.x86_64 5.4.115-200.el7 centos-kernel
kernel-debug-modules.x86_64 5.4.115-200.el7 centos-kernel
kernel-debug-modules-extra.x86_64 5.4.115-200.el7 centos-kernel
kernel-debug-modules-internal.x86_64 5.4.115-200.el7 centos-kernel
kernel-devel.x86_64 5.4.225-200.el7 centos-kernel
kernel-doc.noarch 3.10.0-1160.119.1.el7 updates
kernel-headers.x86_64 5.4.225-200.el7 centos-kernel
kernel-modules.x86_64 5.4.225-200.el7 centos-kernel
kernel-modules-extra.x86_64 5.4.225-200.el7 centos-kernel
kernel-modules-internal.x86_64 5.4.225-200.el7 centos-kernel
kernel-tools.x86_64 5.4.225-200.el7 centos-kernel
kernel-tools-libs.x86_64 5.4.225-200.el7 centos-kernel
kernel-tools-libs-devel.x86_64 5.4.225-200.el7 centos-kernel
libreport-plugin-kerneloops.x86_64 2.1.11-53.el7.centos base
perf.x86_64 5.4.225-200.el7 centos-kernel
perl-generators.noarch 1.08-6.el7 centos-kernel
perl-interpreter.noarch 4:5.16.3-6.el7 centos-kernel
python3-perf.x86_64 5.4.225-200.el7 centos-kernel
texlive-l3kernel.noarch 2:svn29409.SVN_4469-45.el7 base
texlive-l3kernel-doc.noarch 2:svn29409.SVN_4469-45.el7 base
xorg-x11-drv-vmmouse.noarch 14.0.1-0 centos-kernel


# yum --disablerepo="*" --enablerepo="centos-kernel-experimental" list | grep kern
kernel.x86_64 5.4.225-200.el7 @centos-kernel
kernel-core.x86_64 5.4.225-200.el7 @centos-kernel
kernel-devel.x86_64 5.4.225-200.el7 @centos-kernel
kernel-modules.x86_64 5.4.225-200.el7 @centos-kernel
bpftool.x86_64 5.15.80-200.el7 centos-kernel-experimental
kernel.x86_64 5.15.80-200.el7 centos-kernel-experimental
kernel-core.x86_64 5.15.80-200.el7 centos-kernel-experimental
kernel-cross-headers.x86_64 5.15.80-200.el7 centos-kernel-experimental
kernel-debug.x86_64 5.10.33-200.el7 centos-kernel-experimental
kernel-debug-core.x86_64 5.10.33-200.el7 centos-kernel-experimental
kernel-debug-devel.x86_64 5.10.33-200.el7 centos-kernel-experimental
kernel-debug-modules.x86_64 5.10.33-200.el7 centos-kernel-experimental
kernel-debug-modules-extra.x86_64 5.10.33-200.el7 centos-kernel-experimental
kernel-debug-modules-internal.x86_64 5.10.33-200.el7 centos-kernel-experimental
kernel-devel.x86_64 5.15.80-200.el7 centos-kernel-experimental
kernel-headers.x86_64 5.15.80-200.el7 centos-kernel-experimental
kernel-modules.x86_64 5.15.80-200.el7 centos-kernel-experimental
kernel-modules-extra.x86_64 5.15.80-200.el7 centos-kernel-experimental
kernel-modules-internal.x86_64 5.15.80-200.el7 centos-kernel-experimental
kernel-tools.x86_64 5.15.80-200.el7 centos-kernel-experimental
kernel-tools-libs.x86_64 5.15.80-200.el7 centos-kernel-experimental
kernel-tools-libs-devel.x86_64 5.15.80-200.el7 centos-kernel-experimental
perf.x86_64 5.15.80-200.el7 centos-kernel-experimental
perl-generators.noarch 1.08-6.el7 centos-kernel-experimental
perl-interpreter.noarch 4:5.16.3-6.el7 centos-kernel-experimental
python3-perf.x86_64 5.15.80-200.el7 centos-kernel-experimental
xorg-x11-drv-vmmouse.noarch 14.0.1-0 centos-kernel-experimental

Centos 8 或者 Rocky Linux 8 查看可用的内核版本

$ yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
Available Packages
kernel-lt-doc.noarch 5.4.212-1.el7.elrepo elrepo-kernel
kernel-lt-headers.x86_64 5.4.212-1.el7.elrepo elrepo-kernel
kernel-lt-tools.x86_64 5.4.212-1.el7.elrepo elrepo-kernel
kernel-lt-tools-libs.x86_64 5.4.212-1.el7.elrepo elrepo-kernel
kernel-lt-tools-libs-devel.x86_64 5.4.212-1.el7.elrepo elrepo-kernel
kernel-ml.x86_64 5.19.8-1.el7.elrepo elrepo-kernel
kernel-ml-devel.x86_64 5.19.8-1.el7.elrepo elrepo-kernel
kernel-ml-doc.noarch 5.19.8-1.el7.elrepo elrepo-kernel
kernel-ml-headers.x86_64 5.19.8-1.el7.elrepo elrepo-kernel
kernel-ml-tools.x86_64 5.19.8-1.el7.elrepo elrepo-kernel
kernel-ml-tools-libs.x86_64 5.19.8-1.el7.elrepo elrepo-kernel
kernel-ml-tools-libs-devel.x86_64 5.19.8-1.el7.elrepo elrepo-kernel
perf.x86_64 5.19.8-1.el7.elrepo elrepo-kernel
python-perf.x86_64 5.19.8-1.el7.elrepo elrepo-kernel

kernel-lt 和 kernel-ml 二者的区别:

  • kernel-ml 软件包是根据 Linux Kernel Archives 的主线稳定分支提供的源构建的。 内核配置基于默认的 RHEL-7 配置,并根据需要启用了添加的功能。 这些软件包有意命名为 kernel-ml,以免与 RHEL-7 内核发生冲突,因此,它们可以与常规内核一起安装和更新。
  • kernel-lt 包是从 Linux Kernel Archives 提供的源代码构建的,就像 kernel-ml 软件包一样。 不同之处在于 kernel-lt 基于长期支持分支,而 kernel-ml 基于主线稳定分支。

在 ELRepo 中有两个内核选项,一个是 kernel-lt(长期支持版),一个是 kernel-ml(主线最新版本),采用长期支持版本(kernel-lt),更加稳定一些。

安装最新的长期支持版

yum --disablerepo="*" --enablerepo="elrepo-kernel" install kernel-lt kernel-lt-devel

查看系统上可用的内核,最好使用 grubby --info=ALL 命令查看,grubby管理 Grub2 默认内核的工具

$ awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
CentOS Linux (5.4.212-1.el7.elrepo.x86_64) 7 (Core)
CentOS Linux (3.10.0-1062.9.1.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-ed7b5ef29925468cbc59b47c142c26b8) 7 (Core)

# grubby --info=ALL
index=0
kernel="/boot/vmlinuz-6.13.5-1.el8.elrepo.x86_64"
args="ro crashkernel=auto resume=/dev/mapper/rl-swap rd.lvm.lv=rl/root rd.lvm.lv=rl/swap console=ttyS0,115200n8 net.ifnames=0 $tuned_params"
root="/dev/mapper/rl-root"
initrd="/boot/initramfs-6.13.5-1.el8.elrepo.x86_64.img $tuned_initrd"
title="Enterprise Linux (6.13.5-1.el8.elrepo.x86_64) 8.10"
id="8c17e8ecf9c14520b525abb61407347b-6.13.5-1.el8.elrepo.x86_64"
index=1
kernel="/boot/vmlinuz-4.18.0-553.el8_10.x86_64"
args="ro crashkernel=auto resume=/dev/mapper/rl-swap rd.lvm.lv=rl/root rd.lvm.lv=rl/swap console=ttyS0,115200n8 net.ifnames=0 $tuned_params"
root="/dev/mapper/rl-root"
initrd="/boot/initramfs-4.18.0-553.el8_10.x86_64.img $tuned_initrd"
title="Rocky Linux (4.18.0-553.el8_10.x86_64) 8.10 (Green Obsidian)"
id="8c17e8ecf9c14520b525abb61407347b-4.18.0-553.el8_10.x86_64"
index=2
kernel="/boot/vmlinuz-0-rescue-8c17e8ecf9c14520b525abb61407347b"
args="ro crashkernel=auto resume=/dev/mapper/rl-swap rd.lvm.lv=rl/root rd.lvm.lv=rl/swap console=ttyS0,115200n8 net.ifnames=0"
root="/dev/mapper/rl-root"
initrd="/boot/initramfs-0-rescue-8c17e8ecf9c14520b525abb61407347b.img"
title="Rocky Linux (0-rescue-8c17e8ecf9c14520b525abb61407347b) 8.10 (Green Obsidian)"
id="8c17e8ecf9c14520b525abb61407347b-0-rescue"

设置开机从新内核启动

grub2-set-default 0

生成grub 配置文件

$ grub2-mkconfig -o /boot/grub2/grub.cfg

Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.4.212-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-5.4.212-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-1062.9.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1062.9.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-ed7b5ef29925468cbc59b47c142c26b8
Found initrd image: /boot/initramfs-0-rescue-ed7b5ef29925468cbc59b47c142c26b8.img
done

重启系统并验证

reboot
# 或者
shutdown -r now

重启后查看内核版本

$ uname -r
5.4.212-1.el7.elrepo.x86_64

查看系统中已安装的内核

$ rpm -qa | grep kernel
kernel-lt-5.4.212-1.el7.elrepo.x86_64
kernel-tools-libs-3.10.0-1062.9.1.el7.x86_64
kernel-lt-devel-5.4.212-1.el7.elrepo.x86_64
kernel-3.10.0-1062.9.1.el7.x86_64
kernel-headers-3.10.0-1160.76.1.el7.x86_64
kernel-tools-3.10.0-1062.9.1.el7.x86_64

删除旧内核,这一步是可选的

yum remove -y  kernel-devel-3.10.0   kernel-3.10.0  kernel-headers-3.10.0 

# 查看已安装内核
rpm -qa | grep kernel

# 也可以安装 yum-utils 工具,当系统安装的内核大于3个时,会自动删除旧的内核版本
yum install -y yum-utils

升级内核工具包

# 删除旧版本工具包--可选
yum remove kernel-tools-libs.x86_64 kernel-tools.x86_64

# 安装新版本工具包
yum --disablerepo="*" --enablerepo=elrepo-kernel install -y kernel-lt-tools.x86_64

查看已安装内核

$ rpm -qa | grep kernel

kernel-lt-5.4.212-1.el7.elrepo.x86_64
kernel-lt-devel-5.4.212-1.el7.elrepo.x86_64
kernel-lt-tools-libs-5.4.212-1.el7.elrepo.x86_64
kernel-lt-tools-5.4.212-1.el7.elrepo.x86_64

内核升级完成后可再次重启系统进行验证

参考链接

CentOS7 内核升级
CentOS7 内核升级