`chattr +i FILE` 和 `chmod -w FILE` 有什么区别?

What' the differences between `chattr +i FILE` and `chmod -w FILE`?

如果文件的写权限被禁用,则任何人都无法更改文件。

chattr +i FILE也可以保护文件不被更改。

让我知道有什么区别,什么时候我们应该使用 chattr +i 而不是 chown -w

chmod -w file 在所有 UNIX 环境中可用,但是 chattr -i FILE 使用扩展文件属性可能在您的系统上不可用,具体取决于文件类型 System/Distro!

除此之外,请查看 this link 使用扩展文件属性的优点和缺点。

chattr +i 设置文件的 immutable 文件系统属性。它不同于访问控制规则。访问控制规则适用于文件属性,而 immutable 是文件系统 扩展的 文件属性,可能并非在所有文件系统上都可用。只有具有 root 权限的用户才能设置或取消设置此扩展属性。任何人,甚至所有者或具有写权限的用户都不能写入此类文件。没有写文件权限的用户可以创建硬 link 到常规文件,但如果文件被标记为不可变,则用户不能创建硬 link,因为文件系统无法将引用计数更改为这个不可变文件。

chattr +i 可用于防止 root 意外删除。此外,不可变文件不能重命名或从一个目录移动到另一个目录。

来自 chattr 手册页

A file with the 'i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file. Only the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.

如您所见,chattrchmod 更强大。 chmod -w 仅删除对文件内容的“write”权限。而且你还需要使用 chattr +i 到 protect/lock 文件然后 chattr -i 解锁