In this lesson,
you're going to learn about GRUB 2,
which is a popular boot loader in Linux.
We've previously discussed
the various essential components
that make up the Linux boot process.
Your existing knowledge of those concepts
are going to help you leverage boot options
as well as gain control over a how Linux boots.
So now that you're familiar with the role of a boot loader
inside the boot process,
you can now be able to learn
how to configure the primary Linux boot loader,
which at the time of this recording is known as GRUB 2.
Understanding how to configure GRUB and GRUB 2
is really important for you to be able to recover from
or to circumvent various types of problems and issues
you may encounter as a system administrator.
There are several boot loaders out there,
but Linux usually uses GRUB,
which is the GNU GRand Unified Bootloader.
This is by far the most popular boot loader out there.
And there's various versions of it,
known as GRUB and GRUB 2.
GRUB enables users to choose which operating system
or kernel version they want to boot
inside this multi-platform environment.
Although the original version of GRUB
was the primary boot loader for Linux distributions,
it had several limitations,
and eventually, it was phased out
in favor of the new version of GRUB known as GRUB 2.
Now, GRUB 2 is usually just called GRUB
and it's more than just a simple newer version
of GRUB, though.
It's also a completely redesigned and rewritten version
of the GRUB system.
GRUB 2 offers administrators
with more control over the boot process,
the boot devices, and the boot behavior.
GRUB 2 supports non-86 architecture platforms,
things like RISC processors and ARM processors,
as well as supports booting an OS from storage media
and running the OS entirely in memory.
You can do this
without even installing the Linux operating system
and run the entire thing from RAM.
That's one of the things that GRUB 2 offers.
It also supports partition UUIDs
and supports loading modules
to extend GRUB's functionality.
In addition to all that, GRUB 2 also has the ability
to configure the boot loader
through the use of scripts.
And it will attempt to fix boot issues
like corrupted and missing configurations,
as well as having other features
to customize your user experience.
This includes things like background wallpapers,
different font, and more things like that
to personalize your system.
Because of these improvements,
GRUB 2 has become the default boot loader
on almost all modern Linux distributions.
GRUB 2 can also be installed
even if you're currently booting into the OS.
Just execute the grub2-install command
against the device or the partition you desire.
It'll copy over the GRUB 2 files
into the /boot/GRUB2 directory,
and on some platforms,
it'll install GRUB 2 into the boot sector.
However, grub2-install applies to BIOS-based systems,
not UEFI systems.
If you want to install GRUB 2 on a UEFI system,
you're going to have to use a package manager
to install the grub2-efi package.
Installing this package will copy GRUB 2 files
onto the EFI system partition or the ESP
inside of the /boot/efi directory.
Now, to use grub2-install,
you have to use the right syntax,
and the syntax for this is grub2-install, the options,
and the device name.
Now, there's several different options you can use
with the grub2-install command.
For example, you can use --modules in the module names.
This option will allow you
to reload the specified kernel modules
with the GRUB 2 boot loader.
If you use the --install-modules and module names option,
this is going to be used to install only the specified modules
and their dependencies
rather than the default of installing all available modules.
If you use --directory and the directory name option,
this is going to be used to install files
from the specified directory
rather than from the default directory.
If you use --target and the target platform option,
this is going to be used to specify the target platform
that you want to install GRUB 2 for
instead of using the platform
that you're currently running on.
The --boot-directory, directory name option
is going to be used to specify the boot directory
that you want to install GRUB 2 files to
rather than using the default /bootdirectory or /boot/GRUB2.
The -force option is going to be used to install GRUB 2
regardless of any detected issues.
Now, the main configuration file
for your GRUB 2 boot loader is known as grub.cfg.
This is a file that you're going to use on BIOS systems,
and it's going to be located in /boot/grub2.
If you're on a UEFI system,
it's going to be located in /boot/efi/EFI
and then you're going to have your file there.
Now, this file is an executable shell script
that is not recommended to be edited directly,
because it's generated using specific commands
that leverage configuration scripts stored elsewhere
on the file system.
Another important directory is the /etc/grub.d directory.
This contains scripts
that are used to build the main grub.config file.
Each script provides various functions to GRUB 2,
and it's numbered so that the scripts can execute
in a sequence.
It's usually not a good idea to edit the existing scripts
in this directory.
If you want to add a custom script,
you should place this in the directory
with a number number underscore as a file name prefixed,
depending on the order you want the script to be executed in
within your current directory.
You could also add your script
to the existing 40_custom file,
because that way, it will execute last by default.
Now, speaking of this 40_custom file,
this is going to be located
at /etc/grub.d/40_custom.
And this file enables the customization of the menu
that's presented to the user
during the boot process.
The GRUB 2 system is going to offer the user a menu
of installed operating systems to choose from.
This choice is really useful
if you're multi booting your system
and you have more than one operating system
on the computer.
For example,
you might have different Linux kernels there,
or you have Linux and Windows on the same system,
or you might want to boot into rescue mode.
The menu contents here can be customized
by editing the /etc/grub.d/40_custom file
and enabling an administrator
to specify the order of the menu choices,
provide user friendly names,
and password protect different menu entries.
You can also generate a password hash
to protect your boot menu
by using grub2-mkpasswd
pbkdf2 at the command line.
The /etc/default/grub file
is going to contain the GRUB 2 display menu settings
that are read by the /etc/grub.d scripts.
And they're going to be built in to your grub.config file.
It's going to enable you to change options
such as how many seconds the GRUB 2 loader is going to wait
before it automatically selects the default boot option,
whether or not the GRUB 2
will actually order kernel versions
in a sub menu
or whether or not GRUB 2 will display the menu
in a graphical terminal
and many other options.
The grub2-mkconfig command or the make config command
will generate a new GRUB configuration file
or update the existing GRUB configuration file.
This command is going to combine
the configuration file templates
inside of the /etc/grub.d directory
with the settings in the /etc/default/grub,
as well as to generate the grub.cfg configuration file.
To run this, type grub2-mkconfig-o and the file name.
If you simply need to update your GRUB 2 configuration file
instead of making a new one,
you can use the update-grub2 command too.