chattr

From Wikipedia, the free encyclopedia
Jump to: navigation, search

chattr is a command in the Linux operating system that allows a user to set certain attributes on a file residing on many Linux file systems. chflags is the analogous command on modern BSD systems, including OS X. The commands are similar to the attrib command on DOS, OS/2 and Microsoft Windows. Other Unix systems have no analogous commands; Solaris supports extended file attributes, but there are no user level commands for setting or changing them, and the chatr command in HP-UX, and chattr in AIX have unrelated functions.

chattr and lsattr utilities on Linux and the attributes they manipulate were originally specific to the Second Extended Filesystem family (ext2, ext3, ext4), and are available as part of the e2fsprogs package. The functionality has since been extended to many other Linux file systems, including XFS, ReiserFS, JFS, OCFS2 and others, although not all file systems support all flags.

chflags is not specific to particular file systems; UFS on BSD systems, and HFS+, SMB, AFP, and FAT on OS X support chflags for at least some flags.

Among other things, the command can be used to make files immutable so that password files and certain system files cannot be erased during software upgrades.[1]

In Linux systems (chattr)[edit]

Command description[edit]

The form of the chattr command is:

 chattr [-RVf] [-+=AacDdijsSu] [-v version] files...
  • -R is to recursively change attributes of directories and their contents
  • -V is to be verbose and print the program version
  • -f is to suppress most error messages

Attributes[edit]

Some attributes include:

File attributes on a Linux file system according to the chattr(1) Linux man page
Attribute names lsattr flag chattr option Semantics and rationale
No atime updates A +A to set
-A to clear
  • When a file with the A attribute set is accessed, its atime record is not modified.
  • This avoids a certain amount of disk I/O for laptop systems.
Append only a +a to set
-a to clear[note 1]
  • A file with the a attribute set can only be open in append mode for writing.
Compressed c +c to set
-c to clear[note 2]
  • A file with the c attribute set is automatically compressed on the disk by the kernel.
  • A read from this file returns uncompressed data.
  • A write to this file compresses data before storing them on the disk.
Synchronous directory updates D +D to set
-D to clear
  • When a directory with the D attribute set is modified, the changes are written synchronously on the disk
  • This is equivalent to the dirsync mount option, applied to a subset of the files.
No dump d +d to set
-d to clear
  • A file with the d attribute set is not candidate for backup when the dump program is run.
Compression error E (unavailable)
  • The E attribute is used by the experimental compression patches to indicate that a compressed file has a compression error.
Extent format e (unavailable)
  • The e attribute indicates that the file is using extents for mapping the blocks on disk.
Indexed directory I (unavailable)
  • The I attribute is used by the htree program code to indicate that a directory is being indexed using hashed trees.
Huge file h (unavailable)
  • The h attribute indicates the file is storing its blocks in units of the filesystem blocksize instead of in units of sectors.
  • It means that the file is, or at one time was, larger than 2TB.
Immutable i +i to set
-i to clear[note 1]
  • 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.
  • When set, prevents, even the superuser, from erasing or changing the contents of the file.
Data journalling j +j to set
-j to clear[note 3]
  • A file with the j attribute has all of its data written to the ext3 journal before being written to the file itself, if the filesystem is mounted with the "data=ordered" or "data=writeback" options.
  • When the filesystem is mounted with the "data=journal" option all file data is already journalled, so this attribute has no effect.
Secure deletion s +s to set
-s to clear[note 2][note 2]
Synchronous updates S +S to set
-S to clear
  • When a file with the S attribute set is modified, the changes are written synchronously on the disk; this is equivalent to the 'sync' mount option applied to a subset of the files.
  • This is equivalent to the sync mount option, applied to a subset of the files.
Top of directory hierarchy T +T to set
-T to clear
  • A directory with the T attribute will be deemed to be the top of directory hierarchies for the purposes of the Orlov block allocator.
  • This is a hint to the block allocator used by ext3 and ext4 that the subdirectories under this directory are not related, and thus should be spread apart for allocation purposes.
  • For example: it is a very good idea to set the T attribute on the /home directory, so that /home/john and /home/mary are placed into separate block groups.
  • For directories where this attribute is not set, the Orlov block allocator will try to group subdirectories closer together where possible.
No tail-merging t +t to set
-t to clear
  • For those filesystems which support tail-merging, a file with the t attribute will not have a partial block fragment at the end of the file merged with other files.
  • This is necessary for applications such as LILO which read the filesystem directly, and which don't understand tail-merged files.
Undeletable u +u to set
-u to clear[note 2]
  • When a file with the u attribute set is deleted, its contents are saved.
  • This allows the user to ask for its undeletion.
Compression raw access X (unavailable)
  • The X attribute is used by the experimental compression patches to indicate that a raw contents of a compressed file can be accessed directly.
Compressed dirty file Z (unavailable)
  • The Z attribute is used by the experimental compression patches to indicate a compressed file is "dirty".
Version / generation number -v -v version
  • File's version/generation number.

Notes[edit]

  1. ^ a b Only the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear these attributes.
  2. ^ a b c d These attributes are not honored by the ext2 and ext3 filesystems as implemented in the current mainline Linux kernels.
  3. ^ Only the superuser or a process possessing the CAP_SYS_RESOURCE capability can set or clear this attribute.

Other attributes[edit]

Other attributes include:

Related commands[edit]

  • lsattr – view file attributes

In BSD-like systems (chflags)[edit]

Command description[edit]

The form of the chflags command is:

 chflags [-R [-H | -L | -P]] flags file ...
  • -H If the -R option is specified, symbolic links on the command line are followed. (Symbolic links encountered in the tree traversal are not followed.)
  • -L If the -R option is specified, all symbolic links are followed.
  • -P If the -R option is specified, no symbolic links are followed. This is the default.
  • -R Change the file flags for the file hierarchies rooted in the files instead of just the files themselves.

Attributes[edit]

Some attributes include: