centos6、7修改内核启动时间、升级内核、修改默认启动内核

由于对于系统内核升级之后;内核不兼容导致启动服务器失败;这个时候就需要修改默认内核版本了;本文基于linux系统操作

一、修改内核启动时间

[root@iZ8vb1o2lqhuz4bjn2ton6Z ~]# egrep 'timeout' /etc/grub2.cfg 
if [ x$feature_timeout_style = xy ] ; then
  set timeout_style=menu
  set timeout=1   # 设置默认启动时间
# Fallback normal timeout code in case the timeout_style feature is
  set timeout=1   # 设置默认启动时间

二、centos6.x升级内核
第一种方式:手动修改
1、切换centos6的镜像源,默认官方已经不提供了,这边可以使用阿里源
CentOS-Base.repo

vim /etc/yum.repos.d/CentOS-Base.repo
[base]
name=CentOS-6.10
enabled=1
failovermethod=priority
baseurl=http://mirrors.cloud.aliyuncs.com/centos-vault/6.10/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.cloud.aliyuncs.com/centos-vault/RPM-GPG-KEY-CentOS-6

[updates]
name=CentOS-6.10
enabled=1
failovermethod=priority
baseurl=http://mirrors.cloud.aliyuncs.com/centos-vault/6.10/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.cloud.aliyuncs.com/centos-vault/RPM-GPG-KEY-CentOS-6

[extras]
name=CentOS-6.10
enabled=1
failovermethod=priority
baseurl=http://mirrors.cloud.aliyuncs.com/centos-vault/6.10/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.cloud.aliyuncs.com/centos-vault/RPM-GPG-KEY-CentOS-6

epel.repo

vim /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
enabled=1
failovermethod=priority
baseurl=http://mirrors.cloud.aliyuncs.com/epel-archive/6/$basearch
gpgcheck=0
gpgkey=http://mirrors.cloud.aliyuncs.com/epel-archive/RPM-GPG-KEY-EPEL-6

2、清除缓存

yum clean all && yum update

3、安装下elrepo

# 加入资源库
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# 安装6版本的ELRepo
rpm -Uvh elrepo-release-6-12.el6.elrepo.noarch.rpm
# 安装新版本内核
yum --enablerepo=elrepo-kernel install kernel-lt -y

4、配置新内核

cp /etc/grub.conf{,.bak}
vim /etc/grub.conf  # 将其中的default修改为0

5、重启机器生效

第二种:
1、直接更新内核

yum update kernel

2、重启生效

三、centos7.x升级内核
1、安装ELRepo
目前为cetnos 7.x提供了ml和lt两个版本的内核,可以根据需求进行选择:
https://elrepo.org/linux/kernel/el7/x86_64/RPMS/

yum update -y
# 导入资源库
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 
# 安装7.x的ELRepo
rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
# 安装新版本内核
yum --enablerepo=elrepo-kernel install kernel-lt -y

2、修改grub文件

vim /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved		#把这里的saved改成0
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet net.ifnames=0"
GRUB_DISABLE_RECOVERY="true"

3、重新加载内核配置

grub2-mkconfig -o /boot/grub2/grub.cfg

4、重启生效

四、centos7.x修改默认启动内核
1、查看当前内核版本

# 第一种方式
[root@iZ8vb1o2lqhuz4bjn2ton6Z ~]# uname -r
3.10.0-1160.59.1.el7.x86_64

# 第二种方式
[root@iZ8vb1o2lqhuz4bjn2ton6Z ~]# grub2-editenv list
saved_entry=CentOS Linux (3.10.0-1160.59.1.el7.x86_64) 7 (Core)

2、查看系统有哪些内核版本

