Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

If the write permission of the file is disabled, no one can change the file.

The chattr +i FILE also can protect the file from change.

Let me know what the difference, and when we should use chattr +i rather than chown -w.

like image 819
Effective Bugs Avatar asked Feb 02 '18 07:02

Effective Bugs


People also ask

What is chattr used for?

chattr (Change Attribute) is a command line Linux utility that is used to set/unset certain attributes to a file in Linux system to secure accidental deletion or modification of important files and folders, even though you are logged in as a root user. In Linux native filesystems i.e. ext2, ext3, ext4, btrfs, etc.

How do I change immutable files in Linux?

To make the files immutable, “chattr” command can be used. The filesystems in Linux ext2, ext3, ext4, btrfs supports all the flags, however all the flags won't support all non-native FS. One cannot delete or modify file/folder once attributes are sets with chattr command, even though you have full permission.

What is an immutable file?

An immutable file cannot be changed or renamed. An appendOnly file allows append operations, but not delete, modify, or rename operations. An immutable directory cannot be deleted or renamed, and files cannot be added or deleted under such a directory.

What does the immutable bit do to a file?

The command below makes /backups/passwd file immutable (or undeletable). This implies that the file can't be modified in any way: it can't be deleted or renamed. You can't even create a link to it and no data can be written to the file as well.


1 Answers

chattr +i sets the immutable filesystem attribute on the file. It differs from access control rules. Access control rules apply to the file attributes, while immutable is a filesystem extended file attribute, which may not be available on all filesystems. Only a user with root privileges can set or unset this extended attribute. Nobody, not even the owner or a user with write permission, can write into such file. A user without write file permission can create a hard link to a regular file, but if the file is marked as immutable, a user cannot create a hard link, since the filesystem cannot change the references count to this immutable file.

chattr +i is useful for protection from accidental deletion by root. Also an immutable file cannot be renamed or moved from one directory to another.

like image 102
273K Avatar answered Oct 17 '22 12:10

273K