Ubuntu Linux内核版本升级或降级到指定版本(基于ubuntu 18.04示例)

本文以ubuntu 18.04.4系统为例,内核版本为4.15.0-101-generic,降级到内核版本为4.15.0-76-generic。

当前系统版本信息:

root@zyg:~# uname -r
4.15.0-101-generic
root@zyg:~#
root@zyg:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:        18.04
Codename:       bionic

 

1、查看当前系统已安装的内核镜像

root@zyg:~# dpkg --get-selections |grep linux-image
linux-image-4.15.0-101-generic                  install
linux-image-generic                             install

 

2、查询指定版本的linux镜像包

这里以4.15.0-76版本内核为例:

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

注意:

①、如果查询不到,可以在/etc/apt/sources.list配置中,加入源:deb http://security.ubuntu.com/ubuntu trusty-security main

②、也可以在http://kernel.ubuntu.com/~kernel-ppa/mainline/网站中找到所需的deb包。

 

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

root@zyg:~# apt-get install linux-headers-4.15.0-76-generic linux-image-4.15.0-76-generic 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  linux-headers-4.15.0-76 linux-modules-4.15.0-76-generic
Suggested packages:
  fdutils linux-doc-4.15.0 | linux-source-4.15.0 linux-tools
The following NEW packages will be installed:
  linux-headers-4.15.0-76 linux-headers-4.15.0-76-generic linux-image-4.15.0-76-generic linux-modules-4.15.0-76-generic
0 upgraded, 4 newly installed, 0 to remove and 45 not upgraded.
Need to get 33.1 MB of archives.
After this operation, 163 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 linux-headers-4.15.0-76 all 4.15.0-76.86 [10.9 MB]
Get:1 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 linux-headers-4.15.0-76 all 4.15.0-76.86 [10.9 MB]                                                
Get:2 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 linux-headers-4.15.0-76-generic amd64 4.15.0-76.86 [1106 kB]                                      
Get:3 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 linux-modules-4.15.0-76-generic amd64 4.15.0-76.86 [13.0 MB]                                      
Get:4 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 linux-image-4.15.0-76-generic amd64 4.15.0-76.86 [7993 kB]                                        
Fetched 29.1 MB in 8min 56s (54.2 kB/s)                                                                                                                               
Selecting previously unselected package linux-headers-4.15.0-76.
(Reading database ... 71160 files and directories currently installed.)
Preparing to unpack .../linux-headers-4.15.0-76_4.15.0-76.86_all.deb ...
Unpacking linux-headers-4.15.0-76 (4.15.0-76.86) ...
Selecting previously unselected package linux-headers-4.15.0-76-generic.
Preparing to unpack .../linux-headers-4.15.0-76-generic_4.15.0-76.86_amd64.deb ...
Unpacking linux-headers-4.15.0-76-generic (4.15.0-76.86) ...
Selecting previously unselected package linux-modules-4.15.0-76-generic.
Preparing to unpack .../linux-modules-4.15.0-76-generic_4.15.0-76.86_amd64.deb ...
Unpacking linux-modules-4.15.0-76-generic (4.15.0-76.86) ...
Selecting previously unselected package linux-image-4.15.0-76-generic.
Preparing to unpack .../linux-image-4.15.0-76-generic_4.15.0-76.86_amd64.deb ...
Unpacking linux-image-4.15.0-76-generic (4.15.0-76.86) ...
Setting up linux-modules-4.15.0-76-generic (4.15.0-76.86) ...
Setting up linux-image-4.15.0-76-generic (4.15.0-76.86) ...
I: /vmlinuz is now a symlink to boot/vmlinuz-4.15.0-76-generic
I: /initrd.img is now a symlink to boot/initrd.img-4.15.0-76-generic
Setting up linux-headers-4.15.0-76 (4.15.0-76.86) ...
Setting up linux-headers-4.15.0-76-generic (4.15.0-76.86) ...
Processing triggers for linux-image-4.15.0-76-generic (4.15.0-76.86) ...
/etc/kernel/postinst.d/initramfs-tools:
update-initramfs: Generating /boot/initrd.img-4.15.0-76-generic
/etc/kernel/postinst.d/x-grub-legacy-ec2:
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-4.15.0-101-generic
Replacing config file /run/grub/menu.lst with new version
Found kernel: /boot/vmlinuz-4.15.0-101-generic
Found kernel: /boot/vmlinuz-4.15.0-76-generic
Replacing config file /run/grub/menu.lst with new version
Updating /boot/grub/menu.lst ... done

