实验:给grub和系统添加密码
目的
启动系统时可要求输入密码才能进入系统,还有在编辑grub界面时,要求输入密码。这两种要求输入密码的设置
在/boot/grub/grub.conf文件中添加password指令即可。
前提
可用的centos6系统。
实验步骤
1.修改/boot/grub/grub.conf⽂件,增加密码设置
[root@centos6 ~]# vim /boot/grub/grub.conf
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password 123.com #第⼀个password设置密码为123.com,该密码是进⼊grub界⾯要求输⼊的密码。
title CentOS 6 (2.6.32-754.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=UUID=a7f98a2f-7452-4404-b768-d6e1cbae0b5e rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-754.el6.x86_64.img
password 234.com #第⼆个password设置密码为
234.com即在title段最后⼀⾏,该密码是启动系统时要求的密码。
2. 重启reboot
[root@centos6 ~]# reboot
3. 中⽌启动,进⼊grub菜单
- 在如下界⾯时,迅速按⽅向键的上下键,中⽌系统启动,进⼊grub菜单。
- 中⽌系统启动
- 进⼊grub菜单,根据提⽰,输⼊:p。此时,输⼊设置的grub菜单密码:123.com
- 在这个界⾯根据提⽰,才能编辑启动时内核参数,这⾥不做过多介绍指定启动时内核参数,直接回车确认启动系统。
- 要求输⼊启动系统的密码:234.com
注意:此种加密⽅式容易被⼈发现,因为密码以明⽂⽅式写在/boot/grub/grub.conf⽂件中,只要能读取这个⽂件,就可
获得密码。因此,需要对密码进⾏再次加密后,再放在配置⽂件中。
4、⽤md5加密密码,以密⽂的⽅式写在配置⽂件中
- 给启动系统的密码123.com加密,输⼊2次123.com:
[root@centos6 ~]# grub-md5-crypt
Password:
Retype password:
$1$n9Qmu1$w4cBrFJyK28GSpZkHYAA.1
- 给grub菜单的密码234.com加密,输⼊2次234.com:
[root@centos6 ~]# grub-md5-crypt
Password:
Retype password:
$1$dMQmu1$qBHQaHZr/JjgyXd5uePy00
- 再把这两种加密结果,当做密码即写在对应的password指令后⾯即可。
编辑/boot/grub/grub.conf
[root@centos6 ~]# vim /boot/grub/grub.conf
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --md5 $1$n9Qmu1$w4cBrFJyK28GSpZkHYAA.1 #密码123.com
title CentOS 6 (2.6.32-754.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=UUID=a7f98a2f-7452-4404-b768-d6e1cbae0b5e rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-754.el6.x86_64.img
password --md5 $1$dMQmu1$qBHQaHZr/JjgyXd5uePy00 #密码234.com
- 重启测试
[root@centos6 ~]# reboot
按照上述,输⼊密码均可进⼊grub菜单和登录系统。
5、也可使⽤sha512加密
- 仅对以密码234.com为例,使⽤sha512对其加密
[root@centos6 ~]# grub-crypt
Password:
Retype password:
$6$P8wmcaD0AQNGVLB7$sraBmEyxMQJg/CBIcsQrShRmLQLZQHA.uWbzB0gFVE1DjGSUWedHV.75qPiVCY0fHJsgvsIoW0h0wVz4RkjYC/
- 把这个结果当做密码,放置grub.conf⽂件中的password指令的后⾯,但要把–md5替换为–encrypted。
[root@centos6 ~]# vim /boot/grub/grub.conf
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --md5 $1$IxTmu1$TyGwD9umKoisyxSZCzruL.
title CentOS 6 (2.6.32-754.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=UUID=a7f98a2f-7452-4404-b768-d6e1cbae0b5e rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-754.el6.x86_64.img
password --encrypted $6$P8wmcaD0AQNGVLB7$sraBmEyxMQJg/CBIcsQrShRmLQLZQHA.uWbzB0gFVE1DjGSUWedHV.75qPiVCY0fHJsgvsIoW0h0wVz4RkjYC/
- 重启系统
[root@centos6 ~]# reboot
使用该加密方式也可成功加密,输入密码后成功登陆系统。
当然,同时使用md5加密、sha512加密和明文密码3种加密方式写在/boot/grub/grub.conf配置文件中。
完成
注意:以上⼏个实验证明,即使/boot/grub/⽬录下所有⽂件都删除后,仍然是相同的修复⽅法,详情不做过多介绍,简 单总结步骤如下:
1.光盘启动,进入救援模式
2.chroot /mnt/sysimage
3.grub-install /dev/sda
4.vim /boot/grub/grub.conf
把上内核文件、grub启动菜单等内容编写到该文件中即可。
5.exit;reboot