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 ACL 權限備份還原實戰:getfacl/setfacl 批量修正技巧

編輯:Linux 教學

背景

誤執行 chmod -R 777 / 導致系統癱瘓。利用同版本健康主機的 ACL 備份,透過 getfacl/setfacl 快速還原權限,避免重裝系統。

核心指令對照

指令用途
getfacl -R 目錄 > backup.acl遞迴匯出目錄及所有子項的 ACL 到檔案
setfacl --restore=backup.acl從備份檔還原 ACL(會覆蓋現有權限)

實戰流程(以 /root 為例)

# 1. 確認當前目錄
pwd
# /

# 2. 備份健康主機 /root 權限
getfacl -R /root > 11.bak

# 3. 故意破壞權限模擬災難
chmod -R 700 /root/*

# 4. 切換到目標主機同路徑還原
cd /
setfacl --restore=11.bak

# 5. 驗證
ls -l /root

進階技巧

  • 跨主機同步:ssh healthy_host "getfacl -R /etc" | setfacl --restore=- 透過管道直接還原。
  • 僅備份特定類型:getfacl -R -t d /path 僅目錄,-t f 僅檔案。
  • 排除掛載點:備份前 mount --bind / /mnt/rootfs 僅備份根檔案系統,避免跨裝置。或使用 find / -xdev -exec getfacl ...。
  • 定期自動化:寫入 cron,每日備份關鍵目錄 (/etc, /home, /var/www) ACL 到異地存儲。

注意事項

  • 檔案系統必須掛載 acl 選項 (/etc/fstab 加入 acl),tune2fs -o acl /dev/sdX 亦可。
  • setfacl --restore 會完全覆蓋目標 ACL,謹慎執行。
  • 若目標主機缺少備份中存在的用戶/群組,還原會報警告但不會中斷,需事後補齊帳號。

總結

getfacl/setfacl 是權限災難復原的利器。養成「異構主機同版本 ACL 定期備份」習慣,關鍵時刻可將恢復時間從小時級壓縮至分鐘級。

Copyright © Windows教程網 All Rights Reserved