Skip to content

Instantly share code, notes, and snippets.

@zrruziev
Last active January 25, 2023 14:43
Show Gist options
  • Save zrruziev/b6ecb011953cf2d93f923bc0f6a06261 to your computer and use it in GitHub Desktop.
Save zrruziev/b6ecb011953cf2d93f923bc0f6a06261 to your computer and use it in GitHub Desktop.

Install the specific linux-kernel-version that you want

Check the currently installed kernel version on terminal by running the command

uname -r

Check the available kernel versions to install:

apt-cache search linux-image- | grep generic

This command will search all the packages available in the package manager's cache that match the string "linux-image-" and pipe the output to grep, which filters out any packages that contain the string "generic" in their name.


You can also use the following command to see the available versions in the repository:

apt-cache policy linux-image-generic

To see a list of all the available kernel versions on your system, you can run the command

dpkg --list | grep linux-image

To list the kernel versions that are installed and available to boot, you can use the command

dpkg --list | grep linux-image | awk '{ print $2 }'

Once you have determined the version of the kernel that you want to downgrade to,
you can use the following command to install it:

sudo apt-get install linux-image-<version>-generic

Where "version" is the version number of the kernel that you want to install, and "generic" is the type of kernel. After installing the kernel, you will need to reboot your system for the changes to take effect.


To change the kernel version that your Linux system is booting to

We will use the grub bootloader to do this:

  • Open a terminal window and run the following command. This will open the GRUB configuration file in the nano text editor.
    sudo nano /etc/default/grub

  • In the GRUB configuration file make the following changes:

    GRUB_TIMEOUT_STYLE=menu  # it was "hidden" by default
    GRUB_TIMEOUT=10          # it was "0" by default 

    The above changes make grub menu to be shown automatically(you don't need to press ESC or Shift keys) while booting and it waits 10 secs


    Then, update the GRUB bootloader configuration:

    sudo update-grub

    When you run the command sudo update-grub, it scans the system for installed operating systems and kernels, generates a new GRUB configuration file and updates the menu entries in the GRUB bootloader. This ensures that the boot menu shows the correct options and that the system can boot to the correct operating system or kernel version.


    Then reboot the system:

    sudo reboot

    While rebooting, grub menu appears on the screen automatically.
    Press Enter on **Advanced options for Ubuntu section, and then choose desired kernel version you want to boot. After that, your system will be loaded with the chosen kernel, and if you run uname -r it shows selected kernel version that was chosen while rebooting.
    Great, That's done!!!...

Extra:

  • You can make the specific kernel version default while booting. If the kernel version you want to make default is already running on your system (as shown by the output of uname -r) and you would like to prevent it from being upgraded when using sudo apt update && sudo apt upgrade, you can use the apt-mark command to mark the package as hold:

    sudo apt-mark hold linux-image-<version>-generic

    You can make sure it was held by running this:

    apt-mark showhold

    This will prevent the package from being automatically upgraded or removed. To make it default during boot, you can use sudo update-grub command to update the grub bootloader configuration file and set the desired kernel as the default.

    sudo update-grub

    To unhold:

    sudo apt-mark unhold linux-image-<version>-generic

    If the kernel version you want to make default is not running on your system (as shown by the output of uname -r) then read this gist from the start...

Removing kernels

You can remove older kernels that are not in use by running the following command

sudo apt-get purge <kernel-version>

This will free up disk space and make sure that the current kernel version is the only one available to boot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment