This article uses Ubuntu 18.04.4 as an example. The kernel version is 4.15.0-101-generic, and the kernel version is 4.15.0-76-generic.

Note: If you want to upgrade the kernel by compiling the kernel source code, refer to this article

Current system version:

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
Copy the code

1. View the installed kernel image of the current system

Root @ zyg: ~ # DPKG -- get - selections | grep Linux - image Linux - image - 4.15.0-101 - generic install Linux - image - generic installCopy the code

2. Query the Linux image package of the specified version

The kernel 4.15.0-76 is used as an example.

Root @ zyg: ~ # apt - cache search Linux | grep Linux - buildinfo - 4.15.0 4.15.0-76-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-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 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 Modules for version 4.15.0-76Copy the code

Note:

(1), if the query is less than, can be in the/etc/apt/sources list configuration, to join the source: deb security.ubuntu.com/ubuntu trusty ws-security main

②, it can also be found at kernel.ubuntu.com/~kernel-ppa… Find the deb package you need at the web site.

3, install the Linux system header file and image of the specified kernel version found above:

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 - Linux - modules - 4.15.0 4.15.0-76-76 - generic Suggested packages: Fdutils Linux - doc - 4.15.0 | Linux - source - 4.15.0 Linux - The 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 Upgrade, 4 newly installed, 0 to remove and 45 not upgrade. 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 - all 4.15.0 4.15.0-76-76.86 [10.9 MB] Get: 1 http://cn.archive.ubuntu.com/ubuntu bionic - updates/main amd64 Linux - headers - all 4.15.0 4.15.0-76-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 Comics-updates /main AMd64 linux-image-4.15.0-76-generic AMd64 4.15.0-76.86 [7993 kB] Comics-updates /main amd64 linux-image-4.15.0-76- Generic AMd64 4.15.0-76.86 [7993 kB] Touch29.1 MB in 8min 56s 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: /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 the/boot/initrd. Img - 4.15.0-76 - generic/etc/kernel/the 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: The/boot/called - 4.15.0-101 - generic Found the 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/vmlinuz-4.15.0-76-generic Found initrd image: The/boot/initrd. Img - 4.15.0-76 - generic doneCopy the code

4. View the kernel boot sequence of the current system

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 - c32 b753ddfd - 2731-4 - aa02-9 a654abc99c6' {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, $menuentry_id_option 'gnulinux-4.15.0-101-generic-recovery-b753DDfd-2731-4c32-aa02-9a654abc99c6' 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 - c32 b753ddfd - 2731-4 - aa02-9 a654abc99c6' {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 - c32 b753ddfd - 2731-4 - aa02-9 a654abc99c6' {Copy the code

5. Change the kernel boot sequence

If you upgrade to a higher version than the current kernel, the new kernel is installed first by default and you need to restart the system. Otherwise, you need to modify the configuration file as follows:

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 ...) Xfefefefe x01234567 # GRUB_BADRAM = "0, 0, 0 x89abcdef, 0 xefefefef" # 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"Copy the code

Change GRUB_DEFAULT=0 to GRUB_DEFAULT=”Advanced options for Ubuntu>Ubuntu, with Linux 4.15.0-76-generic”, save the Settings and exit.

GRUB_DEFAULT: GRUB_DEFAULT: GRUB_DEFAULT: GRUB_DEFAULT: GRUB_DEFAULT Advanced options for Ubuntu>Ubuntu, with Linux x.x.x-x-generic form.

6. The configuration takes effect

If the configuration file has been modified, run the update-grub command to update the GRUB configuration.

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/vmlinuz-4.15.0-76-generic Found initrd image: The/boot/initrd. Img - 4.15.0-76 - generic doneCopy the code

Then, reboot and verify that the reboot takes effect.

As you can see, the kernel is already 4.15.0-76-generic.

Remove unused kernels

Query all of the other kernel version that does not include the current kernel version: 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 genericCopy the code

The output may include the following three states of the kernel image: RC: indicates that the kernel image has been removed ii: indicates that the kernel image meets the conditions for removal (removable) iU: indicates that the kernel image has entered the APT installation queue but has not been installed (cannot be removed).

My kernel 4.15.0-101.102 is removable, you can run the following command to delete it:

DPKG -- purge Linux - image - 4.4.0-21 - genericCopy the code

7. Turn off automatic kernel or package updates

1. Modify the system configurations and change 1 to 0 in 10Periodic and 20auto-upgrades under the /etc/apt/apt.conf.d directory.

Apt-mark hold linux-image-extra-x.xx.x-xx-generic apt-mark hold linux-image-extra-x.xx.x-xx-generic

To remove the above Settings, take the parameter unhold: sudo apt-mark unhold linux-image-x.xx.x-xx-generic sudo apt-mark unhold linux-image-extra-x.xx.x-xx-generic

In this paper, to the end.

Welcome to follow my wechat public account big fat chat programming, you can contact me through the public account, you can also add friends to exchange and learn together.