CentOS 7 内核升级最新记录(yum及编译) 2024-08

为什么要升级kernel

  • 安全性提升:新版本的kernel通常修复了旧版本中的安全漏洞。
  • 性能改进:升级可能包含对硬件支持的改进,从而提高系统性能。
  • 新功能:新版本可能引入了新的功能或改进了现有功能。
  • 更好的兼容性:新kernel可能会更好地支持最新的硬件和软件。

但是,升级kernel也可能带来风险,比如与现有软件的兼容问题。所以在升级前应该仔细考虑和测试。

测试环境

# 最小化安装 CentOS-7-x86_64-Minimal-1804.iso
[root@localhost-01 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)

[root@localhost-01 ~]# uname -r
3.10.0-862.el7.x86_64

YUM 升级内核

小版本升级

# 查看当前和可升级版本
[root@localhost-01 ~]# yum list kernel | grep kernel
Repodata is over 2 weeks old. Install yum-cron Or run: yum makecache fast
kernel.x86_64                  3.10.0-862.el7                          @anaconda
kernel.x86_64                  3.10.0-1160.119.1.el7                   @updates

# 小版本升级(3.10.0-862.el7 --> 3.10.0-1160.119.1.el7 )
[root@localhost-01 ~]# yum update kernel -y

# 验证
[root@localhost-01 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)

[root@localhost-01 ~]# uname -r
3.10.0-1160.119.1.el7.x86_64

内核下载地址

时间:2024-08

大版本升级

内核种类:
kernel-ml 中的ml是英文【 mainline stable 】的缩写,最新的稳定主线版本。
kernel-lt 中的lt是英文【 long term support 】的缩写,长期支持版本。

# 系统信息
[root@localhost-01 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@localhost-01 ~]# uname -r
3.10.0-862.el7.x86_64

# 更新yum仓库
# 如果只更新软件包可执行:yum -y update --exclude=kernel*
[root@localhost-01 ~]# yum -y update


# 当前内核信息
[root@localhost-01 ~]# rpm -qa | grep kernel
kernel-3.10.0-1160.119.1.el7.x86_64
kernel-tools-libs-3.10.0-1160.119.1.el7.x86_64
kernel-tools-3.10.0-1160.119.1.el7.x86_64
kernel-3.10.0-862.el7.x86_64

# 由于ELRepo仓库的不再支持 CentOS 7 ,只能手动下载需要版本rpm包,这里选择安装版本(kernel-lt-5.4.278)
[root@localhost-01 ~]# wget https://mirrors.aliyun.com/elrepo/archive/kernel/el7/x86_64/RPMS/kernel-lt-5.4.278-1.el7.elrepo.x86_64.rpm
[root@localhost-01 ~]# wget https://mirrors.aliyun.com/elrepo/archive/kernel/el7/x86_64/RPMS/kernel-lt-devel-5.4.278-1.el7.elrepo.x86_64.rpm
[root@localhost-01 ~]# wget https://mirrors.aliyun.com/elrepo/archive/kernel/el7/x86_64/RPMS/kernel-lt-headers-5.4.278-1.el7.elrepo.x86_64.rpm
[root@localhost-01 ~]# wget https://mirrors.aliyun.com/elrepo/archive/kernel/el7/x86_64/RPMS/kernel-lt-tools-5.4.278-1.el7.elrepo.x86_64.rpm
[root@localhost-01 ~]# wget https://mirrors.aliyun.com/elrepo/archive/kernel/el7/x86_64/RPMS/kernel-lt-tools-libs-5.4.278-1.el7.elrepo.x86_64.rpm
[root@localhost-01 ~]# wget https://mirrors.aliyun.com/elrepo/archive/kernel/el7/x86_64/RPMS/kernel-lt-doc-5.4.278-1.el7.elrepo.noarch.rpm

[root@localhost-01 ~]# yum localinstall kernel-lt-*

# 若安装提示冲突,则卸载旧版本tools
[root@localhost-01 ~]# yum remove kernel-tools-libs-3.10.0-1160.119.1.el7.x86_64 kernel-tools-3.10.0-1160.119.1.el7.x86_64

# 查看当前默认内核启动
[root@localhost-01 ~]# grub2-editenv list
saved_entry=CentOS Linux (3.10.0-1160.119.1.el7.x86_64) 7 (Core)

