Pages

Friday, May 5, 2017

What is chattr command used for

chattr - Change attribute, changes the file attributes on a Linux file system


This is very useful to set attributes in system files like passwd and shadow files wherein user’s info are contains.

Syntax

# chattr [operator] [flags] [filename]

Options

a - file set with ‘a‘ attribute, can only be open in append mode for writing.
A - If a file is accessed with ‘A‘ attribute set, its atime record is not updated.
S - If a file is modified with ‘S‘ attribute set, the changes are updates synchronously on the disk.
i - file set with ‘i‘ attribute, cannot be modified (immutable). Means no renaming, no symbolic link creation, no execution, no writable, only superuser can unset the attribute.
j - file set with ‘j‘ attribute is set, all of its information updated to the ext3 journal before being updated to the file itself.
t - A file is set with ‘t‘ attribute, no tail-merging.
d - file set with ‘d‘, will no more candidate for backup when the dump process is run.
u - file set with ‘u‘ attribute is deleted, its data are saved. This enables the user to ask for its undeletion.

The letters 'aAcCdDeijsStTu' select the new attributes for  the  files: append only (a), no atime updates (A), compressed (c), no copy on write (C), no dump (d), synchronous directory updates (D), extent format (e), immutable  (i),  data journalling (j), secure deletion (s), synchronous updates (S), no tail-merging (t), top of directory hierarchy  (T),  and undeletable (u).

Operator

+ : Adds the attribute to the existing attribute of the files.
– : Removes the attribute to the existing attribute of the files.
= : Keep the existing attributes that the files have.

Note: The immutable bit +i can only be set by superuser (i.e root) user or a user with sudo privileges can able to set.

After setting immutable bit, let’s verify the attribute with command ‘lsattr‘.
# lsattr

The  operator  ‘+’  causes  the  selected attributes to be added to the existing attribs of the files; ‘-’ causes them to  be  removed;  and ‘=’ causes them to be the only attributes that the files have.

No comments:

Post a Comment