# Grub2

##### RedHat/CentOS 7

設定檔：

- /etc/default/grub，通用設定
- /etc/grub.d/40\_custom，自訂內容

建立開機用的設定檔（請勿手動編輯）

```shell
grub2-mkconfig -o /boot/grub2/grub.cfg

# 如果是 EFI 開機，改用這行
grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
```

修改預設的開機選項  
預設的項目是透過 /etc/default/grub 檔內的 GRUB\_DEFAULT 行來定義。不過，要是 GRUB\_DEFAULT 行被設定為 saved，這個選項便儲存在 /boot/grub2/grubenv 檔內。你可以這樣檢視它：

```shell
# 列出目前所有開機選項
awk -F\' '$1=="menuentry " {print $2}' /boot/grub2/grub.cfg
# 或
grep "^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2

# 目前預設的開機
grub2-editenv list

saved_entry=CentOS Linux (3.10.0-229.14.1.el7.x86_64) 7 (Core)

# 調整預設開機
grub2-set-default 2              # NOTE: 開機選項清單的第一項編號是 0
```

##### 密碼保護 GRUB

避免用戶以 single user mode 進入修改 root 密碼。

建立密碼

```
[root@rh4 ~]# /sbin/grub-md5-crypt
Password:
Retype password:
$1$Nk8AX$zsPr79zeiwd0zwF94DND60
```

修改 /boot/grub/grub.conf

增加 password 那兩行，與填入密碼的加密字串。

```
default=0
timeout=5

# Password Protecting GRUB
password --md5 $1$Nk8AX$zsPr79zeiwd0zwF94DND60

splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
```

##### Learning

- [How to Rescue, Repair and Reinstall GRUB Boot Loader in Ubuntu](https://www.tecmint.com/rescue-repair-and-reinstall-grub-boot-loader-in-ubuntu/)