Three ways to upgrade the kernel of CentOS (yum/rpm/source code)

Three ways to upgrade the kernel of CentOS (yum/rpm/source code)

In the process of using CentOS, it is inevitable to upgrade the kernel, but sometimes due to source code compilation dependencies, not all programs support the latest kernel version, so the following will introduce three ways to upgrade the kernel.

Precautions

关于内核种类:
kernel-ml 中的ml是英文【 mainline stable 】的缩写,elrepo-kernel中罗列出来的是最新的稳定主线版本。
kernel-lt 中的lt是英文【 long term support 】的缩写,elrepo-kernel中罗列出来的长期支持版本。
# 检查内核版本
uname -r

One, yum installation

1. Import warehouse source

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

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm

2. View the installable software packages

yum --enablerepo="elrepo-kernel" list --showduplicates | sort -r | grep kernel-ml.x86_64

3. Select ML or LT version to install

If no version is specified, the latest is installed by default

# 安装 ML 版本
yum --enablerepo=elrepo-kernel install  kernel-ml-devel kernel-ml -y   

# 安装 LT 版本,K8S全部选这个
yum --enablerepo=elrepo-kernel install kernel-lt-devel kernel-lt -y

4. View the existing kernel startup sequence

awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg

5. Modify the default startup items

xxx is the serial number, and the xth item in the specified startup list is the startup item, and x starts counting from 0

grub2-set-default xxxx

For example, set to start with 4.4 kernel

Then enter "grub2-set-default 0" directly, and you can start from 4.4 next time

# 查看内核启动序号
[root@localhost ~] awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg

CentOS Linux (4.4.179-1.el7.elrepo.x86_64) 7 (Core)

CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)

CentOS Linux (0-rescue-6d4c599606814867814f1a8eec7bfd1e) 7 (Core)


# 设置启动序号
[root@localhost ~] grub2-set-default 0

# 重启
reboot

# 检查内核版本
uname -r

Two, RPM installation

check kernel version

uname -r

1. Find the version

Because the ELRepo source is the latest version, the old version of the kernel can only be downloaded manually.

Find the historical version of kernel rpm:

http://mirrors.coreix.net/elrepo-archive-archive/kernel/el7/x86_64/RPMS/

2. A total of three types of rpm need to be downloaded

wget http://mirrors.coreix.net/elrepo-archive-archive/kernel/el7/x86_64/RPMS/kernel-lt-devel-4.4.215-1.el7.elrepo.x86_64.rpm
wget http://mirrors.coreix.net/elrepo-archive-archive/kernel/el7/x86_64/RPMS/kernel-lt-headers-4.4.215-1.el7.elrepo.x86_64.rpm
wget http://mirrors.coreix.net/elrepo-archive-archive/kernel/el7/x86_64/RPMS/kernel-lt-4.4.215-1.el7.elrepo.x86_64.rpm

3. Install the kernel

rpm -ivh kernel-lt-4.4.215-1.el7.elrepo.x86_64.rpm
rpm -ivh kernel-lt-devel-4.4.215-1.el7.elrepo.x86_64.rpm
或者
#一键安装所有
rpm -Uvh *.rpm

4. Confirm that the kernel version is installed

[root@localhost ~]# rpm -qa | grep kernel
kernel-headers-3.10.0-1160.15.2.el7.x86_64
kernel-devel-3.10.0-1160.49.1.el7.x86_64
kernel-tools-libs-3.10.0-957.el7.x86_64
kernel-3.10.0-957.el7.x86_64
kernel-ml-4.9.9-1.el7.elrepo.x86_64
kernel-lt-4.4.215-1.el7.elrepo.x86_64
kernel-tools-3.10.0-957.el7.x86_64
kernel-lt-devel-4.4.215-1.el7.elrepo.x86_64

5. Set start

# 查看启动顺序
[root@localhost ~]# awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
CentOS Linux (4.4.215-1.el7.elrepo.x86_64) 7 (Core)
CentOS Linux (4.9.9-1.el7.elrepo.x86_64) 7 (Core)
CentOS Linux (3.10.0-957.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-b91f945269084aa98e8257311ee713c5) 7 (Core)

# 设置启动顺序
[root@localhost ~]# grub2-set-default 0

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

3. Source code installation

1. Install the core software package

