在前面的文章中,讲过了忘记 root 密码的方法,是修改 grub 启动信息,使其进入单用户模式,这样呢,也存着一种安全隐患。我们可以把 grub 加上密码,这样在启动的时候,编辑菜单,需要先输入密码,才可以编辑,下面来讲一下。
PS:适合用于 Centos Redhat 等版本。
在系统启动以后,在命令行模式下输入 grub-md5-crypt 命令:
[root@localhost ~]# grub-md5-crypt Password: Retype Password: $1$ZGcZx$UGjztKlsrLfIgNYyVgcPB1 [root@localhost ~]#
需要把这个加密后的密码,也就是 $1$ 开头的这个,记录下来,等下我们会用到。
拿到加密后的密文后,直接编辑 /boot/grub/grub.conf 文件,在 title 的前一行,加上下面一行:
password --md5 $1$ZGcZx$UGjztKlsrLfIgNYyVgcPB1
然后保存,退出,即可,我的文件如下:
[root@bogon ~]# cat /boot/grub/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/mapper/VolGroup-lv_root # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu password --md5 $1$ZGcZx$UGjztKlsrLfIgNYyVgcPB1 title CentOS 6 (2.6.32-573.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM initrd /initramfs-2.6.32-573.el6.x86_64.img [root@bogon ~]#
截图如下:
再次重启的时候,若要修改 grub 菜单,则需要先按 P,然后输入密码,后才可以编辑
PS:也可以直接在 grub.conf 使用明文密码 password 123456 这样,只不过密码是明文的存在。