# Root Password Recovery
[TOC]
- One simple way is to let super-user uses privilege to change root's password.
- But if the super-user account cannot be accessed or there is no super-user, here is another way to fix the problem -- GRUB.
## [GRUB](https://zh.wikipedia.org/wiki/GNU_GRUB)
- Restart the system and hold the `Shift` key or press the `Esc` key to enter into safe mode (**recovery mode**). Once you have entered safe mode, you will see the **GRUB menu**.
### Ubuntu SOP
1. Press `Esc` will enter BIOS, so you should **press `shift`** in order to see the option of recovery mode.

2. Get into root shell prompt without password, then you can change root's password.
### FreeBSD SOP
1. Press `shift` then press `shift-2` enter **Boot Single user**

2. The `mount -a` mentioned in article[2] doesn't work, should use the following one instead:
```bash=
mount -o rw -u /
```
- :x:

- :heavy_check_mark:

### CentOS SOP
- The way to change boot mode at GRUB is different between CentOS7 and previous version.
1. Press `e` at default boot option

2. It will enter a text editor

3. Add ` rd.break` at the end of the line which starts with `Linux` or `kernel`

> If the version of CentOS is lower than 7, the added string will be ` single`
4. Press `Ctrl-x` to enter single-user mode
5. Remount the `/sysroot/` and change root directory to `/sysroot` by following commands
```bash=
mount -o remount,rw /sysroot/
chroot /sysroot
```
6. Change password

7. Reboot

- Two ways
- Faster
```bash=
load_policy -i
chcon -t shadow_t /etc/shadow
exit
```
- Slower
```bash=
touch /.autorelabel
exit
```
## Reference
### Official
### Article
1. [How to Reset or Change the Root Password in Ubuntu/CentOS](https://phoenixnap.com/kb/how-to-change-root-password-linux)
2. [如何重設 Ubuntu、CentOS、Debian、FreeBSD 的 root 密碼](https://blog.miniasp.com/post/2012/06/12/How-to-reset-root-password-for-Ubuntu-CentOS-Debian-FreeBSD)
3. [Recover – Reset Forgotten Linux Root Password](https://linuxconfig.org/recover-reset-forgotten-linux-root-password)
4. [CentOS 7 如何重置root密碼](https://autumncher.pixnet.net/blog/post/462809249-%E3%80%90centos7%E3%80%91%E5%A6%82%E4%BD%95%E9%87%8D%E7%BD%AEroot%E5%AF%86%E7%A2%BC-%28how-to-recover-root-pas)