Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 誤刪 root 使用者?用安裝光碟救援模式恢復 root 帳號與密碼

編輯:關於 Linux

不慎刪除 /etc/passwd 中的 root 列,或用 userdel root 導致無法以最高權限登入。只要根分區未損壞,透過安裝光碟進入救援模式掛載分區、修補 /etc/passwd 即可恢復。

救援模式恢復 root 示意圖

恢復步驟

  1. 插入 Linux 安裝光碟啟動,選擇 Rescue Mode / Linux Rescue(開機選單按 Alt 或 Tab 輸入 linux rescue)。
  2. 選擇語言、鍵盤,跳過網路設定,選擇 Continue 讓系統自動掃描並掛載根分區至 /mnt/sysimage。
  3. 若未自動掛載,手動執行:
# 查看分區
fdisk -l

# 掛載根分區 (假設為 /dev/sda2)
mount /dev/sda2 /mnt

# 綁定裝置節點 (必要時)
mount -o bind /dev /mnt/dev
  1. 進入 chroot 環境:
chroot /mnt
  1. 編輯 /etc/passwd,確認或新增 root 列:
root:x:0:0:root:/root:/bin/bash

欄位說明:使用者名:密碼佔位符:UID:GID:描述:家目錄:Shell

  1. 設定 root 密碼:
passwd root
# 輸入兩次新密碼
  1. 離開 chroot、卸載、重啟:
exit
umount /mnt/dev
umount /mnt
reboot

驗證恢復

重啟後以 root 登入,確認權限正常:

whoami
# root
id
# uid=0(root) gid=0(root) groups=0(root)

擴充:同時修復 /etc/shadow 與 /etc/group

若 /etc/shadow 也遺失 root 列,需同步新增:

root:$6$salt$hashed_password:18000:0:99999:7:::

密碼雜湊可用 openssl passwd -6 產生,或直接用 passwd 指令自動更新。

/etc/group 確認 root 群組存在:

root:x:0:

預防措施

  • 避免直接編輯 /etc/passwd,改用 vipw、vigr(鎖定檔案防衝突)。
  • 建立 sudo 使用者並加入 wheel/sudo 群組,日常操作不使用 root 登入。
  • 定期備份 /etc/passwd、/etc/shadow、/etc/group、/etc/gshadow。

救援模式是 Linux 系統管理的「後悔藥」,熟練掌握 chroot 修補系統檔案技巧,能化解多種無法開機、帳號遺失的危機。

Copyright © Windows教程網 All Rights Reserved