首页·终端命令

chattr

权限与归属

用法

sudo chattr +i [file]

设置文件系统级别的属性,比如不可修改;加了 +i 连 root 也得先取消才能改,而且只在 Linux 的 ext4、xfs、btrfs 上有效。

常用选项

选项含义
+iImmutable: not even root may change or delete it until -i
-iClear the immutable flag so the file can be edited again
+aAppend only, which suits a log file
+ADo not update the access time on reads
-RApply through a whole tree
lsattrThe companion command that shows which flags are set

示例

sudo chattr +i /etc/resolv.conf

Stops anything from overwriting the file.

lsattr /etc/resolv.conf

Shows the i in the flag list.

sudo chattr -i /etc/resolv.conf

Removes 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