linux7升级内核

1. 查看当前内核版本#

1
2
3
4
[root@192.168.118.11 ~]#cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
[root@192.168.118.11 ~]#uname -r
3.10.0-1062.el7.x86_64

 

2. 升级内核版本#

(1)配置国内yum镜像

备份系统默认配置源

#mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

下载aliyun镜像yum

1
#wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

更新系统升级程序包

1
#yum update -y 

 

启用 ELRepo仓库

ELRepo 仓库是基于社区的用于企业级 Linux 仓库,提供对 RedHat Enterprise (RHEL) 和 其他基于 RHEL的 Linux 发行版(CentOS、Scientific、Fedora 等)的支持。

ELRepo 聚焦于和硬件相关的软件包,包括文件系统驱动、显卡驱动、网络驱动、声卡驱动和摄像头驱动等。

 

导入ELRepo仓库的公共密钥

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

安装ELRepo 仓库的yum源

1
[root@192.168.118.11 ~]#rpm -ivhU http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

 

3. 安装系统内核包#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@192.168.118.11 ~]#yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * elrepo-kernel: mirrors.tuna.tsinghua.edu.cn
elrepo-kernel                                                                                                                                                                              | 2.9 kB  00:00:00     
elrepo-kernel/primary_db                                                                                                                                                                   | 1.8 MB  00:00:00     
Available Packages
elrepo-release.noarch                                                                                    7.0-4.el7.elrepo                                                                            elrepo-kernel
kernel-lt.x86_64                                                                                         4.4.199-1.el7.elrepo                                                                        elrepo-kernel
kernel-lt-devel.x86_64                                                                                   4.4.199-1.el7.elrepo                                                                        elrepo-kernel
kernel-lt-doc.noarch                                                                                     4.4.199-1.el7.elrepo                                                                        elrepo-kernel
kernel-lt-headers.x86_64                                                                                 4.4.199-1.el7.elrepo                                                                        elrepo-kernel
kernel-lt-tools.x86_64                                                                                   4.4.199-1.el7.elrepo                                                                        elrepo-kernel
kernel-lt-tools-libs.x86_64                                                                              4.4.199-1.el7.elrepo                                                                        elrepo-kernel
kernel-lt-tools-libs-devel.x86_64                                                                        4.4.199-1.el7.elrepo                                                                        elrepo-kernel
kernel-ml.x86_64                                                                                         5.3.9-1.el7.elrepo                                                                          elrepo-kernel
kernel-ml-devel.x86_64                                                                                   5.3.9-1.el7.elrepo                                                                          elrepo-kernel
kernel-ml-doc.noarch                                                                                     5.3.9-1.el7.elrepo                                                                          elrepo-kernel
kernel-ml-headers.x86_64                                                                                 5.3.9-1.el7.elrepo                                                                          elrepo-kernel
kernel-ml-tools.x86_64                                                                                   5.3.9-1.el7.elrepo                                                                          elrepo-kernel
kernel-ml-tools-libs.x86_64                                                                              5.3.9-1.el7.elrepo                                                                          elrepo-kernel
kernel-ml-tools-libs-devel.x86_64                                                                        5.3.9-1.el7.elrepo                                                                          elrepo-kernel
perf.x86_64                                                                                              5.3.9-1.el7.elrepo                                                                          elrepo-kernel
python-perf.x86_64                                                                                       5.3.9-1.el7.elrepo                                                                          elrepo-kerne

 

可以看到上面有 kernel-lt 4.4.199 和 kernel-ml 5.3.9, kernel lt 和 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),更加稳定一些。

1
#yum --enablerepo=elrepo-kernel install kernel-lt -y

--enablerepo 选项开启 CentOS 系统上的指定仓库。

 

查看系统上所有可用的内核:

1
2
3
4
5
#awk -F \' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg 
0 : CentOS Linux (4.4.199-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-1062.4.1.el7.x86_64) 7 (Core)
2 : CentOS Linux (3.10.0-1062.el7.x86_64) 7 (Core)
3 : CentOS Linux (0-rescue-a19b5bd4e2cf4a9aa74027ab3c4150a3) 7 (Core)

设置新的内核为grub2的默认版本

服务器上存在4 个内核,我们要使用 4.4.199 这个版本,可以通过 grub2-set-default 0 命令或编辑 /etc/default/grub 文件来设置

 

方法1 通过 grub2-set-default 0 命令设置

其中 0  是上面查询出来的可用内核

1
#grub2-set-default 0

 

方法2 编辑 /etc/default/grub 文件

设置 GRUB_DEFAULT=0,通过上面查询显示的编号为 0 的内核作为默认内核:

1
2
3
4
5
6
7
8
9
#vim /etc/default/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/root rd.lvm.lv=centos/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

 

使用上面两种方式都必须执行这一步,生成 grub 文件并重启:

1
2
3
4
5
6
7
8
9
10
11
12
#grub2-mkconfig -o /boot/grub2/grub.cfg 
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.4.199-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-4.4.199-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-1062.4.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1062.4.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-1062.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1062.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-a19b5bd4e2cf4a9aa74027ab3c4150a3
Found initrd image: /boot/initramfs-0-rescue-a19b5bd4e2cf4a9aa74027ab3c4150a3.img
done
#reboot

 

启动查看内核:

 

 

验证:

1
2
#uname -r
4.4.199-1.el7.elrepo.x86_64

 

4. 删除旧的内核(可选操作)#

重要提示:要删除旧的内核必须在重启完成当前系统为新的内核时,才能进行删除。

查看系统中全部的内核:

1
2
3
4
5
6
7
#rpm -qa | egrep kernel
kernel-3.10.0-1062.el7.x86_64
kernel-tools-3.10.0-1062.4.1.el7.x86_64
kernel-3.10.0-1062.4.1.el7.x86_64
kernel-tools-libs-3.10.0-1062.4.1.el7.x86_64
kernel-headers-3.10.0-1062.4.1.el7.x86_64
kernel-lt-4.4.199-1.el7.elrepo.x86_64

 

方法1 yum remove 删除旧内核的 rpm 包

1
2
3
4
5
yum remove kernel-3.10.0-1062.el7.x86_64 \
kernel-tools-3.10.0-1062.4.1.el7.x86_64 \
kernel-3.10.0-1062.4.1.el7.x86_64 \
kernel-tools-libs-3.10.0-1062.4.1.el7.x86_64 \
kernel-headers-3.10.0-1062.4.1.el7.x86_64 -y

 

方法2 yum-utils 工具

如果安装的内核不多于 3 个,yum-utils 工具不会删除任何一个。只有在安装的内核大于 3 个时,才会自动删除旧内核。

安装 yum-utils

1
#yum install yum-utils -y

删除旧版本:

1
#package-cleanup --oldkernels

 

启动查看内核

 

 

 

 

posted @ 2021-03-01 17:51  昌北F4  阅读(359)  评论(0编辑  收藏  举报