Skip to main content

Linux Rescue

RedHat/CentOS

Single-User Mode

Situation: Reset the root's password

For GRUB Bootloader)

系統重開機 -> 進入開機選單

  1. Select the kernel
  2. Press the e key to edit the entry
  3. Select second line (the line starting with the word kernel)
  4. Press the e key to edit kernel entry so that you can append single user mode
  5. Append the letter S (or word Single) to the end of the (kernel) line
  6. Press ENTER key (或者 Ctrl + x 直接啟動系統,跳過下一個步驟)
  7. Now press the b key to boot the Linux kernel into single user mode
  8. At prompt type passwd command to reset password:
mount -t proc proc /proc
mount -o remount,rw /
passwd
sync
reboot

For LILO Bootloader)

Boot: linux single

passwd
sync
reboot

TIP: 在 RedHat 8 的環境,single-user mode 就是 rescue mode,輸入的字串改成  systemd.unit=rescue.target

除了 single-user mode,還有另一個 rd.break 也可以試試。

以安裝光碟開機的救援

以安裝光碟開機,在開機選單裡選擇 Rescue 項目,開機過程會有以下導引:

  1. 選擇語言
  2. 選擇鍵盤類型
  3. 是否要啟用網路功能

最後進入 shell 模式,系統的根目錄會被掛載為 /mnt/sysimage。

切換根目錄指令

chroot /mnt/sysimage
Emergency Mode (緊急開機模式)

NOTE: Emergency 不支援網路功能,如果系統修復會需要網路,不適合使用這模式。

除了 single-user mode,還有另一種開機模式 emergency mode,也適合用來修復無法開機的系統,不過,要進入 emergency mode 必須先輸入 root 密碼,所以不適合用來做重置密碼的用途。

在 RedHat 6 環境,使用方法與 single-user mode 一樣,只是將字串替換成 emergency

在 RedHat 8 環境,字串換成 systemd.unit=emergency.target

Ubuntu/Debian

Reset the root's password

Chroot

Fix a Broken Bootloader Using Chroot
# Make a bootable USB by downloading a Linux ISO and booting from the USB.
# Mount your system partition to work with chroot.
sudo mount -t ext4 /dev/sda /mnt

sudo mount --bind /dev /mnt/dev &&
sudo mount --bind /dev/pts /mnt/dev/pts &&
sudo mount --bind /proc /mnt/proc &&
sudo mount --bind /sys /mnt/sys

# Let’s chroot into the “/mnt” directory and enter the broken system.
sudo chroot /mnt

# Install, check, and update the GRUB bootloader in your system.
grub-install /dev/sda
grub-install --recheck /dev/sda
update-grub

# Exit the shell using the exit command mentioned earlier. 
# Unbind the previously bound directories and unmount the filesystem.
sudo umount /mnt/sys &&
sudo umount /mnt/proc &&
sudo umount /mnt/dev/pts &&
sudo umount /mnt/dev &&
sudo umount /mnt

# Reboot your PC and unplug the live USB.

Q & A

根目錄唯讀,使用了 remount 也沒用

試過了 single-user,emergency,安裝光碟救援的所有模式,始終無法將根目錄掛載成可讀寫,使用的指令是 mount -o remount,rw / 。原因可能是根目錄的 filesystem 有區塊損壞,必須先完成修復,才能進行重新掛載。

修復 filesystem

fsck -y /dev/your-root-disk