[root@linux ~]# /sbin/grub-md5-crypt
Password: ******** # 輸入明文密碼,如 Grub@2024
Retype password: ******** # 確認
$1$7uDL20$eSB.XRPG2A2Fv8AeH34nZ0 # 輸出的 MD5 密文,複製備用
注意: GRUB Legacy 僅支持 MD5 (grub-md5-crypt);GRUB2 使用 PBKDF2 (grub-mkpasswd-pbkdf2),兩者不兼容。
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
password --md5 $1$7uDL20$eSB.XRPG2A2Fv8AeH34nZ0
title CentOS 7 (3.10.0-1160.el7.x86_64)
root (hd0,0)
kernel /vmlinuz-3.10.0-1160.el7.x86_64 ro root=/dev/mapper/centos-root
initrd /initramfs-3.10.0-1160.el7.x86_64.img
title Windows 10
rootnoverify (hd0,1)
chainloader +1
效果:直接啟動默認項;按 p 輸入密碼後可進入命令行、編輯參數、選擇其他菜單項。
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
password --md5 $1$7uDL20$eSB.XRPG2A2Fv8AeH34nZ0
title CentOS 7 (3.10.0-1160.el7.x86_64)
lock
root (hd0,0)
kernel /vmlinuz-3.10.0-1160.el7.x86_64 ro root=/dev/mapper/centos-root
initrd /initramfs-3.10.0-1160.el7.x86_64.img
title Windows 10
rootnoverify (hd0,1)
chainloader +1
效果:默認項啟動需密碼;Windows 項無密碼可直接啟動(雙系統場景)。
# 生成第二個密碼
# grub-md5-crypt -> $1$salt2$hash2
default=0
timeout=5
# 全局密碼(超級管理員)
password --md5 $1$7uDL20$eSB.XRPG2A2Fv8AeH34nZ0
title CentOS 7 Production
lock
password --md5 $1$salt2$hash2 # 運行維護密碼
root (hd0,0)
kernel /vmlinuz-prod ro root=...
initrd /initramfs-prod.img
title CentOS 7 Rescue
lock
# 僅超級管理員密碼可進
root (hd0,0)
kernel /vmlinuz-rescue ro root=... single
initrd /initramfs-rescue.img
效果:生產內核可用維護密碼或超級密碼解鎖;救援模式僅超級密碼。
| 指令 | 位置 | 作用 |
|---|---|---|
password --md5 <hash> | 全局/菜單項 | 設置密碼,全局放在 timeout 後,菜單項放在 lock 後 |
lock | 菜單項首行 | 鎖定該項,需密碼解鎖(必須配合 password 使用) |
timeout=N | 全局 | N 秒後自動啟動 default 項;N=0 立即啟動;N=-1 無限等待 |
Grub@2024 → 進入編輯模式lock 的項 → 應提示密碼chown root:root /boot/grub/grub.conf
chmod 600 /boot/grub/grub.conf
# 防止非 root 讀取 MD5 密文(雖然不可逆,但防彩虹表)
# 1. 生成 PBKDF2 密碼
grub-mkpasswd-pbkdf2
# 輸入密碼 -> grub.pbkdf2.sha512.10000.XXXX
# 2. 編輯 /etc/grub.d/40_custom
set superusers="root"
password_pbkdf2 root grub.pbkdf2.sha512.10000.XXXX
# 3. 針對菜單項加鎖 (在 menuentry 內)
menuentry "CentOS 7" --users root --unrestricted {
# --unrestricted 表示無需密碼啟動
# 去掉 --unrestricted 則需密碼
}
# 4. 重新生成配置
grub2-mkconfig -o /boot/grub2/grub.cfg
/mnt/sysimage/boot/grub/grub.conf,刪除或註釋 password 行cat /boot/grub/grub.conf 查看配置grub-install /dev/sda