How to use the command "grub-set-default" (with examples)

How to use the command "grub-set-default" (with examples)

The grub-set-default command is a useful tool for setting the default boot entry for the GRUB bootloader. This allows you to specify which operating system or kernel should be automatically selected and booted when your system starts up.

In this article, we will explore different use cases of the grub-set-default command and provide code examples to illustrate each case. By understanding how to use this command, you can easily customize your boot process and streamline your system’s startup.

Use Case 1: Setting the Default Boot Entry by Entry Number

Code:

sudo grub-set-default entry_number

Motivation:

This use case is helpful when you know the specific entry number of the boot option that you want to set as the default. This could be useful, for example, if you have multiple operating systems installed and want to choose a particular one to boot by default.

Explanation:

  • sudo: This command is used to run the following command with administrative privileges.
  • grub-set-default: This is the main command that sets the default boot entry for GRUB.
  • entry_number: Replace this with the specific entry number of the boot option that you want to set as the default. The entry number is based on the order of the boot options listed in your GRUB configuration file.

Example Output:

Let’s say you have three boot options listed in your GRUB configuration file, with entry numbers 0, 1, and 2. If you want to set the second boot option as the default, you would use the following command:

sudo grub-set-default 1

After running this command and restarting your system, the second boot option will be automatically selected and booted.

Use Case 2: Setting the Default Boot Entry by Name or Identifier

Code:

sudo grub-set-default "entry_name_or_identifier"

Motivation:

If you have a boot option that has a unique name or identifier, it can be more convenient to set the default boot entry using this information rather than remembering the entry number.

Explanation:

  • entry_name_or_identifier: Replace this with the specific name or identifier of the boot option that you want to set as the default. This should match the name or identifier listed in your GRUB configuration file.

Example Output:

Suppose you have a boot option with the name “Ubuntu” in your GRUB configuration file. To set this boot option as the default, you would use the following command:

sudo grub-set-default "Ubuntu"

After running this command and restarting your system, the “Ubuntu” boot option will be automatically selected and booted.

Use Case 3: Setting the Default Boot Entry for an Alternative Boot Directory

Code:

sudo grub-set-default --boot-directory /path/to/boot_directory entry_number

Motivation:

Sometimes, you may have an alternative boot directory where your GRUB configuration file resides. This use case allows you to specify this directory path and set the default boot entry accordingly.

Explanation:

  • --boot-directory /path/to/boot_directory: By including this option and specifying the path to your alternative boot directory, you can tell grub-set-default to use the GRUB configuration file in that directory instead of the default location.
  • entry_number: Replace this with the specific entry number of the boot option that you want to set as the default. The entry number is based on the order of the boot options listed in the GRUB configuration file.

Example Output:

Assuming your alternative boot directory is located at /mnt/boot and contains a GRUB configuration file, and you want to set the third boot option in that file as the default, you would use the following command:

sudo grub-set-default --boot-directory /mnt/boot 2

After running this command and restarting your system, the third boot option in the alternative boot directory’s GRUB configuration file will be automatically selected and booted.

Conclusion

In this article, we explored different use cases of the grub-set-default command and provided code examples for each case. By utilizing this command, you can easily set the default boot entry to meet your specific needs, whether it be by entry number, name or identifier, or for an alternative boot directory. Understanding how to customize the default boot selection can help streamline your system’s startup process and make it more tailored to your preferences.

Related Posts

How to use the command 'dmesg' (with examples)

How to use the command 'dmesg' (with examples)

The ‘dmesg’ command in Linux is used to display kernel messages.

Read More
How to use the command screenfetch (with examples)

How to use the command screenfetch (with examples)

Screenfetch is a command-line utility that displays system information in the terminal.

Read More
How to use the command "gotty" (with examples)

How to use the command "gotty" (with examples)

The “gotty” command allows you to share your terminal as a web application.

Read More