You are on page 1of 7

chattr

chattr is the command in the GNU operating system (with Linux


chattr
kernel) that allows a user to set certain attributes of a file. lsattr is
the command that displays the attributes of a file. Original author(s) Remy Card
Operating system GNU/Linux
Most BSD-like systems, including macOS, have always had an
analogous chflags command to set the attributes, but no command Platform Cross-platform
specifically meant to display them; specific options to the ls Type Command
command are used instead. The chflags command first appeared in
4.4BSD. chflags
Solaris has no commands specifically meant to manipulate them. Operating system BSD-like
chmod[1] and ls[2] are used instead. systems,
including
Other Unixes, in general, have no analogous commands. The similar- macOS
sounding commands chatr (from HP-UX) and lsattr (from Platform Cross-platform
AIX) exist but have unrelated functions.
Type Command
Among other things, the chattr command is useful to make files
immutable so that password files and certain system files cannot be erased during software upgrades.[3]

Contents
In GNU/Linux systems (chattr and lsattr)
File system support
chattr description
lsattr description
Attributes
Notes
In BSD-like systems (chflags)
File system support
chflags description
Displaying
Attributes
See also
Notes
References

In GNU/Linux systems (chattr and lsattr)


File system support

The command line tools chattr (to manipulate attributes) and lsattr (to list attributes) were originally
specific to the Second Extended Filesystem family (ext2, ext3, ext4), and are available as part of the e2fsprogs
package.

However, the functionality has since been extended, fully or partially, to many other systems, including XFS,
ReiserFS, JFS and OCFS2. The btrfs file system includes the attribute functionality, including the C flag,
which turns off the built-in copy-on-write (CoW) feature of btrfs due to slower performance associated with
CoW.

chattr description

The form of the chattr command is:

chattr [-RVf] [-+=AacDdijsTtSu] [-v version] files...

-R recursively changes attributes of directories and their contents


-V is to be verbose and print the program version
-f suppresses most error messages

lsattr description

The form of the lsattr command (gnu 1.41.3):

lsattr [ -RVadv ] [ files... ]

-R recursively lists attributes of directories and their contents


-V displays the program version
-a lists all files in directories, including dotfiles
-d lists directories like other files, rather than listing their contents

Attributes

Some attributes include:


File attributes on a Linux file system according to the chattr(1) Linux man page

lsattr
Attribute chattr option Semantics and rationale
flag

No atime +A to set When a file with the A attribute set is accessed, its atime
A record is not modified.
updates -A to clear
This avoids a certain amount of disk I/O operations.

+a to set
Append only a A file with the a attribute set can only be open in append mode
-a to clear[note 1] for writing.

A file with the c attribute set is automatically compressed on the


+c to set disk by the kernel.
Compressed c A read from this file returns uncompressed data.
-c to clear[note 2]
A write to this file compresses data before storing them on the
disk.

A file with the C attribute will not be subject to Copy-on-Write


No Copy-on- +C to set updates.
C
Write (CoW) -C to clear[note 3] Updates to these files may not be subject to atomic snapshots,
and may lack some reliability information on some filesystems
and kernels.

Synchronous When a directory with the D attribute set is modified, the


+D to set changes are written synchronously on the disk
directory D
-D to clear
updates This is equivalent to the dirsync mount option, applied to a
subset of the files.

+d to set A file with the d attribute set is not candidate for backup when
No dump d
-d to clear the dump program is run.

Compression The E attribute is used by the experimental compression


E (unavailable) patches to indicate that a compressed file has a compression
error
error.

Extent format e (unavailable) The e attribute indicates that the file is using extents for
mapping the blocks on disk.

The h attribute indicates the file is storing its blocks in units of


Huge file h (unavailable) the filesystem blocksize instead of in units of sectors.
It means that the file is, or at one time was, larger than 2TB.

Indexed The I attribute is used by the htree program code to indicate


I (unavailable)
directory that a directory is being indexed using hashed trees.

A file with the i attribute cannot be modified.


+i to set It cannot be deleted or renamed, no link can be created to this
Immutable i file and no data can be written to the file.
-i to clear[note 1]
When set, prevents, even the superuser, from erasing or
changing the contents of the file.

Data j +j to set
journaling -j to clear[note 4] 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 journaled, so this attribute has no
effect.

+s to set
Secure When a file with the s attribute set is deleted, its blocks are
s -s to
deletion zeroed and written back to the disk.
clear[note 2][note 5]

When a file with the S attribute set is modified, the changes are
Synchronous +S to set written synchronously on the disk; this is equivalent to the 'sync'
S mount option applied to a subset of the files.
updates -S to clear
This is equivalent to the sync mount option, applied to a subset
of the files.

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
Top of the subdirectories under this directory are not related, and thus
+T to set should be spread apart for allocation purposes.
directory T
-T to clear
hierarchy 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.

For those filesystems that support tail-merging, a file with the t


No tail- +t to set attribute will not have a partial block fragment at the end of the
t file merged with other files.
merging -t to clear
This is necessary for applications such as LILO, which reads
the filesystem directly and doesn't understand tail-merged files.

+u to set When a file with the u attribute set is deleted, its contents are
Undeletable u saved.
-u to clear[note 2]
This allows the user to ask for its undeletion.

Compression The X attribute is used by the experimental compression


