File attribute manipulation with chattr
Linux systems have a utility for changing file attributes on a Linux file system chattr
, as well as lsattr
that lists the attributes that are set currently. Those are not the -rwxrw-r--
-type permissions for owner/group/others, but lower level attributes, which have some interesting properties.
Previously this functionality was available for ext{2,3,4} file systems and could be managed with e2fsprogs, however now it is available for other file systems as well (JFS, XFS, ReiserFS).
By default, a file on a Linux filesystem does not have any attributes set. You can check it with lsattr
:
$> lsattr test.file
---------------- test.file
There are quite a few attributes, some of them can be set and cleared with chattr
, some are meant to be set in special circumstances, so they do not have any corresponding flag for chattr. Some file attributes can be set only with root user.
Interesting attributes
One of the noteworthy attributes is i
, which, if set, means, that the file is immutable - it cannot be changed or deleted by anyone, not even root. This attribute generally can only be set/cleared by root.
$> sudo chattr +i test.file
$> lsattr test.file
----i----------- test.file
$> sudo rm test.file
rm: cannot remove test.file: Operation not permitted
$> echo "blimp" > test.file
-bash: test.file: Permission denied
$> sudo echo "blimp" > test.file
-bash: test.file: Permission denied
Similarly, a file with an a
flag set means, that it can only be appended. This attribute too can only be set/cleared by root.
$> sudo chattr +a test.file
$> lsattr test.file
-----a---------- test.file
$> sudo echo "blimp" > test.file
-bash: test.file: Operation not permitted
$> sudo echo "blimp" >> test.file
$> echo $?
0
The A
flag, if set, disables updating atime
record of a file:
$> touch test.file
$> stat test.file
[..]
Access: 2016-10-02 12:08:17.973873162 +0000
Modify: 2016-10-02 12:08:17.973873162 +0000
Change: 2016-10-02 12:08:17.973873162 +0000
$> cat test.file
$> stat test.file
[..]
Access: 2016-10-02 12:08:28.224995928 +0000
Modify: 2016-10-02 12:08:17.973873162 +0000
Change: 2016-10-02 12:08:17.973873162 +0000
$> chattr +A test.file
$> lsattr test.file
-------A-------- test.file
$> cat test.file
$> stat test.file
[..]
Access: 2016-10-02 12:08:28.224995928 +0000
Modify: 2016-10-02 12:08:17.973873162 +0000
Change: 2016-10-02 12:08:46.595175969 +0000
noatime
can also be set as a mount option when mounting a filesystem and in /etc/fstab, but this attribute allows it to be set per-file.
Other attributes are set automatically in some circumstances, change low-level filesystem parameters (e.g. data journaling or sync options) or they are not supported by ext filesystems. Wikipedia in this case has a good list on the topic.