chattr
權限與擁有者
用法
sudo chattr +i [file]設定檔案系統層級的屬性,例如不可修改;加了 +i 連 root 也得先取消才能改,而且只在 Linux 的 ext4、xfs、btrfs 上有效。
常用選項
| 選項 | 含義 |
|---|---|
| +i | Immutable: not even root may change or delete it until -i |
| -i | Clear the immutable flag so the file can be edited again |
| +a | Append only, which suits a log file |
| +A | Do not update the access time on reads |
| -R | Apply through a whole tree |
| lsattr | The companion command that shows which flags are set |
範例
sudo chattr +i /etc/resolv.confStops anything from overwriting the file.
lsattr /etc/resolv.confShows the i in the flag list.
sudo chattr -i /etc/resolv.confRemoves it again so you can edit.
數字模式和符號模式說的是同一件事:755 和 u=rwx,go=rx 是同一個權限。
怎麼看
- 方括號 [ ] 表示這部分可以不寫。
- 省略號 … 表示可以寫多個。
- 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。
常見問題
Q. chattr 是做什麼的?
設定檔案系統層級的屬性,例如不可修改;加了 +i 連 root 也得先取消才能改,而且只在 Linux 的 ext4、xfs、btrfs 上有效。
Q. 怎麼寫?
sudo chattr +i [file] —— 方括號表示可以省略的部分。
Q. 值得記的選項有幾個?
這裡列了 6 個,完整列表在 man chattr。這條命令屬於權限與擁有者。
相關命令
man 手冊: man chattr