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、重启生效

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于CentOS 7,内核升级可以通过以下步骤完成: 1. 首先,确保你的系统已经安装了最新的补丁和更新。可以运行以下命令更新系统软件包: ``` sudo yum update ``` 2. 安装ELRepo存储库,它包含了更高版本的内核: ``` sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org sudo rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm ``` 3. 安装新的内核版本。你可以使用以下命令列出可用的内核版本: ``` sudo yum --disablerepo='*' --enablerepo=elrepo-kernel list available ``` 4. 选择一个合适的内核版本安装。假设你选择安装最新版本的内核,可以使用以下命令: ``` sudo yum --enablerepo=elrepo-kernel install kernel-ml ``` 5. 安装完成后,重新启动系统使新内核生效: ``` sudo reboot ``` 6. 系统重启后,默认仍然使用旧内核启动。你可以通过编辑GRUB引导配置文件来设置默认启动内核。打开`/etc/default/grub`文件并找到`GRUB_DEFAULT`行,将其设置为新安装的内核版本。例如: ``` GRUB_DEFAULT="0" ``` 7. 保存文件后,更新GRUB配置并生成新的引导文件: ``` sudo grub2-mkconfig -o /boot/grub2/grub.cfg ``` 8. 最后,重启系统使更改生效: ``` sudo reboot ``` 完成以上步骤后,你的CentOS 7系统应该已经成功升级到新的内核版本。请注意,在升级内核之前,务必备份重要数据并确保你了解内核升级可能带来的风险。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值