# 查看当前内核启动可选项
[root@localhost-01 ~]# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (5.4.278-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-1160.119.1.el7.x86_64) 7 (Core)
2 : CentOS Linux (3.10.0-862.el7.x86_64) 7 (Core)
3 : CentOS Linux (0-rescue-c4ad8cdbfdb44ce190f1c662815d35f4) 7 (Core)

# 修改默认启动顺序
[root@localhost-01 ~]# ls -l /etc/grub2.cfg
lrwxrwxrwx 1 root root 22 Jun 17 15:23 /etc/grub2.cfg -> ../boot/grub2/grub.cfg

[root@localhost-01 ~]# grub2-set-default 'CentOS Linux (5.4.278-1.el7.elrepo.x86_64) 7 (Core)'
[root@localhost-01 ~]# grub2-editenv list
saved_entry=CentOS Linux (5.4.278-1.el7.elrepo.x86_64) 7 (Core)
 或
[root@localhost-01 ~]# grub2-set-default 0
[root@localhost-01 ~]# grub2-editenv list
saved_entry=0

# 重启生效
[root@localhost-01 ~]# reboot

# 验证结果
[root@localhost-01 ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@localhost-01 ~]# uname -r
5.4.278-1.el7.elrepo.x86_64

删除旧版本内核(可选)

[root@localhost-01 ~]# rpm -qa | grep kernel
kernel-lt-5.4.278-1.el7.elrepo.x86_64
kernel-lt-tools-5.4.278-1.el7.elrepo.x86_64
kernel-3.10.0-1160.119.1.el7.x86_64
kernel-3.10.0-862.el7.x86_64
kernel-lt-tools-libs-5.4.278-1.el7.elrepo.x86_64

[root@localhost-01 ~]# yum remove kernel-3.10.0-862.el7.x86_64 kernel-3.10.0-1160.119.1.el7.x86_64

编译安装升级至指定内核

  • 源码清华镜像站地址
    linux-4.xx.xx.tar.xz
    linux-4.xx.xx.tar.gz
    这两个格式都可以的,tar.xz压缩率更高,文件更小。
# 准备编译环境
[root@localhost ~]# yum install -y gcc make git ctags ncurses-devel openssl-devel
[root@localhost ~]# yum install -y bison flex elfutils-libelf-devel bc wget

# 下载内核源码,并解压缩
[root@localhost ~]# wget https://mirror.tuna.tsinghua.edu.cn/kernel/v4.x/linux-4.19.316.tar.xz
[root@localhost ~]# tar -xf linux-4.19.316.tar.xz
[root@localhost ~]# cd linux-4.19.316
[root@localhost linux-4.19.316]# make clean && make mrproper

#步骤AB,二选一执行
#(可选步骤A: 也可通过现有内核参数去生成新的配置参数文件.config)
[root@localhost linux-4.19.316]# cp /boot/config-3.10.0-1160.el7.x86_64 ./.config
[root@localhost linux-4.19.316]# make oldconfig     (长按回车:保持默认)

#(可选步骤B: 可直接通过menuconfig 增减内核模块或组件,选择后可查看.config参数文件的更新情况)
[root@localhost linux-4.19.316]# make menuconfig

如不修改,则按Tab键至< Save > 回车,下一步选择 < Exit >


# 编译内核
[root@localhost linux-4.19.316]# make -j 4 all

# 说明:
# -j 4: 将使用4核CPU资源去编译内核(grep 'processor' /proc/cpuinfo | sort -u | wc -l)
# all: 编译所有选择模块

# 安装内核
[root@localhost linux-4.19.316]# make modules_install install

# 查看当前内核启动可选项
[root@localhost ~]# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (4.19.316) 7 (Core)
1 : CentOS Linux (3.10.0-862.el7.x86_64) 7 (Core)
2 : CentOS Linux (0-rescue-49ac73bb3441491996c90d9285ffbc3d) 7 (Core)

# 修改默认启动顺序
[root@localhost ~]# grub2-set-default 'CentOS Linux (4.19.316) 7 (Core)'
[root@localhost ~]# grub2-editenv list
saved_entry=CentOS Linux (4.19.316) 7 (Core)
 或
[root@localhost ~]# grub2-set-default 0
[root@localhost ~]# grub2-editenv list
saved_entry=0

# 重启生效
[root@localhost ~]# reboot

# 验证结果
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@localhost ~]# uname -r
4.19.316

posted @ 2024-06-24 10:01  讲文张字  阅读(190)  评论(0编辑  收藏  举报
返回顶部