How to disable grub menu at boot time in Ubuntu 20.04?

Solution 1:

Solution / tl;dr

echo 'set timeout_style=hidden
set timeout=0' | sudo tee /boot/grub/custom.cfg

Explanation

Though UnKNOWn's approach does indeed work, the modification of files installed by the base system should be avoided, if possible. In this case (i. e. Ubuntu 20.04), apt-file shows that /etc/grub.d/30_os-prober belongs to the package grub-common (this is also true for all files located in /etc/grub.d/, which are used by the update-grub command to compile the final GRUB configuration file):

Output of "apt-file search /etc/grub.d/30_os-prober"

If the package grub-common gets updated in the future, apt would detect the change and prompt you to decide what it should do (as both options - the file modified by you as well as the unmodified file from the distributor - are valid). In the worst-case scenario you would have to compare and merge the file contents manually.

So, what are our options if we want to hide the GRUB menu in a multiboot environment? For once, the grub-common package only wants to make sure that each and every user can switch between the installed operating systems. Because of that, the script /etc/grub.d/30_os-prober contains the following code snippet:

adjust_timeout () {
  if [ "$quick_boot" = 1 ] && [ "x${found_other_os}" != "x" ]; then
    cat << EOF
set timeout_style=menu
if [ "\${timeout}" = 0 ]; then
  set timeout=10
fi
EOF
  fi
}

This script (which is evaluated once the update-grub command gets executed) basically allows you to set a custom timeout even for multiboot environments - but it will not allow a timeout of 0, as novice users would perhaps not know how to boot their other operating systems without an obvious menu. This means that you could set GRUB_TIMEOUT to 1, and so the GRUB menu would show up for just a second before booting the default boot option.

But how can we hide the GRUB menu for good without touching files installed by system packages? Luckily, the grub-common package provides a facility to apply one-off settings. Have a look at the last file evaluated by the update-grub command, /etc/grub.d/41_custom:

#!/bin/sh
cat <<EOF
if [ -f  \${config_directory}/custom.cfg ]; then
  source \${config_directory}/custom.cfg
elif [ -z "\${config_directory}" -a -f  \$prefix/custom.cfg ]; then
  source \$prefix/custom.cfg;
fi
EOF

OK, so a file named custom.cfg, which must be placed in the same location as the actual GRUB configuration file (/boot/grub/grub.cfg), gets sourced every time the GRUB configuration file gets evaluated (i. e. at boot time). So let us add our desired settings to this file (/boot/grub/custom.cfg):

GRUB will now hide the menu and instead boot the default boot option immediately. Other operating systems can still be booted by using the UEFI.

Solution 2:

Tested with Dual boot of 2 Ubuntu 20.04 OS'es

Edit the below line in the file /etc/default/grub file

GRUB_TIMEOUT=10

like this

GRUB_TIMEOUT=0

and run the below command

sudo update-grub

this makes the timeout to 0 with condition that if there is Single OS it will stick to 0 otherwise it will be overwrite to 10.

In the /etc/grub.d/30_os-prober file, change the line

quick_boot="1"

to

quick_boot="0"

This will prevent in multi OS'es cases from changing the timeout value to 10seconds..

see the function "adjust_timeout" from the file /etc/grub.d/30_os-prober

prefix="/usr"
exec_prefix="/usr"
datarootdir="/usr/share"
quick_boot="1"

export TEXTDOMAIN=grub
export TEXTDOMAINDIR="${datarootdir}/locale"

. "$pkgdatadir/grub-mkconfig_lib"

found_other_os=

adjust_timeout () {
  if [ "$quick_boot" = 1 ] && [ "x${found_other_os}" != "x" ]; then
    cat << EOF
set timeout_style=menu
if [ "\${timeout}" = 0 ]; then
  set timeout=10
fi
EOF
  fi
}

When you want to see grub menu, pressing the Esc key at rite time will show you. (rite time varies based on conditions, generally after the BIOS menu hand overs to grub menu time)

In case if you wish to change Default OS, you can set from the same file /etc/default/grub.

for the first time, get to the grub menu by hitting Esc at the rite time. Then count the number starting from 0 from top to list your favorite OS Entry..

For example, I have two 20.04 OS'es.. one in /dev/sda2 and other in /dev/sda6 /dev/sda2 is the default one. If I want to make the one in /dev/sda6 as the default OS, I need to find its number like below..

enter image description here

So, its position is at number 2.

Edit the file /etc/default/grub and change the line

GRUB_DEFAULT=0

to

GRUB_DEFAULT=2

then run the below command

sudo update-grub

System will boot straight to Ubuntu 20.04 on /dev/sda6