/etc/kernel/postinst.d/zz-update-grub:
Sourcing file `/etc/default/grub'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.15.0-101-generic
Found initrd image: /boot/initrd.img-4.15.0-101-generic
Found linux image: /boot/vmlinuz-4.15.0-76-generic
Found initrd image: /boot/initrd.img-4.15.0-76-generic
done

 

4、查看当前系统中内核的启动顺序

root@zyg:~# 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-b753ddfd-2731-4c32-aa02-9a654abc99c6' {
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-b753ddfd-2731-4c32-aa02-9a654abc99c6' {
        menuentry 'Ubuntu, with Linux 4.15.0-101-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-101-generic-advanced-b753ddfd-2731-4c32-aa02-9a654abc99c6' {
        menuentry 'Ubuntu, with Linux 4.15.0-101-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-101-generic-recovery-b753ddfd-2731-4c32-aa02-9a654abc99c6' {
        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-b753ddfd-2731-4c32-aa02-9a654abc99c6' {
        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-b753ddfd-2731-4c32-aa02-9a654abc99c6' {

 

5、修改内核启动顺序

如果你升级的版本比当前内核版本高的话,默认新安装的内核就是第一顺序启动的,只需重启系统就行了,否则,则需要修改配置文件,具体如下:

root@zyg:~# 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=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="debian-installer/custom-installation=/custom find_preseed=/preseed.cfg auto preseed/file=/floppy/preseed.cfg automatic-ubiquity noprompt priority=critical locale=en_US console-setup/modelcode=evdev"
GRUB_CMDLINE_LINUX=""

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"

将GRUB_DEFAULT=0修改为GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 4.15.0-76-generic",保存退出。

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

6、生效配置

若上面修改了配置文件,则需执行update-grub命令,更新grub配置:

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

之后,reboot,验证是否生效。

 

Ubuntu Linux内核版本升级或降级到指定版本(基于ubuntu 18.04示例)_第1张图片

可以看到,内核已经是4.15.0-76-generic的了。

 

6、移除不用的内核

查询不包括当前内核版本的其它所有内核版本:
dpkg -l | tail -n +6| grep -E 'linux-image-[0-9]+'| grep -Fv $(uname -r)

root@zyg:~# dpkg -l | tail -n +6| grep -E 'linux-image-[0-9]+'| grep -Fv $(uname -r)
ii  linux-image-4.15.0-101-generic         4.15.0-101.102                                  amd64        Signed kernel image generic

输出的内容中可能会包括内核映像的如下三种状态:
rc:表示已经被移除
ii:表示符合移除条件(可移除)
iU:已进入 apt 安装队列,但还未被安装(不可移除)。

 

我这里的4.15.0-101.102 内核为可移除状态,可以执行如下命令删除:

dpkg --purge linux-image-4.4.0-21-generic

 

7、关闭内核或包的自动更新


①、修改系统配置,在/etc/apt/apt.conf.d目录下
10periodic、20auto-upgrades配置中1改为0即可。

 

②、使用apt-mark的hold参数来禁止包更新
apt-mark hold linux-image-x.xx.x-xx-generic
apt-mark hold linux-image-extra-x.xx.x-xx-generic

解除上面设置执行,带unhold的参数即可:
sudo apt-mark unhold linux-image-x.xx.x-xx-generic
sudo apt-mark unhold linux-image-extra-x.xx.x-xx-generic


 

你可能感兴趣的:(linux)