服务器环境

操作系统:CentOS 7 2009版本 最小化安装

1、升级内核

通过官网查看长期稳定版内核 https://www.kernel.org/

操作方法可参考elrepo网站:http://elrepo.org/tiki/HomePage

本文档操作步骤参考:Linux centos7升级内核(两种方法:内核编译和yum更新)

# 查看内核版本
[root@jumpserver ~]# uname -rs
Linux 3.10.0-1160.el7.x86_64
# CentOS 允许使用 ELRepo,这是一个第三方仓库,可以将内核升级到最新版本。
# 在CentOS 7上启用 ELRepo 仓库,运行如下命令:
# 导入该源的秘钥
[root@jumpserver ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# 启用该源仓库
[root@jumpserver ~]# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
获取http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
获取http://elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
准备中...                          ################################# [100%]
正在升级/安装...
   1:elrepo-release-7.0-4.el7.elrepo  ################################# [100%]

# 查看有哪些内核版本可供安装
[root@jumpserver ~]# yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
已加载插件:fastestmirror
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrors.elrepo.org/mirrors-elrepo-kernel.el7 error was
12: Timeout on http://mirrors.elrepo.org/mirrors-elrepo-kernel.el7: (28, 'Connection timed out after 30000 milliseconds')
 * elrepo-kernel: elrepo.org
elrepo-kernel                                            | 3.0 kB     00:00
elrepo-kernel/primary_db                                   | 2.1 MB   00:03
可安装的软件包
elrepo-release.noarch                   7.0-6.el7.elrepo           elrepo-kernel
kernel-lt.x86_64                        5.4.225-1.el7.elrepo       elrepo-kernel
kernel-lt-devel.x86_64                  5.4.225-1.el7.elrepo       elrepo-kernel
kernel-lt-doc.noarch                    5.4.225-1.el7.elrepo       elrepo-kernel
kernel-lt-headers.x86_64                5.4.225-1.el7.elrepo       elrepo-kernel
kernel-lt-tools.x86_64                  5.4.225-1.el7.elrepo       elrepo-kernel
kernel-lt-tools-libs.x86_64             5.4.225-1.el7.elrepo       elrepo-kernel
kernel-lt-tools-libs-devel.x86_64       5.4.225-1.el7.elrepo       elrepo-kernel
kernel-ml.x86_64                        6.0.10-1.el7.elrepo        elrepo-kernel
kernel-ml-devel.x86_64                  6.0.10-1.el7.elrepo        elrepo-kernel
kernel-ml-doc.noarch                    6.0.10-1.el7.elrepo        elrepo-kernel
kernel-ml-headers.x86_64                6.0.10-1.el7.elrepo        elrepo-kernel
kernel-ml-tools.x86_64                  6.0.10-1.el7.elrepo        elrepo-kernel
kernel-ml-tools-libs.x86_64             6.0.10-1.el7.elrepo        elrepo-kernel
kernel-ml-tools-libs-devel.x86_64       6.0.10-1.el7.elrepo        elrepo-kernel
perf.x86_64                             5.4.225-1.el7.elrepo       elrepo-kernel
python-perf.x86_64                      5.4.225-1.el7.elrepo       elrepo-kernel

# 安装长期稳定版本,5.4.225-1.el7.elrepo
[root@jumpserver ~]# yum --enablerepo=elrepo-kernel install kernel-lt -y
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrors.elrepo.org/mirrors-elrepo.el7 error was
12: Timeout on http://mirrors.elrepo.org/mirrors-elrepo.el7: (28, 'Connection timed out after 30008 milliseconds')
Could not retrieve mirrorlist http://mirrors.elrepo.org/mirrors-elrepo-kernel.el7 error was
12: Timeout on http://mirrors.elrepo.org/mirrors-elrepo-kernel.el7: (28, 'Connection timed out after 30000 milliseconds')
 * base: mirrors.bfsu.edu.cn
 * elrepo: elrepo.org
 * elrepo-kernel: elrepo.org
 * extras: mirrors.nju.edu.cn
 * updates: mirrors.bfsu.edu.cn
base                                                     | 3.6 kB     00:00
elrepo                                                   | 3.0 kB     00:00
extras                                                   | 2.9 kB     00:00
updates                                                  | 2.9 kB     00:00
(1/5): base/7/x86_64/group_gz                              | 153 kB   00:00
(2/5): extras/7/x86_64/primary_db                          | 249 kB   00:00
(3/5): base/7/x86_64/primary_db                            | 6.1 MB   00:00
(4/5): updates/7/x86_64/primary_db                         |  18 MB   00:01
(5/5): elrepo/primary_db                                   | 449 kB   00:02
正在解决依赖关系
--> 正在检查事务
---> 软件包 kernel-lt.x86_64.0.5.4.225-1.el7.elrepo 将被 安装
--> 解决依赖关系完成

依赖关系解决

================================================================================
 Package        架构        版本                       源                  大小
================================================================================
正在安装:
 kernel-lt      x86_64      5.4.225-1.el7.elrepo       elrepo-kernel       50 M

事务概要
================================================================================
安装  1 软件包

总下载量:50 M
安装大小:228 M
Downloading packages:
kernel-lt-5.4.225-1.el7.elrepo.x86_64.rpm                  |  50 MB   00:09
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
  正在安装    : kernel-lt-5.4.225-1.el7.elrepo.x86_64                       1/1
  验证中      : kernel-lt-5.4.225-1.el7.elrepo.x86_64                       1/1

已安装:
  kernel-lt.x86_64 0:5.4.225-1.el7.elrepo

完毕!
[root@jumpserver ~]#
# 重启服务器,手动选择新安装的内核版本进入系统!!!

2、设置GRUB默认的内核版本

为了让新安装的内核成为默认启动选项,需要修改GRUB配置。

[root@jumpserver ~]# vi /etc/default/grub

修改前文件内容如下:

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos_jumpserver/root rd.lvm.lv=centos_jumpserver/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

修改后文件内容如下:(修改为GRUB_DEFAULT=0,意思是GRUB初始化页面的第一个内核做为默认内核。)

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=0
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos_jumpserver/root rd.lvm.lv=centos_jumpserver/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

执行命令:grub2-mkconfig -o /boot/grub2/grub.cfg

[root@jumpserver ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.4.225-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-5.4.225-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-8cb56c471048fd4eacb09d19b73e2a71
Found initrd image: /boot/initramfs-0-rescue-8cb56c471048fd4eacb09d19b73e2a71.img
done
[root@jumpserver ~]#
# 查看升级后内核
[root@jumpserver ~]# uname -rs
Linux 5.4.225-1.el7.elrepo.x86_64
[root@jumpserver ~]#

3、切换repo源(可选操作)

# 备份
[root@jumpserver redis-stable]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

# 下载新的CentOS-Base.repo到/etc/yum.repos.d/
[root@jumpserver redis-stable]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
--2022-12-01 15:14:41--  https://mirrors.aliyun.com/repo/Centos-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 113.219.178.239, 113.219.178.243, 113.219.178.244, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|113.219.178.239|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2523 (2.5K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”

100%[======================================>] 2,523       --.-K/s 用时 0.02s

2022-12-01 15:14:42 (142 KB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])

[root@jumpserver redis-stable]#

# 非阿里云ECS用户会出现 Couldn't resolve host 'mirrors.cloud.aliyuncs.com' 信息,不影响使用。用户也可自行修改相关配置: eg:
[root@jumpserver redis-stable]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

# 运行yum makecache生成缓存
[root@jumpserver redis-stable]# yum makecache
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrors.elrepo.org/mirrors-elrepo.el7 error was
12: Timeout on http://mirrors.elrepo.org/mirrors-elrepo.el7: (28, 'Connection timed out after 30001 milliseconds')
 * elrepo: elrepo.org
base                                                     | 3.6 kB     00:00
elrepo                                                   | 3.0 kB     00:00
extras                                                   | 2.9 kB     00:00
updates                                                  | 2.9 kB     00:00
(1/8): elrepo/other_db                                     |  35 kB   00:01
(2/8): elrepo/filelists_db                                 |  43 kB   00:02
(3/8): extras/7/x86_64/filelists_db                        | 276 kB   00:02
(4/8): extras/7/x86_64/other_db                            | 149 kB   00:01
(5/8): updates/7/x86_64/other_db                           | 1.2 MB   00:08
(6/8): base/7/x86_64/other_db                              | 2.6 MB   00:26
(7/8): base/7/x86_64/filelists_db                          | 7.2 MB   01:00
(8/8): updates/7/x86_64/filelists_db                       |  10 MB   01:30
元数据缓存已建立

[root@jumpserver redis-stable]#
Logo

更多推荐