yum install -y gcc make git ctags ncurses-devel openssl-devel
yum install -y bison flex elfutils-libelf-devel bc

2. Create a kernel compilation directory

homeUse kernelbuildthe directory under

mkdir ~/kernelbuild

3. Get the kernel source code

Mirror station of Tsinghua University: https://mirror.tuna.tsinghua.edu.cn/kernel/v4.x/?C=M&O=D

Other source code installation package download address: https://mirrors.edge.kernel.org/pub/linux/kernel/

  • linux-4.xx.xx.tar.xz
  • linux-4.xx.xx.tar.gz
  • Both formats are acceptable, tar.xz has a higher compression ratio and smaller files.
在线下载:wget https://mirror.tuna.tsinghua.edu.cn/kernel/v4.x/linux-4.17.11.tar.xz

4. Decompress the kernel code

Unzip it and enter the source code directory:

tar -xvJf linux-4.17.11.tar.xz

To make sure the kernel tree is absolutely clean, go into the kernel directory and execute the make mrproper command:

cd linux-4.17.11
make clean && make mrproper

5. Kernel configuration

Copy the current kernel configuration file

config-3.10.0-862.el7.x86_64It is the kernel configuration file of my current environment, modified according to the actual situation

cp /boot/config-3.10.0-862.el7.x86_64 .config

advanced configuration

y is enable, n is disable, m is enable if needed.
make menuconfig: old ncurses interface, replaced by nconfig
make nconfig: new command line ncurses interface

6. Compile and install

Compile the kernel

如果你是四核的机器,x可以是8
make -j x

install kernel

Install after compiling the kernel: Warning: From here on, you need root authority to execute the command, otherwise it will fail.

make modules_install install

7. Set start

# 查看启动顺序
[root@localhost ~]# awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
CentOS Linux (4.17.11-1.el7.elrepo.x86_64) 7 (Core)
CentOS Linux (4.9.9-1.el7.elrepo.x86_64) 7 (Core)
CentOS Linux (3.10.0-957.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-b91f945269084aa98e8257311ee713c5) 7 (Core)

# 设置启动顺序
[root@localhost ~]# grub2-set-default 0

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

4. Uninstall/downgrade the kernel

For example:

When the 5.4.103 version of the LT kernel already exists in the system, if you continue to install the 4.4.215 version of the LT kernel, you will be prompted:package kernel-lt-5.4.103-1.el7.elrepo.x86_64 (which is newer than kernel-lt-4.4.215-1.el7.elrepo.x86_64) is already installed

At this time, you need to downgrade the kernel and uninstall the latest version of the kernel.

1. View the current kernel version of the system

[root@localhost ~]# uname -r
5.4.103-1.el7.elrepo.x86_64

2. View all kernels in the system

[root@localhost ~]# rpm -qa | grep kernel
kernel-headers-3.10.0-1160.15.2.el7.x86_64
kernel-devel-3.10.0-1160.49.1.el7.x86_64
kernel-tools-libs-3.10.0-957.el7.x86_64
kernel-3.10.0-957.el7.x86_64
kernel-ml-4.9.9-1.el7.elrepo.x86_64
kernel-lt-5.4.103-1.el7.elrepo.x86_64
kernel-tools-3.10.0-957.el7.x86_64
kernel-lt-devel-5.4.103-1.el7.elrepo.x86_64

3. Delete the specified kernel

Here is an example of deleting the 5.4.103 version of the LT kernel

**NOTE:** The current kernel version cannot be uninstalled. Rebooting is not necessarily required after uninstallation

yum remove -y kernel-lt-devel-5.4.103-1.el7.elrepo.x86_64

yum remove -y kernel-lt-5.4.103-1.el7.elrepo.x86_64

Check the kernel version after uninstallation

[root@localhost ~]# rpm -qa | grep kernel
kernel-headers-3.10.0-1160.15.2.el7.x86_64
kernel-devel-3.10.0-1160.49.1.el7.x86_64
kernel-tools-libs-3.10.0-957.el7.x86_64
kernel-3.10.0-957.el7.x86_64
kernel-ml-4.9.9-1.el7.elrepo.x86_64
kernel-tools-3.10.0-957.el7.x86_64

Guess you like

Origin blog.csdn.net/weixin_45019350/article/details/125710682