centos7在线升级内核

使用 uname -sr 查看自己内核版本号:

[root@localhost ~]# uname -sr
Linux 3.10.0-862.el7.x86_64
然后我们在浏览器中输入 www.kernel.org,查看centos,最新内核版本为:4.18

一:、安装elrepo的yum源
elrepo是CentOS十分有用的稳定的软件源,这是一个第三方仓库,可以将内核升级到最新版本。我们可以在命令行中输入:

首先导入public key

[root@localhost ~]# rpm –import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
[root@localhost ~]# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
Retrieving http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
Retrieving http://elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
Preparing… ################################# [100%]
Updating / installing…
1:elrepo-release-7.0-3.el7.elrepo ################################# [100%]
仓库启用后,我们可以使用下面的命令列出可用的内核相关包:

yum –disablerepo="*" –enablerepo="elrepo-kernel" list available

Loaded plugins: fastestmirror, langpacks
Repository ‘QJC’ is missing name in configuration, using id
Loading mirror speeds from cached hostfile

  • elrepo-kernel: hkg.mirror.rackspace.com
    elrepo-kernel | 2.9 kB 00:00:00
    elrepo-kernel/primary_db | 1.8 MB 00:00:00
    Available Packages
    kernel-lt.x86_64 4.4.152-1.el7.elrepo elrepo-kernel
    kernel-lt-devel.x86_64 4.4.152-1.el7.elrepo elrepo-kernel
    kernel-lt-doc.noarch 4.4.152-1.el7.elrepo elrepo-kernel
    kernel-lt-headers.x86_64 4.4.152-1.el7.elrepo elrepo-kernel
    kernel-lt-tools.x86_64 4.4.152-1.el7.elrepo elrepo-kernel
    kernel-lt-tools-libs.x86_64 4.4.152-1.el7.elrepo elrepo-kernel
    kernel-lt-tools-libs-devel.x86_64 4.4.152-1.el7.elrepo elrepo-kernel
    kernel-ml.x86_64 4.18.5-1.el7.elrepo elrepo-kernel
    kernel-ml-devel.x86_64 4.18.5-1.el7.elrepo elrepo-kernel
    kernel-ml-doc.noarch 4.18.5-1.el7.elrepo elrepo-kernel
    kernel-ml-headers.x86_64 4.18.5-1.el7.elrepo elrepo-kernel
    kernel-ml-tools.x86_64 4.18.5-1.el7.elrepo elrepo-kernel
    kernel-ml-tools-libs.x86_64 4.18.5-1.el7.elrepo elrepo-kernel
    kernel-ml-tools-libs-devel.x86_64 4.18.5-1.el7.elrepo elrepo-kernel
    perf.x86_64 4.18.5-1.el7.elrepo elrepo-kernel
    python-perf.x86_64 4.18.5-1.el7.elrepo elrepo-kernel
    二:升级内核
    在yum的elrepo源中有ml和lt两种内核,其中ml(mainline)为最新版本的内核,lt为长期支持的内核。

如果要安装ml内核,使用如下命令:

yum –enablerepo=elrepo-kernel -y install kernel-ml kernel-ml-devel

如果要安装lt内核,使用如下命令:

yum –enablerepo=elrepo-kernel -y install kernel-lt kernel-lt-devel

在此我们安装的是ml内核,如下:

yum –enablerepo=elrepo-kernel install kernel-ml
Loaded plugins: fastestmirror, langpacks
Repository ‘QJC’ is missing name in configuration, using id
Loading mirror speeds from cached hostfile

  • elrepo: hkg.mirror.rackspace.com
  • elrepo-kernel: hkg.mirror.rackspace.com
    file:///media/repodata/repomd.xml: [Errno 14] curl#37 – "Couldn’t open file /media/repodata/repomd.xml"
    Trying other mirror.
    elrepo | 2.9 kB 00:00:00
    elrepo/primary_db | 469 kB 00:00:00
    Resolving Dependencies
    –> Running transaction check
    —> Package kernel-ml.x86_64 0:4.18.5-1.el7.elrepo will be installed
    –> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================
Package Arch Version Repository Size

Installing:
kernel-ml x86_64 4.18.5-1.el7.elrepo elrepo-kernel 45 M

Transaction Summary

Install 1 Package

Total download size: 45 M
Installed size: 203 M
Is this ok [y/d/N]: y
Downloading packages:
kernel-ml-4.18.5-1.el7.elrepo.x86_64.rpm | 45 MB 00:00:04
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : kernel-ml-4.18.5-1.el7.elrepo.x86_64 1/1
Verifying : kernel-ml-4.18.5-1.el7.elrepo.x86_64 1/1

Installed:
kernel-ml.x86_64 0:4.18.5-1.el7.elrepo

Complete!
内核升级完毕后,不会立即生效,还需要我们修改grub.conf文件。

方法一:

查看默认启动顺序

awk -F\’ ‘$1=="menuentry " {print $2}’ /boot/grub2/grub.cfg

默认启动的顺序是从0开始

grub2-set-default 0
reboot
uname -r
方法二:
查看默认
grub2-editenv list
查看所有内核
cat /boot/grub2/grub.cfg | grep menuentry
设置
grub2-set-default "CentOS Linux (4.4.212-1.el7.elrepo.x86_64) 7 (Core)"
再次查看
grub2-editenv list
reboot uname -r