grub使用总结
2013-10-06 22:18:53 阿炯

Grub是大多数Linux的启动引导器,基本上代替了LILO成为多数计算机的引导程序,本文就介绍一些与之相关的使用方法。

-----------------------------
grub修改超时值

grub 2 是新版本的系统引导程序,它完全重写过,从1.90版及以上下面介绍的方法都适用。

GRUB 2 is the next generation of GNU GRUB, using version numbers 1.90 and above. GRUB 2 is a complete rewrite. The most important goal is to make GNU GRUB cleaner, safer, more robust, more portable and more powerful.

If you're already familiar with GRUB Legacy, you'll find that the GRUB 2 configuration file is similar in broad strokes, but it varies in many details. You should be aware of alternative means of configuring GRUB 2, as well.

To make any changes the way GRUB 2 works you need to edit the main GRUB 2 configuration file  located in /etc/default/ folder, you can edit it by running the following command from terminal:
sudo gedit /etc/default/grub you should see the content very similar to this ....

/boot/grub/grub.cfg

Change default Timeout:
To change the default timeout option in GRUB 2, you just need to change the GRUB_TIMEOUT parameter. The value of this parameter is in sec, change this value as per your requirement.

Change the default boot option:
To change the default boot option in GRUB 2, you just need to change the GRUB_DEFAULT parameter. 0 is the first entry, so if you want Windows to be your default boot option which is, say at, 5th position in the grub menu, then you just need to change the GRUB_DEFAULT value to 4 and save the file and close it.

debian 7下
一般不需要直接编辑'/boot/grub/grbu.cfg'文件,而修改其配置文件:/etc/default/grub,更改'GRUB_TIMEOUT'值即可。

在vyatta 6.6下,还是修改'/boot/grub/grub.cfg'中的选项:
set timeout=1

-----------------------------
解决老电脑关机不畅

老电脑或低配置的虚拟机关闭acpi电源管理模块,在启动的grub的配置文件中指定记录:acpi=off

-----------------------------
在UEFI平台通过grub2引导各种介质操作系统
-----------------------------
ArchLinux-GRUB
-----------------------------
查看当前系统默认启动的内核版本,结果可能有如下两种:
$ grub2-editenv list
# 结果1:无相关信息
saved_entry=0
# 结果2:当前运行的系统版本
saved_entry=CentOS Linux (3.10.0-327.el7.x86_64) 7 (Core)

切换系统启动时默认内核版本:
$ grub2-set-default 'CentOS Linux (3.10.0-957.el7.x86_64) 7 (Core)'

如果能顺利重启服务器,登录后查看系统信息:
$ uname -a
$ cat /proc/version   
Linux version 4.1.52 (root@cruxp) (gcc version 5.2.0 (CRUX-x86_64-multilib) ) #5 SMP Sat Oct 13 09:55:58 CST 2018
-----------------------------
进入CentOS7紧急模式恢复root密码

本操作方法用于找回操作系统的最高权限用户root的密码,从引导程序进行了入手处理。

第一步、重启CentOS7的主机,在Grub的引导界面选择要编辑的内核(一般第一个),按e进入编辑界面


第二步、在编辑界面找到如下一行,将ro改为rw init=/sysroot/bin/sh。改完后<Ctrl>+x启动紧急模式


第三步、换根,修改root密码,退出&重启

chroot /sysroot    换根

LANG=en    设置当前语言

passwd    修改密码

touch /.autorelabel    更新系统信息,如果SELinux开着,没有这一步新密码将无法生效

exit    退出当前根

reboot    重启

如果没有指定'LANG=en'时,中文界面会有方块字出现(无法渲染的宽字符集字体),但也无需担心继续正常操作即可。

-----------------------------
CentOS7下为Grub设置密码防止通过单用户模式破解root密码

接上文的紧急(单用户)模式破解root密码,需要进行相关的设定来防止一些能取得我们的主机本地操作界面从而破解密码的情况。

产生加密密码
$ LANG="en_US.UTF-8"
$ grub2-mkpasswd-pbkdf2
Enter password:
Reenter password:
PBKDF2 hash of your password is grub.pbkdf2.sha512.10000.很长的一串大写字母与数字字串

2)将加密密码加入到/etc/grub.d/00_header
# vim /etc/grub.d/00_header
//到最后加一段
cat <<EOF
set superusers='freeoa'    //这里的用户名可以不存在于系统中,单独使用于grub加密
//这里也需要写用户名
password_pbkdf2 freeoa grub.pbkdf2.sha512.10000.很长的一串大写字母与数字字串
EOF
//然后保存退出

3)更新grub信息
# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-862.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-862.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-f5097333f3d74aec9635c5h9d6bc0638
Found initrd image: /boot/initramfs-0-rescue-f5097333f3d74aec9635c5h9d6bc0638.img
done

注意:必须更新grub信息,否则不会生效

4)重启时,想进入单用户模式破解root用户时,就必须输入设置的用户名和密码

我们按下e进入单用户模式

-----------------------------

-----------------------------

-----------------------------

-----------------------------


-----------------------------

-----------------------------

-----------------------------

-----------------------------

-----------------------------

-----------------------------