Skip to content

Instantly share code, notes, and snippets.

@chaiyujin
Created December 8, 2020 12:42
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save chaiyujin/c08e59752c3e238ff3b1a5098322b363 to your computer and use it in GitHub Desktop.
Save chaiyujin/c08e59752c3e238ff3b1a5098322b363 to your computer and use it in GitHub Desktop.
Ubuntu: Install Kernel and Set GRUB Default Kernel

Ubuntu: Install Kernel and Set GRUB Default Kernel

Install Kernel

Install the default kernel:

sudo apt install linux-generic

Set GRUB Default Kernel

  1. Find entrance from /boot/grub/grub.cfg
    • Get the $menuentry_id_option:
      grep submenu /boot/grub/grub.cfg
      Example output:
      submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-4591a659-55e2-4bec-8dbe-d98bd9e489cf' {
      'gnulinux-advanced-4591a659-55e2-4bec-8dbe-d98bd9e489cf' is what we need.
    • Get the specific kernel option:
      grep gnulinux-4.15.0 /boot/grub/grub.cfg
      Example output:
      menuentry 'Ubuntu, with Linux 4.15.0-126-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-126-generic-advanced-4591a659-55e2-4bec-8dbe-d98bd9e489cf' {
      'gnulinux-4.15.0-126-generic-advanced-4591a659-55e2-4bec-8dbe-d98bd9e489cf' is what we need.
  2. Set GRUB_DEFAULT in /etc/default/grub
    • Join two entry strings obtained above by '>', set to GRUB_DEFAULT.
      GRUB_DEFAULT='gnulinux-advanced-4591a659-55e2-4bec-8dbe-d98bd9e489cf>gnulinux-4.15.0-126-generic-advanced-4591a659-55e2-4bec-8dbe-d98bd9e489cf'
  3. Update grub
    sudo update-grub
  4. Reboot the machine
@dreamerboy0406
Copy link

I have tried following this steps but update-grub isn't updating the grub file.
what am I missing?

@Abir-Tx
Copy link

Abir-Tx commented Dec 26, 2022

Nice instructions. Thanks

@gorsaribekyan
Copy link

Thanks.

@boypt
Copy link

boypt commented Mar 3, 2023

Let scripts do the work:

KERNELVER=5.15.0-58-generic
MID=$(awk '/Advanced options for Ubuntu/{print $(NF-1)}' /boot/grub/grub.cfg | cut -d\' -f2)
KID=$(awk "/with Linux $KERNELVER/"'{print $(NF-1)}' /boot/grub/grub.cfg | cut -d\' -f2 | head -n1)

# echo "GRUB_DEFAULT='${MID}>${KID}'" >> /etc/default/grub

# or prefer put it in a seprate file
cat > /etc/default/grub.d/95-savedef.cfg <<EOF
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true
EOF
grub-editenv /boot/grub/grubenv set saved_entry="${MID}>${KID}"
update-grub

And here's to UNDO the changes

# UNDO  changes ?
grub-editenv /boot/grub/grubenv unset saved_entry
rm -f /etc/default/grub.d/95-savedef.cfg
update-grub

@douglasss
Copy link

After that I can remove the old kernel files?

@LowLatencyMatters
Copy link

After that I can remove the old kernel files?

Sure, after successfully booted into particular version and supposing everything went well, you may delete the old kernels, headers and other related packages.

@korantu
Copy link

korantu commented May 2, 2023

Works as advertised, just make sure to put quotes:

   GRUB_DEFAULT='gnulinux-advanced-4591a659-55e2-4bec-8dbe-d98bd9e489cf>gnulinux-4.15.0-126-generic-advanced-4591a659-55e2-4bec-8dbe-d98bd9e489cf'

@janpfeifer
Copy link

Doesn't seem to work here, doesn't matter how I try it ... My ids look like:

gnulinux-6.2.0-20-generic-advanced-7ceb1b1a-7139-409e-aff6-4e6ea53ce07b

and

gnulinux-5.17.5-051705-generic-recovery-7ceb1b1a-7139-409e-aff6-4e6ea53ce07b

The automatic option it creates is always for the first one, while I want the deafult to be 5.17.5...

@umer-practiceprovider
Copy link

Worked for me thanks 👍

@ronech
Copy link

ronech commented Sep 27, 2023

thanks for the script @boypt

@agail
Copy link

agail commented Jan 14, 2024

Came here looking for the exact same thing, kernel 6.5 seems to be having problems with nvida gpu (390).
I made a function (put it in your .bash_aliases file or create a .bash_functions that you source at login)

kernel-default () {
 local _action=$1
 local _kernelver=$2
 if [[ ! "${_action}" =~ ^(set)$ || ! "${_kernelver}" ]] && [[ ! "${_action}" =~ ^(unset)$ ]]; then
    echo -e "\n\e[90m kernel-default <set|unset> (default list)\e[m"
     echo -e "\e[91m\n  No \e[4maction\e[;91m or \e[4;91mkernel\e[;91m ver defined, issue \e[mkernel-default set \$(uname -r)\e[91m as example,"
     echo -e "  or use any of the following existing:\e[m $(kernel-mgmt | awk -F "[-]" '/image/ {print $3 "-" $4}' | sed '$!N;s/\n/, /g')"
     echo -e "\n\e[94m    Example:\e[m kernel-default set $(uname -r | cut -d '-' -f1-2)\e[m\n"

     local _savedcustom=$(grub-editenv list | cut -d '>' -f2)
     if [ "${_savedcustom}" ]; then
         echo -e "\e[90m Default kernel override active: \e[m${_savedcustom}\n"
     else
         echo -e "\e[90m Default kernel override active: \e[mnone\n"
     fi
 elif [ "${_action}" == "set" ]; then
    local _mid=$(awk '/Advanced options for Ubuntu/{print $(NF-1)}' /boot/grub/grub.cfg | cut -d\' -f2)
    local _kid=$(awk "/with Linux $_kernelver/"'{print $(NF-1)}' /boot/grub/grub.cfg | cut -d\' -f2 | head -n1)
    echo -e "GRUB_DEFAULT=saved\nGRUB_SAVEDEFAULT=true" | sudo tee /etc/grub.d/95-savedef.cfg
    sudo chmod +x /etc/grub.d/95-savedef.cfg
    sudo grub-editenv /boot/grub/grubenv set saved_entry="${_mid}>${_kid}"
    sudo update-grub
 elif [ "${_action}" == "unset" ]; then
    sudo grub-editenv /boot/grub/grubenv unset saved_entry
    sudo rm -f /etc/grub.d/95-savedef.cfg
    sudo update-grub
 fi
}

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