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

su 切換用戶報「Permission denied」根因:根目錄遺失執行權限

編輯:Linux 教學

某 Oracle 資料庫伺服器重啟後,root 無法 su - oracle,報錯:

su: warning: cannot change directory to /home/oracle: Permission denied
su: /bin/bash: Permission denied

直接以 oracle 登入同樣失敗。檢查 /bin/bash、/home/oracle、/lib/ld-*.so 權限均正常,甚至將 home 改為 /tmp 並設 777 仍無效。最終發現真兇是 根目錄 / 遺失執行權限(x bit)。

排查過程關鍵點

  1. 常規權限檢查(二進制檔、家目錄、動態連結器)均正常。
  2. 修改 /etc/passwd 將 home 指向 /tmp(777)仍失敗,排除家目錄權限問題。
  3. 使用 stat / 發現根目錄權限為 drw-rw-rw- (0666),缺少 執行權限(x)。

為什麼缺少 x 權限會導致 su 失敗?

目錄的 執行權限(x)代表「允許進入該目錄並存取其中的 inode」。su 過程需:

  1. 進入目標用戶家目錄(需目錄 x 權限)
  2. 讀取 /etc/passwd、/bin/bash、/lib/ld-*.so 等檔案(需其父目錄一路有 x 權限)
  3. 啟動 Shell 進程(需 / 有 x 權限以解析路徑)

根目錄失去 x 權限,導致所有跨目錄的路徑解析失敗,故 su、ssh、login、各類 daemon 皆會異常。

修復命令

# chmod 755 /

修復後立即生效,無需重啟。

常見導致原因與預防

錯誤操作說明
chmod 666 /誤以為給根目錄最大權限,實則移除 x bit
chmod 700 /lib/ld-*.so動態連結器失去存取權,同樣導致 su 失敗
自動化腳本誤跑 chmod -R 666 /毀滅性操作,需從備份恢復

預防建議:

  • 嚴禁對 /、/usr、/lib、/bin、/sbin 遞迴修改權限。
  • 使用 rpm --setperms 或 dpkg --verify 定期核對套件權限。
  • 建立權限基線審計腳本,監控關鍵目錄權限變更。
Copyright © Windows教程網 All Rights Reserved