Linux 升级内核版本

小版本升级

5.10.75 升级到 5.10.96,这里以AWS服务器为例

查看当前内核版本

1
# uname -sr

查看可以升级的内核版本

1
2
3
4
5
# yum list kernel -q
Installed Packages
kernel.x86_64 5.10.75-79.358.amzn2 installed
Available Packages
kernel.x86_64 5.10.96-90.460.amzn2 @amzn2extra-kernel-5.10

安装新版本内核

1
# yum install kernel-5.10.96-90.460.amzn2

查看系统可用内核,并设置启动顺序

1
2
3
4
5
6
# rpm -q kernel

查看启动顺序
# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /boot/grub2/grub.cfg
0 : Amazon Linux (5.10.96-90.460.amzn2.x86_64) 2
1 : Amazon Linux (5.10.75-79.358.amzn2.x86_64) 2

已经自动将新内核设置为默认启动项了

重启系统生效

1
# reboot

手动设置内核启动项,例如设置 1为默认启动项

1
2
# yum install grub2-pc
# grub2-set-default 1

生成 grub 配置文件

1
# grub2-mkconfig -o /boot/grub2/grub.cfg 

重启系统

1
# reboot

大版本升级

4.x 升级到 5.x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
查看当前内核版本
# uname -sr
Linux 4.14.248-189.473.el7.x86_64

查看操作系统版本
# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

导入 ELRepo 仓库的公共密钥
# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

安装 ELRepo 仓库的 yum 源
# yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm

替换为清华 ELRepo 源
# sed -i "s/mirrorlist=/#mirrorlist=/g" /etc/yum.repos.d/elrepo.repo
# sed -i "s#elrepo.org/linux#mirrors.tuna.tsinghua.edu.cn/elrepo#g" /etc/yum.repos.d/elrepo.repo

更新 yum 缓存
# yum makecache

查看可用的内核版本,kernel-ml(mainline stable):稳定主线版本,kernel-lt(long term support):长期支持版本
# yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
已加载插件:fastestmirror, langpacks
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Loading mirror speeds from cached hostfile
* elrepo-kernel: linux-mirrors.fnal.gov
可安装的软件包
kernel-lt.x86_64 5.4.127-1.el7.elrepo elrepo-kernel
kernel-lt-devel.x86_64 5.4.127-1.el7.elrepo elrepo-kernel
kernel-lt-doc.noarch 5.4.127-1.el7.elrepo elrepo-kernel
kernel-lt-headers.x86_64 5.4.127-1.el7.elrepo elrepo-kernel
kernel-lt-tools.x86_64 5.4.127-1.el7.elrepo elrepo-kernel
kernel-lt-tools-libs.x86_64 5.4.127-1.el7.elrepo elrepo-kernel
kernel-lt-tools-libs-devel.x86_64 5.4.127-1.el7.elrepo elrepo-kernel
kernel-ml.x86_64 5.12.12-1.el7.elrepo elrepo-kernel
kernel-ml-devel.x86_64 5.12.12-1.el7.elrepo elrepo-kernel
kernel-ml-doc.noarch 5.12.12-1.el7.elrepo elrepo-kernel
kernel-ml-headers.x86_64 5.12.12-1.el7.elrepo elrepo-kernel
kernel-ml-tools.x86_64 5.12.12-1.el7.elrepo elrepo-kernel
kernel-ml-tools-libs.x86_64 5.12.12-1.el7.elrepo elrepo-kernel
kernel-ml-tools-libs-devel.x86_64 5.12.12-1.el7.elrepo elrepo-kernel
perf.x86_64 5.12.12-1.el7.elrepo elrepo-kernel
python-perf.x86_64 5.12.12-1.el7.elrepo elrepo-kernel

升级为主线版本
# yum --enablerepo=elrepo-kernel install kernel-ml -y

配置开机加载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
查看可用内核版本及启动顺序
# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /boot/grub2/grub.cfg

查看启动顺序
# yum install grub2-pc
# grub2-editenv list

设置开机启动
# grub2-set-default 0
或者
# grub2-mkconfig -o /boot/grub2/grub.cfg

重启生效
# reboot

重启后查看内核版本
# uname -sr
-------------本文结束感谢您的阅读-------------