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