centos 7 升级内核

centos 7 升级内核版本

查看当前内核版本

1
2
3
4
5
# uname -r
3.10.0-693.el7.x86_64

# uname -a
Linux k8s-master-01-3.kxl 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

更新yum源仓库和插件

1
yum -y update update

升级内核

导入ELRepo仓库的公共密钥

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

安装ELRepo仓库的yum源

1
# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm

查看可用的系统内核包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
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.205-1.el7.elrepo elrepo-kernel
kernel-lt-devel.x86_64 4.4.205-1.el7.elrepo elrepo-kernel
kernel-lt-doc.noarch 4.4.205-1.el7.elrepo elrepo-kernel
kernel-lt-headers.x86_64 4.4.205-1.el7.elrepo elrepo-kernel
kernel-lt-tools.x86_64 4.4.205-1.el7.elrepo elrepo-kernel
kernel-lt-tools-libs.x86_64 4.4.205-1.el7.elrepo elrepo-kernel
kernel-lt-tools-libs-devel.x86_64 4.4.205-1.el7.elrepo elrepo-kernel
kernel-ml.x86_64 5.4.1-1.el7.elrepo elrepo-kernel
kernel-ml-devel.x86_64 5.4.1-1.el7.elrepo elrepo-kernel
kernel-ml-doc.noarch 5.4.1-1.el7.elrepo elrepo-kernel
kernel-ml-headers.x86_64 5.4.1-1.el7.elrepo elrepo-kernel
kernel-ml-tools.x86_64 5.4.1-1.el7.elrepo elrepo-kernel
kernel-ml-tools-libs.x86_64 5.4.1-1.el7.elrepo elrepo-kernel
kernel-ml-tools-libs-devel.x86_64 5.4.1-1.el7.elrepo elrepo-kernel
perf.x86_64 5.4.1-1.el7.elrepo elrepo-kernel
python-perf.x86_64 5.4.1-1.el7.elrepo elrepo-kernel

        结果为长期维护版本lt为4.4,最新主线稳定版ml为5.4,这里安装的是4.4

安装kernel

1
# yum -y --enablerepo=elrepo-kernel install kernel-lt-4.4.205-1.el7.elrepo kernel-lt-devel-4.4.205-1.el7.elrepo

卸载kernel工具包

1
yum remove kernel-tools-libs-3.10.0-1062.4.3.el7.x86_64 kernel-tools-3.10.0-1062.4.3.el7.x86_64

安装新版本的工具包

1
yum --disablerepo=\* --enablerepo=elrepo-kernel -y install kernel-lt-tools.x86_64

设置 grub2

        内核安装好后,需要设置为默认启动选项并重启后才会生效

查看当前实际启动内核版本

1
grub2-editenv list

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

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

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

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

  • 方法1
    通过 grub2-set-default 0 命令设置,其中 0 是上面查询出来的可用内核

    1
    2
    grub2-set-default 0
    grub2-editenv list
  • 方法2
    编辑 /etc/default/grub 文件,设置 GRUB_DEFAULT=0,通过上面查询显示的编号为 0 的内核作为默认内核。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # vim /etc/default/grub
    GRUB_TIMEOUT=1
    GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
    GRUB_DEFAULT=0
    GRUB_DISABLE_SUBMENU=true
    GRUB_TERMINAL="serial console"
    GRUB_SERIAL_COMMAND="serial --speed=115200"
    GRUB_CMDLINE_LINUX="console=tty0 crashkernel=auto console=ttyS0,115200"
    GRUB_DISABLE_RECOVERY="true"

生成 grub 配置文件并重启

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

# reboot

验证

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

删除旧内核

查看系统中全部的内核

1
2
3
4
5
6
# rpm -qa | grep kernel
kernel-lt-devel-4.4.205-1.el7.elrepo.x86_64
kernel-3.10.0-693.el7.x86_64
kernel-3.10.0-957.12.1.el7.x86_64
kernel-lt-4.4.205-1.el7.elrepo.x86_64
kernel-3.10.0-1062.4.3.el7.x86_64

yum remove 删除旧内核的 RPM 包

1
# yum remove kernel-3.10.0-693.el7.x86_64 kernel-3.10.0-957.12.1.el7.x86_64  kernel-3.10.0-1062.4.3.el7.x86_64

使用ansible自动化升级

坚持原创技术分享,您的支持将鼓励我继续创作!
0%