使用命令将Ubuntu Linux内核版本升级或降级到指定版本

本文以ubuntu 18.4.1为例,内核版本为4.15.0-153-generic,降级到内核版本为4.15.0-122-generic。
1.查看当前系统版本信息

root @ubuntu18X4X1: ~#uname - r
     4.15 .0 - 153 - generic
root @ubuntu18X4X1: ~#lsb_release - a
     No LSB modules are available.
     Distributor ID: Ubuntu
     Description: Ubuntu 18.04 .1 LTS
     Release: 18.04
     Codename: bionic

2.查看当前系统已安装的内核镜像

root@ubuntu18X4X1:~# dpkg --get-selections |grep linux-image
     linux-image-4.15.0-143-generic                  install
     linux-image-4.15.0-153-generic                  install
     linux-image-generic                             install

3.查询指定版本的linux镜像包,这里我查询4.15.0-122版本

root@ubuntu18X4X1:~# apt-cache search linux| grep 4.15.0-122
     linux-buildinfo-4.15.0-122-generic - Linux kernel buildinfo for version 4.15.0 on 64 bit x86 SMP
     linux-buildinfo-4.15.0-122-lowlatency - Linux kernel buildinfo for version 4.15.0 on 64 bit x86 SMP
     linux-cloud-tools-4.15.0-122 - Linux kernel version specific cloud tools for version 4.15.0-122
     linux-cloud-tools-4.15.0-122-generic - Linux kernel version specific cloud tools for version 4.15.0-122
     linux-cloud-tools-4.15.0-122-lowlatency - Linux kernel version specific cloud tools for version 4.15.0-122
     linux-headers-4.15.0-122 - Header files related to Linux kernel version 4.15.0
     linux-headers-4.15.0-122-generic - Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
     linux-headers-4.15.0-122-lowlatency - Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
     linux-image-4.15.0-122-generic - Signed kernel image generic
     linux-image-4.15.0-122-lowlatency - Signed kernel image lowlatency
     linux-image-unsigned-4.15.0-122-generic - Linux kernel image for version 4.15.0 on 64 bit x86 SMP
     linux-image-unsigned-4.15.0-122-lowlatency - Linux kernel image for version 4.15.0 on 64 bit x86 SMP
     linux-modules-4.15.0-122-generic - Linux kernel extra modules for version 4.15.0 on 64 bit x86 SMP
     linux-modules-4.15.0-122-lowlatency - Linux kernel extra modules for version 4.15.0 on 64 bit x86 SMP
     linux-modules-extra-4.15.0-122-generic - Linux kernel extra modules for version 4.15.0 on 64 bit x86 SMP
     linux-tools-4.15.0-122 - Linux kernel version specific tools for version 4.15.0-122
     linux-tools-4.15.0-122-generic - Linux kernel version specific tools for version 4.15.0-122
     linux-tools-4.15.0-122-lowlatency - Linux kernel version specific tools for version 4.15.0-122
     linux-modules-nvidia-390-4.15.0-122-generic - Linux kernel nvidia modules for version 4.15.0-122
     linux-modules-nvidia-390-4.15.0-122-lowlatency - Linux kernel nvidia modules for version 4.15.0-122
     linux-modules-nvidia-418-server-4.15.0-122-generic - Linux kernel nvidia modules for version 4.15.0-122
     linux-modules-nvidia-418-server-4.15.0-122-lowlatency - Linux kernel nvidia modules for version 4.15.0-122
     linux-modules-nvidia-435-4.15.0-122-generic - Linux kernel nvidia modules for version 4.15.0-122
     linux-modules-nvidia-435-4.15.0-122-lowlatency - Linux kernel nvidia modules for version 4.15.0-122
     linux-modules-nvidia-440-server-4.15.0-122-generic - Linux kernel nvidia modules for version 4.15.0-122
     linux-modules-nvidia-440-server-4.15.0-122-lowlatency - Linux kernel nvidia modules for version 4.15.0-122
     linux-modules-nvidia-450-4.15.0-122-generic - Linux kernel nvidia modules for version 4.15.0-122
     linux-modules-nvidia-450-4.15.0-122-lowlatency - Linux kernel nvidia modules for version 4.15.0-122
     linux-modules-nvidia-450-server-4.15.0-122-generic - Linux kernel nvidia modules for version 4.15.0-122
     linux-modules-nvidia-450-server-4.15.0-122-lowlatency - Linux kernel nvidia modules for version 4.15.0-122

4.安装上面查到的指定版本内核的linux系统头文件及镜像

root@ubuntu18X4X1:~# apt-get install linux-headers-4.15.0-122-generic linux-image-4.15.0-122-generic  
     Reading package lists... Done
     Building dependency tree       
     Reading state information... Done
     The following additional packages will be installed:
       linux-headers-4.15.0-122 linux-modules-4.15.0-122-generic
      。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
       。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  
     Found linux image: /boot/vmlinuz-4.15.0-143-generic
     Found initrd image: /boot/initrd.img-4.15.0-143-generic
     Found linux image: /boot/vmlinuz-4.15.0-122-generic
     Found initrd image: /boot/initrd.img-4.15.0-122-generic
     done

5.查看当前系统中内核的启动顺序

