By default, GRUB allows users to edit boot parameters during startup. This can be a security risk since anyone with access to the system can modify boot settings (e.g., boot into single-user mode and change the root password).
To prevent unauthorized access, we can secure GRUB with a password.
โ Works for: Ubuntu, Debian, CentOS, RHEL, Fedora, Rocky Linux, AlmaLinux
Instead of storing a plain-text password, we use an encrypted password.
Run the following command:
grub-mkpasswd-pbkdf2๐ Example Output:
Enter password: ********
Reenter password: ********
PBKDF2 hash of your password is grub.pbkdf2.sha512.10000.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
๐น Copy the generated hash (everything after PBKDF2 hash of your password is).
Now, we need to store the password in the GRUB configuration.
sudo nano /etc/grub.d/40_customset superusers="admin"
password_pbkdf2 admin grub.pbkdf2.sha512.10000.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX๐น Replace admin with your desired username.
๐น Replace grub.pbkdf2.sha512... with the hash generated earlier.
โ
Save and exit: CTRL + X โ Y โ ENTER
Now, prevent normal users from modifying boot settings.
sudo nano /etc/grub.d/10_linuxFind the echo "menuentry ..." section and modify it like this:
echo "menuentry 'Ubuntu' --users admin {"๐ This ensures only admin can edit GRUB entries.
โ
Save and exit: CTRL + X โ Y โ ENTER
After making modifications, update the GRUB configuration:
sudo grub-mkconfig -o /boot/grub/grub.cfg๐ On UEFI Systems:
sudo grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg # RHEL/CentOSโ Now, GRUB is protected!
1๏ธโฃ Reboot the system:
sudo reboot2๏ธโฃ Press e to edit a boot entry โ You should now be prompted for a username and password.
3๏ธโฃ Enter the admin username and password.
โ Success! Unauthorized users can no longer modify boot parameters.
Even with a GRUB password, users can boot into single-user mode and change the root password.
To prevent this:
sudo nano /etc/sysconfig/initChange:
SINGLE=/sbin/suloginsudo nano /etc/inittabAdd:
~:S:wait:/sbin/sulogin
โ Now, single-user mode requires the root password!
If you forget the GRUB password, follow these steps:
1๏ธโฃ Boot from a Live USB
2๏ธโฃ Mount the root partition
sudo mount /dev/sda2 /mnt
sudo chroot /mnt3๏ธโฃ Remove the GRUB Password
nano /etc/grub.d/40_custom๐ Delete the password_pbkdf2 line, save, and exit.
4๏ธโฃ Regenerate GRUB Configuration
sudo grub-mkconfig -o /boot/grub/grub.cfg5๏ธโฃ Reboot the System
โ Now GRUB password protection is removed!
| Step | Command | Purpose |
|---|---|---|
| Generate Password Hash | grub-mkpasswd-pbkdf2 |
Creates a secure GRUB password |
| Edit GRUB Config | sudo nano /etc/grub.d/40_custom |
Adds password to GRUB |
| Restrict Menu Access | sudo nano /etc/grub.d/10_linux |
Requires authentication for boot options |
| Update GRUB | sudo grub-mkconfig -o /boot/grub/grub.cfg |
Applies the password protection |
| Secure Single-User Mode | sudo nano /etc/sysconfig/init |
Prevents unauthorized root password resets |
| Recover Forgotten Password | Boot Live USB โ Edit /etc/grub.d/40_custom |
Remove password if lost |