X (unavailable) patches to indicate that a raw contents of a compressed file can
raw access
be accessed directly.

Compressed The Z attribute is used by the experimental compression


Z (unavailable)
dirty file patches to indicate a compressed file is "dirty".

Version /
generation -v -v version File's version/generation number.
number

Notes
1. Only the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set
or clear these attributes.
2. These attributes are not honored by the ext2 and ext3 filesystems as implemented in the
current mainline Linux kernels.
3. These attributes only make sense for Copy-on-Write file-systems such as btrfs.
4. Only the superuser or a process possessing the CAP_SYS_RESOURCE capability can set or
clear this attribute.
5. This attribute is not honored by the ext4 filesystem as implemented in the current mainline
Linux kernels as reported in Bug #17872 (https://bugzilla.kernel.org/show_bug.cgi?id=17872).

In BSD-like systems (chflags)

File system support

The chflags command is not specific to particular file systems. UFS on BSD systems, and APFS, HFS+,
SMB, AFP, and FAT on macOS support least some flags.

chflags description

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.

Displaying

BSD-like systems, in general, have no default user-level command specifically meant to display the flags of a
file. The ls command will do with either the -lo, or the -lO, depending on the system, flags passed.

Attributes

All traditional attributes can be set or cleared by the super-user; some can also be set or cleared by the owner
of the file. Some attributes include:
File attributes
chflags Owner- OS
Attribute ls flag Semantics and rationale
flag settable support

arch,
Archived arch No All File is archived
archived

Directory is opaque when viewed through a


Opaque opaque opaque Yes All
union mount
No dump nodump nodump Yes All File cannot be dumped

System sappnd, Existing data in the file can't be overwritten


sappnd No All
append-only sappend and the file cannot be truncated

User uappnd, Existing data in the file can't be overwritten


uappnd Yes All
append-only uappend and the file cannot be truncated

schg,
System File cannot be changed, renamed, moved,
schg schange, No All
immutable or removed
simmutable

uchg,
User File cannot be changed, renamed, moved,
uchg uchange, Yes All
immutable or removed
uimmutable

sunlnk, File cannot be removed, renamed or


System no-
sunlnk No All mounted on; on macOS this flag needs to
unlink sunlink
be set or cleared from single user mode

User no- uunlnk, File cannot be removed, renamed or


uunlnk Yes Some
unlink uunlink mounted on; not supported by macOS

File is hidden by default in the GUI (but not


Hidden hidden hidden No FreeBSD
in ls)

File is hidden by default in the GUI (but not


User hidden uhidden uhidden Yes FreeBSD
in ls)

File is hidden by default in the GUI (but not


Hidden hidden hidden Yes macOS
in ls)

Tracked tracked tracked Yes macOS File modifications and deletions are tracked

File is protected by System Integrity


Protection; accompanied by the extended
Restricted restricted restricted No macOS attribute com.apple.rootless; flag
needs to be set or cleared from Recovery
Mode
File is HFS-compressed (read-only flag);
Compressed compressed No macOS
not available on APFS-formatted volumes
Hidden privacy flag since macOS Mojave
Data Vault No macOS set by the core system to prohibit any
access without special entitlements

BSD systems offer additional flags like offline, snapshot, sparse,[4] and uarchive; see References.

See also
ATTRIB – analogous command in MS-DOS, OS/2 and Microsoft Windows
chown – change file/directory ownership in a Unix system
chmod – change file access control attributes in a Unix system
cacls – change file access control lists in Microsoft Windows NT

Notes
1. chmod(1) (http://schillix.org/opensolarisman/man1/chmod.1.html) –
illumos and OpenSolaris User Commands Reference Manual from latest Sun based
OpenSolaris
2. ls(1) (http://schillix.org/opensolarisman/man1/ls.1.html) – illumos and
OpenSolaris User Commands Reference Manual from latest Sun based OpenSolaris
3. chflags(1) (https://man.openbsd.org/chflags.1) – OpenBSD General
Commands Manual
4. Different from the APFS sparse files on macOS (https://developer.apple.com/documentation/fo
undation/file_system/about_apple_file_system), which have no special flags.

References
chattr(1) (https://linux.die.net/man/1/chattr) – Linux User Commands
Manual
lsattr(1) (https://linux.die.net/man/1/lsattr) – Linux User Commands
Manual
chflags(1) (https://man.openbsd.org/chflags.1) – OpenBSD General
Commands Manual
chflags(1) (https://www.freebsd.org/cgi/man.cgi?query=chflags&sekti
on=1) – FreeBSD General Commands Manual
chflags(1) (http://netbsd.gw.com/cgi-bin/man-cgi?chflags+1+NetBSD-c
urrent) – NetBSD General Commands Manual
chflags(1) (https://www.unix.com/man-page/mojave/1/chflags) – Darwin
and macOS General Commands Manual (outdated; see newer version (https://www.unix.com/
man-page/osx/1/chflags/))
stat.h (https://github.com/apple/darwin-xnu/blob/master/bsd/sys/stat.h#L466-L519) (flags
section in the BSD system source code of the macOS XNU kernel)

Retrieved from "https://en.wikipedia.org/w/index.php?title=Chattr&oldid=978252664"

This page was last edited on 13 September 2020, at 20:16 (UTC).

Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this
site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia
Foundation, Inc., a non-profit organization.

You might also like