[root@iZ8vb1o2lqhuz4bjn2ton6Z ~]# cat /boot/grub2/grub.cfg |grep ^menuentry
menuentry 'CentOS Linux (3.10.0-1160.66.1.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-1160.59.1.el7.x86_64-advanced-9c8d46a4-6089-4e07-91bf-e6b0ee93c966' {
menuentry 'CentOS Linux (3.10.0-1160.59.1.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-1160.59.1.el7.x86_64-advanced-9c8d46a4-6089-4e07-91bf-e6b0ee93c966' {
menuentry 'CentOS Linux (3.10.0-1160.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-1160.el7.x86_64-advanced-9c8d46a4-6089-4e07-91bf-e6b0ee93c966' {
menuentry 'CentOS Linux (0-rescue-20220322150908035081894917625632) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-20220322150908035081894917625632-advanced-9c8d46a4-6089-4e07-91bf-e6b0ee93c966' {

3、设置默认启动内核版本

[root@iZ8vb1o2lqhuz4bjn2ton6Z ~]# grub2-set-default  'CentOS Linux (3.10.0-1160.66.1.el7.x86_64)'

4、重启机器查看当前内核版本

[root@iZ8vb1o2lqhuz4bjn2ton6Z ~]# uname -r
3.10.0-1160.66.1.el7.x86_64

五、centos6.x修改默认启动内核
1、查看当前内核版本
2、查看当前有几个内核版本

cat /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You do not have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /, eg.
#          root (hd0,0)
#          kernel /boot/vmlinuz-version ro root=/dev/vda1 net.ifnames=0 console=tty0 console=ttyS0,115200n8
#          initrd /boot/initrd-[generic-]version.img
#boot=/dev/vda
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-754.35.1.el6.x86_64)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.32-754.35.1.el6.x86_64 ro root=UUID=b7aae0d4-268c-4b60-914a-f3b48e22819c rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM   net.ifnames=0 console=tty0 console=ttyS0,115200n8
        initrd /boot/initramfs-2.6.32-754.35.1.el6.x86_64.img
title CentOS (2.6.32-696.16.1.el6.x86_64)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.32-696.16.1.el6.x86_64 ro root=UUID=b7aae0d4-268c-4b60-914a-f3b48e22819c rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM   net.ifnames=0 console=tty0 console=ttyS0,115200n8
        initrd /boot/initramfs-2.6.32-696.16.1.el6.x86_64.img
title CentOS 6 (2.6.32-696.el6.x86_64)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=b7aae0d4-268c-4b60-914a-f3b48e22819c rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM   net.ifnames=0 console=tty0 console=ttyS0,115200n8
        initrd /boot/initramfs-2.6.32-696.el6.x86_64.img

可以看到有2.6.32-754.35.1.el6.x86_64、2.6.32-696.16.1.el6.x86_64、2.6.32-696.el6.x86_64三个版本

3、设置默认启动内核,修改default即可
在grub.conf文件中决定开机使用哪个内核版本做启动的参数是default,默认值为0,代表从最新的内核启动。代表启动的内核版本从上往下依次是0、1、2等

vim /boot/grub/grub.conf
default=1
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-754.35.1.el6.x86_64)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.32-754.35.1.el6.x86_64 ro root=UUID=b7aae0d4-268c-4b60-914a-f3b48e22819c rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM   net.ifnames=0 console=tty0 console=ttyS0,115200n8
        initrd /boot/initramfs-2.6.32-754.35.1.el6.x86_64.img
title CentOS (2.6.32-696.16.1.el6.x86_64)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.32-696.16.1.el6.x86_64 ro root=UUID=b7aae0d4-268c-4b60-914a-f3b48e22819c rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM   net.ifnames=0 console=tty0 console=ttyS0,115200n8
        initrd /boot/initramfs-2.6.32-696.16.1.el6.x86_64.img
title CentOS 6 (2.6.32-696.el6.x86_64)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=b7aae0d4-268c-4b60-914a-f3b48e22819c rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM   net.ifnames=0 console=tty0 console=ttyS0,115200n8
        initrd /boot/initramfs-2.6.32-696.el6.x86_64.img


4、重启生效

六、Rocky Linux 8.7如何修改默认启动内核

查看已安装内核:
首先,你可以通过以下命令查看当前系统上已安装的所有内核版本:

   sudo rpm -q kernel

检查当前默认内核:
使用以下命令来查看当前的默认内核:

  sudo grubby --default-kernel

设置默认启动内核:
假设你想将某个特定的内核版本设为默认启动内核,你可以使用 grubby 命令。首先找到你想要设置为默认的内核版本,然后运行以下命令(将替换为你选择的内核版本):

   sudo grubby --set-default /boot/vmlinuz-<version>
例如,如果想要将内核版本 5.14.0-284.el9.x86_64 设置为默认,可以运行:

   sudo grubby --set-default /boot/vmlinuz-5.14.0-284.el9.x86_64

验证更改:
运行以下命令再次检查当前的默认内核,确保更改已生效:

sudo grubby --default-kernel

重启系统:
最后,重启系统以应用新的默认内核:

   sudo reboot
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值