root@ubuntu18X4X1:~# grep menuentry /boot/grub/grub.cfg
     if [ x"${feature_menuentry_id}" = xy ]; then
       menuentry_id_option="--id"
       menuentry_id_option=""
     export menuentry_id_option
     menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os      $menuentry_id_option 'gnulinux-simple-15bb239d-04e5-465d-a638-5c0025d374df' {
     submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-15bb239d-04e5-465d-a638-5c0025d374df' {
        menuentry 'Ubuntu, with Linux 4.15.0-153-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-153-generic-advanced-15bb239d-04e5-465d-a638-5c0025d374df' {
        menuentry 'Ubuntu, with Linux 4.15.0-153-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-153-generic-recovery-15bb239d-04e5-465d-a638-5c0025d374df' {
        menuentry 'Ubuntu, with Linux 4.15.0-143-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-143-generic-advanced-15bb239d-04e5-465d-a638-5c0025d374df' {
        menuentry 'Ubuntu, with Linux 4.15.0-143-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-143-generic-recovery-15bb239d-04e5-465d-a638-5c0025d374df' {
        menuentry 'Ubuntu, with Linux 4.15.0-122-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-122-generic-advanced-15bb239d-04e5-465d-a638-5c0025d374df' {
        menuentry 'Ubuntu, with Linux 4.15.0-122-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-122-generic-recovery-15bb239d-04e5-465d-a638-5c0025d374df' {
        menuentry 'Ubuntu, with Linux 4.15.0-76-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-76-generic-advanced-15bb239d-04e5-465d-a638-5c0025d374df' {
        menuentry 'Ubuntu, with Linux 4.15.0-76-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-76-generic-recovery-15bb239d-04e5-465d-a638-5c0025d374df' {

6.如果要升级的版本比当前内核版本高的话,默认新安装的内核就是第一顺序启动的,只需重启系统。否则,需要修改/etc/default目录下的grub文件配置文件,步骤如下:

root@ubuntu18X4X1:~#  vi /etc/default/grub
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=2
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX=""
。。。。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。。。。

修改为:

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

#GRUB_DEFAULT=0
GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 4.15.0-122-generic"
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=2
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX=""
。。。。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。。。。

注:ubuntu 的旧版本(好像是16.04之前),GRUB_DEFAULT是修改成对应序号即可,16.04之后则是要修改成:Advanced options for Ubuntu>Ubuntu, with Linux x.x.x-x-generic的形式。
7.使配置生效

root@ubuntu18X4X1:~# update-grub
     Sourcing file `/etc/default/grub'
     Generating grub configuration file ...
     Found linux image: /boot/vmlinuz-4.15.0-153-generic
     Found initrd image: /boot/initrd.img-4.15.0-153-generic
     Found linux image: /boot/vmlinuz-4.15.0-143-generic
     Found initrd image: /boot/initrd.img-4.15.0-143-generic
     Found linux image: /boot/vmlinuz-4.15.0-122-generic
     Found initrd image: /boot/initrd.img-4.15.0-122-generic
     Found linux image: /boot/vmlinuz-4.15.0-76-generic
     Found initrd image: /boot/initrd.img-4.15.0-76-generic
     done

8.重启,再查看内核版本

root@ubuntu18X4X1:~# reboot
root@ubuntu18X4X1:~# uname -r
     4.15.0-122-generic

可以看到,内核版本变成了4.15.0-122-generic
9.若是已经安装的内核版本太多,可以删除一些(以删除4.15.0-140和4.15.0-143为例),输入命令:

root@ubuntu18X4X1:~# sudo apt-get remove linux-image-4.15.0-140-generic  linux-image-4.15.0-143-generic 
### 关于Ubuntu内核降级 对于操作系统层面的内核版本变更,特别是从高版本向低版本回滚,通常涉及较为复杂的过程。针对希望将Ubuntu系统的内核版本由24.4至更低版本的需求,需注意实际上并不存在所谓“24.4”的Ubuntu内核版本编号,这可能是表述上的误差是混淆了其他软件包发行版的信息。 当确实存在合理需求来更换至更早版本内核时,一般建议遵循如下方法: #### 方法一:通过APT手动安装旧版内核 可以利用`apt-get`命令下载指定版本号的Linux内核镜像文件,并完成安装操作。例如要安装特定的老版本内核(假设为5.4系列),可执行以下指令[^1]: ```bash sudo apt install linux-image-5.4.0-xx-generic ``` 其中`xx`代表具体的修订次数。之后重启计算机,在GRUB引导菜单中选择刚装入的新内核启动项即可生效。 #### 方法二:借助第三方PPA源获取历史版本 如果目标内核不在当前系统默认仓库范围内,则考虑添加可靠的个人包档案(PPA),从中选取所需的历史版本进行部署。不过此途径存在一定风险,务必谨慎评估来源的安全性和兼容性问题后再做决定。 重要提示:无论是哪种方式实现内核级别的倒退动作都充满不确定性因素,极有可能引发硬件驱动失配、安全漏洞暴露等问题,因此强烈反对轻易尝试此类行为除非有充分理由和技术保障措施作为支撑。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不见梦长安

谢谢您的认可~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值