正文:
步骤一:手动设置 GRUB2 Bootloader 密码 (第 1 种方法)
(注意:步骤一和步骤二只用完成其一就行)
1.1 生成密码的 GRUB2 密码的 SHA512 值
# grub2-mkpasswd-pbkdf2
Enter password:
Reenter password:
PBKDF2 hash of your password is
grub.pbkdf2.sha512.10000.
B857B79D02FF55CA3A69B8485C1A4A427424630C804CC8A89134520A34E056D7882A778F6BC8AD856CB95DF8B99BE25F9FEAD899D826BB3915FB20BAD682D10A.4A1CF49B7F815A0578031CCA2CE98C66BDFBEAB0AE2721531BB54AFC6CFFA990FBD9062F41C006376C283B717FEC1BD9BB1AFB882AF91B5F4A812459D7974D1F
(补充:这里以生成 eternalcenter 的 GRUB2 SHA512 值为例)
(注意:grub.pbkdf2.sha512.10000……. 后面的一长串字母和数字其实是 1 行,这里因为是显示问题所以看上去是多行)
1.2 创建 GRUB2 密码文件
如果是 EFI 的 CentOS & RHEL:
# echo "GRUB2_PASSWORD=grub.pbkdf2.sha512.10000.B857B79D02FF55CA3A69B8485C1A4A427424630C804CC8A89134520A34E056D7882A778F6BC8AD856CB95DF8B99BE25F9FEAD899D826BB3915FB20BAD682D10A.4A1CF49B7F815A0578031CCA2CE98C66BDFBEAB0AE2721531BB54AFC6CFFA990FBD9062F41C006376C283B717FEC1BD9BB1AFB882AF91B5F4A812459D7974D1F" > /boot/efi/EFI/redhat/user.cfg
(
补充:
1) 登录用户 root
2) 登录密码 eternalcenter
3) 这里的 grub.pbkdf2.sha512.10000.B857B…… 是由步骤一生成的
)
(注意:这里其实只有 1 行,因为是显示问题所以看上去是多行)
如果是 BIOS 的 CentOS & RHEL:
# echo "GRUB2_PASSWORD=grub.pbkdf2.sha512.10000.B857B79D02FF55CA3A69B8485C1A4A427424630C804CC8A89134520A34E056D7882A778F6BC8AD856CB95DF8B99BE25F9FEAD899D826BB3915FB20BAD682D10A.4A1CF49B7F815A0578031CCA2CE98C66BDFBEAB0AE2721531BB54AFC6CFFA990FBD9062F41C006376C283B717FEC1BD9BB1AFB882AF91B5F4A812459D7974D1F" > /boot/grub2/user.cfg
(
补充:
1) 登录用户 root
2) 登录密码 eternalcenter
3) 这里的 grub.pbkdf2.sha512.10000.B857B…… 是由步骤一生成的
)
(注意:这里其实只有 1 行,因为是显示问题所以看上去是多行)
步骤二:通过命令 grub2-setpassword 设置 GRUB2 Bootloader 密码 (第二种方法)
(注意:步骤一和步骤二只用完成其一就行)
2.1 通过命令 grub2-setpassword 设置 GRUB2 Bootloader 密码
# grub2-setpassword
Enter password: <password>
Confirm password: <password>
Run the following command to update the grub2 configuration:
2.2 重新配置 /boot/grub2/grub.cfg 文件
# grub2-mkconfig -o /boot/grub2/grub.cfg
步骤三:测试 GRUB2 Bootloader 密码
3.1 重启系统
(步骤略)
3.2 进入到 GRUB2 Bootloader 模式
当启动系统时按下 “E” 键
补充:CentOS & RHEL 更改 GRUB2 的登录用户
补充一:手动设置登录 GRUB2 Bootloader 的用户
如果是 EFI 的 CentOS & RHEL:
# vim /boot/efi/EFI/redhat/grub.cfg
将以下内容:
### BEGIN /etc/grub.d/01_users ###
if [ -f ${prefix}/user.cfg ]; then
source ${prefix}/user.cfg
if [ -n "${GRUB2_PASSWORD}" ]; then
set superusers="root"
export superusers
password_pbkdf2 root ${GRUB2_PASSWORD}
fi
fi
### END /etc/grub.d/01_users ###
修改为:
### BEGIN /etc/grub.d/01_users ###
if [ -f ${prefix}/user.cfg ]; then
source ${prefix}/user.cfg
if [ -n "${GRUB2_PASSWORD}" ]; then
set superusers="eternalcenter"
export superusers
password_pbkdf2 root ${GRUB2_PASSWORD}
fi
fi
### END /etc/grub.d/01_users ###
(补充:这里以将登陆用户设置为 eternalcenter 为例)
如果是 BIOS 的 CentOS & RHEL:
# vim /boot/grub2/grub.cfg
将以下内容:
### BEGIN /etc/grub.d/01_users ###
if [ -f ${prefix}/user.cfg ]; then
source ${prefix}/user.cfg
if [ -n "${GRUB2_PASSWORD}" ]; then
set superusers="root"
export superusers
password_pbkdf2 root ${GRUB2_PASSWORD}
fi
fi
### END /etc/grub.d/01_users ###
修改为:
### BEGIN /etc/grub.d/01_users ###
if [ -f ${prefix}/user.cfg ]; then
source ${prefix}/user.cfg
if [ -n "${GRUB2_PASSWORD}" ]; then
set superusers="eternalcenter"
export superusers
password_pbkdf2 root ${GRUB2_PASSWORD}
fi
fi
### END /etc/grub.d/01_users ###
(补充:这里以将登陆用户设置为 eternalcenter 为例)
补充二:通过 grub2-editenv 命令设置登录 GRUB2 Bootloader 的用户
# grub2-editenv - set grub_users="root"
(补充:这里以将登录 GRUB2 Bootloader 的用户设置为 root 为例)
参考文献:
https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/8/html/managing_monitoring_and_updating_the_kernel/assembly_protecting-grub-with-a-password_managing-monitoring-and-updating-the-kernel