To set a password for GRUB, you need to go through three simple steps. First we generate a password hash, then we need to set it by changing the settings, and at the end we need to update the GRUB configuration. At the same time, I'll tell you how to enable the system to boot without a password, and check if everything works correctly.
Generate Password hash
To generate a password hash, we need the grub-mkpasswd-pbkdf2 utility. It is installed by default in many distributions.
To generate a password hash, just run the command in the terminal:
Then enter the required password for GRUB twice.
Copy the resulting hash and save it somewhere, we still need it. The hash starts with grub.pbkdf2.
If your distribution doesn't have grub-mkpasswd-pbkdf2. This means that the grub-common package is missing from the system. Therefore, it must be installed manually.
If you are using Ubuntu, then install the package with one command:
sudo apt install grub-common
Change GRUB settings
Now you need to assign a super user for GRUB. To do this, we need to edit the following file: /etc/grub.d/00_header.
Open the file with the nano text editor:
sudo nano /etc/grub.d/00_header
Scroll to the bottom of the content. And then on a new line, paste the following information:
cat << EOF set superusers="userName" password_pbkdf2 userName grub.pbkdf2.sha512.10000.51C2A3F4768C5317AD6CC22D0CDAA9B29968B7B5F34AF0E479785A973002855E4F62D1A7AE4204B7341A04B913B690D00ABD9CCA4A086337E9457635E5D6B307.18D9DA0C1784F464717A437E28C4E7776E2A7C97A0CBFE44D0A78EB5392CC452A653C3729B432D255D78AEA31E0BAC906E5F157204C434B21D3DED104A53C368 EOF
The main thing! Replace userName on the second and third lines with your preferred GRUB superuser name, and replace the password hash with your own.
Update GRUB configuration
Now let's update the GRUB configurations to apply the previously made changes.
To do this, enter a simple command:
Restart your computer to check your changes. As soon as the GRUB interface appears, you will be prompted for a username and then a password.
Setting a password for GRUB will be very useful in many situations. This will help make your system more secure, as the regular superuser password is very easy to reset.