# Chattr

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Chattr
> Markdown URL: https://mediated.wiki/source/Chattr.md
> Source: https://en.wikipedia.org/wiki/Chattr
> Source revision: 1353506963
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Command in Linux}}
{{Lowercase title}}
{{Distinguish|Chatr}}
{{Infobox software
| name                   = chattr
| logo                   = 
| screenshot             = 
| screenshot size        = 
| caption                = 
| author                 = Remy Card
| developer              = 
| released               = 
| latest release version = 
| latest release date    = 
| operating system       = [Linux](/source/Linux)
| platform               = [Cross-platform](/source/Cross-platform)
| genre                  = [Command](/source/Command_(computing))
| license                = 
| website                = 
}}
{{Infobox software
| name                   = chflags
| logo                   = 
| screenshot             = 
| screenshot size        = 
| caption                = 
| author                 = 
| developer              = 
| released               = 
| latest release version = 
| latest release date    = 
| operating system       = [BSD-like systems](/source/Berkeley_Software_Distribution), including [macOS](/source/macOS)
| platform               = [Cross-platform](/source/Cross-platform)
| genre                  = [Command](/source/Command_(computing))
| license                = 
| website                = 
}}
'''{{mono|chattr}}''' is the [command](/source/command_(computing)) in [Linux](/source/Linux) that allows a user to set certain [attributes](/source/file_attribute) of a file. {{mono|lsattr}} is the command that displays the attributes of a file.

Most [BSD-like systems](/source/Berkeley_Software_Distribution), including [macOS](/source/macOS), have always had an analogous '''{{mono|chflags}}''' command to set the attributes, but no command specifically meant to display them; specific options to the [{{mono|ls}}](/source/ls) command are used instead. The chflags command first appeared in [4.4BSD](/source/4.4BSD).

[Solaris](/source/Solaris_(operating_system)) has no commands specifically meant to manipulate them. [{{mono|chmod}}](/source/chmod)<ref name="chmod man OpenSolaris">{{man|1|chmod|OpenSolaris}}</ref> and [{{mono|ls}}](/source/ls)<ref name="ls man OpenSolaris">{{man|1|ls|OpenSolaris}}</ref> are used instead.

Other [Unix-like](/source/Unix-like) operating systems, in general, have no analogous commands. The similar-sounding commands {{mono|chatr}} (from [HP-UX](/source/HP-UX)) and {{mono|lsattr}} (from [AIX](/source/IBM_AIX)) exist but have unrelated functions.

Among other things, the {{mono|chattr}} command is useful to make files immutable so that password files and certain system files cannot be erased during software upgrades.<ref name="chattr man openbsd">{{man|1|chflags|OpenBSD}}</ref>

== In Linux systems ({{mono|chattr}} and {{mono|lsattr}}) ==

=== File system support ===

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

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

=== {{mono|chattr}} description ===
The form of the {{mono|chattr}} command is:

<syntaxhighlight lang="bash">
 chattr [-RVf] [-+=AacDdijsTtSu] [-v version] files...
</syntaxhighlight>

* <code>-R</code> recursively changes attributes of directories and their contents
* <code>-V</code> is to be verbose and print the program version
* <code>-f</code> suppresses most error messages

=== {{mono|lsattr}} description ===
The form of the {{mono|lsattr}} command (gnu 1.41.3):

<syntaxhighlight lang="bash">
 lsattr [ -RVadv ] [ files...  ]
</syntaxhighlight>

* <code>-R</code> recursively lists attributes of directories and their contents
* <code>-V</code> displays the program version
* <code>-a</code> lists all files in directories, including [dotfiles](/source/Hidden_file_and_hidden_directory)
* <code>-d</code> lists directories like other files, rather than listing their contents

=== Attributes ===
Some attributes include:

{| class="wikitable sortable" style="margin-left:auto; margin-right:auto"
|+ File attributes on a Linux file system according to the <code>chattr(1)</code> Linux man page
|-
!Attribute
!{{mono|lsattr}} flag
!{{mono|chattr}} option
!Semantics and rationale
|-
!No <code>atime</code> updates
|style="text-align:center" |<code>A</code>
|<code>+A</code> to set<br /><code>-A</code> to clear
|
*When a file with the <code>A</code> attribute set is accessed, its [{{mono|atime}} record](/source/stat_(Unix)) is not modified.
*This avoids a certain amount of disk I/O operations.
|-
!Append only
|style="text-align:center" |<code>a</code>
|<code>+a</code> to set<br /><code>-a</code> to clear<ref name=cap_immutable group=note>Only the superuser or a process possessing the <code>CAP_LINUX_IMMUTABLE</code> capability can set or clear these attributes.</ref>
|
*A file with the <code>a</code> attribute set can only be open in append mode for writing.
|-
!Compressed
|style="text-align:center" |<code>c</code>
|<code>+c</code> to set<br /><code>-c</code> to clear<ref name=not_honored_ext23 group=note>These attributes are not honored by the {{mono|[ext2](/source/ext2)}} and {{mono|[ext3](/source/ext3)}} filesystems as implemented in the current mainline Linux kernels.</ref>
|
*A file with the <code>c</code> 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.
|-
!No Copy-on-Write (CoW)
|style="text-align:center" |<code>C</code>
|<code>+C</code> to set<br /><code>-C</code> to clear<ref name=cow_only group=note>These attributes only make sense for Copy-on-Write file-systems such as {{mono|[btrfs](/source/btrfs)}}.</ref>
|
*A file with the <code>C</code> attribute will not be subject to Copy-on-Write updates.
*Updates to these files may not be subject to atomic snapshots, and may lack some reliability information on some filesystems and kernels.
|-
|-
!Synchronous directory updates
|style="text-align:center" |<code>D</code>
|<code>+D</code> to set<br /><code>-D</code> to clear
|
*When a directory with the <code>D</code> attribute set is modified, the changes are written synchronously on the disk
*This is equivalent to the <code>dirsync</code> [mount option](/source/Mount_(Unix)), applied to a subset of the files.
|-
!No dump
|style="text-align:center" |<code>d</code>
|<code>+d</code> to set<br /><code>-d</code> to clear
|
*A file with the <code>d</code> attribute set is not candidate for backup when the [{{mono|dump}} program](/source/dump_(Unix)) is run.
|-
!Compression error
|style="text-align:center" |<code>E</code>
| {{n/a|''(unavailable)''}}
|
*The <code>E</code> attribute is used by the experimental compression patches to indicate that a compressed file has a compression error.
|-
!Extent format
|style="text-align:center" |<code>e</code>
| {{n/a|''(unavailable)''}}
|
*The <code>e</code> attribute indicates that the file is using [extents](/source/Extent_(file_systems)) for mapping the blocks on disk.
|-
!Huge file
|style="text-align:center" |<code>h</code>
| {{n/a|''(unavailable)''}}
|
*The <code>h</code> 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.
|-
!Indexed directory
|style="text-align:center" |<code>I</code>
| {{n/a|''(unavailable)''}}
|
*The <code>I</code> attribute is used by the [{{mono|htree}} program](/source/HTree) code to indicate that a directory is being indexed using hashed trees.
|-
!Immutable
|style="text-align:center" |<code>i</code>
|<code>+i</code> to set<br /><code>-i</code> to clear<ref name=cap_immutable group=note />
|
*A file with the <code>i</code> 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 journaling
|style="text-align:center" |<code>j</code>
|<code>+j</code> to set<br /><code>-j</code> to clear<ref name=cap_sysres group=note>Only the superuser or a process possessing the <code>CAP_SYS_RESOURCE</code> capability can set or clear this attribute.</ref>
|
*A file with the <code>j</code> attribute has all of its data written to the {{mono|[ext3](/source/ext3)}} journal before being written to the file itself, if the filesystem is mounted with the <code>"data=ordered"</code> or <code>"data=writeback"</code> options.
*When the filesystem is mounted with the <code>"data=journal"</code> option all file data is already [journaled](/source/Journaling_file_system), so this attribute has no effect.
|-
!Secure deletion
|style="text-align:center" |<code>s</code>
|<code>+s</code> to set<br /><code>-s</code> to clear<ref name=not_honored_ext23 group=note /><ref name=not_honored_ext4 group=note>This attribute is not honored by the {{mono|[ext4](/source/ext4)}} filesystem as implemented in the current mainline Linux kernels [https://bugzilla.kernel.org/show_bug.cgi?id=17872 as reported in Bug #17872] {{Webarchive|url=https://web.archive.org/web/20150130192356/https://bugzilla.kernel.org/show_bug.cgi?id=17872 |date=2015-01-30 }}.</ref>
|
*When a file with the <code>s</code> attribute set is deleted, [its blocks are zeroed](/source/Data_remanence) and written back to the disk.
|-
!Synchronous updates
|style="text-align:center" |<code>S</code>
|<code>+S</code> to set<br /><code>-S</code> to clear
|
*When a file with the <code>S</code> 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 <code>sync</code> [mount option](/source/Mount_(Unix)), applied to a subset of the files.
|-
!Top of directory hierarchy
|style="text-align:center" |<code>T</code>
|<code>+T</code> to set<br /><code>-T</code> to clear
|
*A directory with the <code>T</code> attribute will be deemed to be the top of directory hierarchies for the purposes of the [Orlov block allocator](/source/Orlov_block_allocator).
*This is a hint to the block allocator used by {{mono|[ext3](/source/ext3)}} and {{mono|[ext4](/source/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 <code>T</code> attribute on the <code>/home</code> directory, so that <code>/home/john</code> and <code>/home/mary</code> 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
|style="text-align:center" |<code>t</code>
|<code>+t</code> to set<br /><code>-t</code> to clear
|
*For those filesystems that support [tail-merging](/source/Block_suballocation), a file with the <code>t</code> 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](/source/LILO_(boot_loader)), which reads the filesystem directly and doesn't understand tail-merged files.
|-
!Undeletable
|style="text-align:center" |<code>u</code>
|<code>+u</code> to set<br /><code>-u</code> to clear<ref name=not_honored_ext23 group=note />
|
*When a file with the <code>u</code> attribute set is deleted, its contents are saved.
*This allows the user to ask for its [undeletion](/source/undeletion).
|-
!Compression raw access
|style="text-align:center" |<code>X</code>
| {{n/a|''(unavailable)''}}
|
*The <code>X</code> 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
|style="text-align:center" |<code>Z</code>
| {{n/a|''(unavailable)''}}
|
*The <code>Z</code> attribute is used by the experimental compression patches to indicate a compressed file is "dirty".
|-
!Version / generation number
|style="text-align:center" |<code>-v</code>
|style="text-align:center" |<code>-v ''version''</code>
|
*File's version/generation number.
|-
|}

==== Notes ====

{{reflist|group=note}}

== In BSD-like systems ({{mono|chflags}}) ==

=== File system support ===

The {{mono|chflags}} command is not specific to particular file systems. [UFS](/source/Unix_File_System) on BSD systems, and [APFS](/source/APFS), [HFS+](/source/HFS%2B), [SMB](/source/Server_Message_Block), [AFP](/source/Apple_Filing_Protocol), and [FAT](/source/File_Allocation_Table) on macOS support at least some flags.

=== {{mono|chflags}} description ===
The form of the {{mono|chflags}} command is:
<syntaxhighlight lang="bash">
 chflags [-R [-H | -L | -P]] flags file ...
</syntaxhighlight>
* <code>-H</code> If the -R option is specified, symbolic links on the command line are followed.  (Symbolic links encountered in the tree traversal are not followed.)
* <code>-L</code> If the -R option is specified, all symbolic links are followed.
* <code>-P</code> If the -R option is specified, no symbolic links are followed. This is the default.
* <code>-R</code> 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 [{{mono|ls}} command](/source/ls) will do with either the <code>-lo</code>, or the <code>-lO</code>, 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:

{| class="wikitable sortable" style="margin-left:auto; margin-right:auto"
|+ '''File attributes'''
|-
!Attribute
!{{mono|ls}} flag
!{{mono|chflags}} flag
!Owner-settable
!OS support
!Semantics and rationale
|-
!Archived
|{{mono|arch}}
|{{mono|arch}}, {{mono|archived}}
|{{No}}
|All
|File is archived
|-
!Opaque
|{{mono|opaque}}
|{{mono|opaque}}
|{{Yes}}
|All
|Directory is opaque when viewed through a union mount
|-
!No dump
|{{mono|nodump}}
|{{mono|nodump}}
|{{Yes}}
|All
|File cannot be dumped
|-
!System append-only
|{{mono|sappnd}}
|{{mono|sappnd}}, {{mono|sappend}}
|{{No}}
|All
|Existing data in the file can't be overwritten and the file cannot be truncated
|-
!User append-only
|{{mono|uappnd}}
|{{mono|uappnd}}, {{mono|uappend}}
|{{Yes}}
|All
|Existing data in the file can't be overwritten and the file cannot be truncated
|-
!System immutable
|{{mono|schg}}
|{{mono|schg}}, {{mono|schange}}, {{mono|simmutable}}
|{{No}}
|All
|File cannot be changed, renamed, moved, or removed
|-
!User immutable
|{{mono|uchg}}
|{{mono|uchg}}, {{mono|uchange}}, {{mono|uimmutable}}
|{{Yes}}
|All
|File cannot be changed, renamed, moved, or removed
|-
!System no-unlink
|{{mono|sunlnk}}
|{{mono|sunlnk, sunlink}}
|{{No}}
|FreeBSD, DragonFly BSD, macOS
|File cannot be removed, renamed or mounted on; on macOS this flag needs to be set or cleared from [single user mode](/source/single_user_mode)
|-
!User no-unlink
|{{mono|uunlnk}}
|{{mono|uunlnk, uunlink}}
|{{Yes}}
|FreeBSD, DragonFly BSD
|File cannot be removed, renamed or mounted on
|-
!Hidden
|{{mono|hidden}}
|{{mono|hidden}}
|{{Yes}}
|macOS
|File is hidden by default in the GUI (but not in {{mono|ls}})
|-
!Hidden
|{{mono|hidden}}
|{{mono|hidden, uhidden}}
|{{Yes}}
|FreeBSD
|File might be hidden by default in some GUI desktops (but not in {{mono|ls}})
|-
!Tracked
|{{mono|tracked}}
|{{mono|tracked}}
|{{Yes}}
|macOS
|File modifications and deletions are tracked
|-
!Restricted
|{{mono|restricted}}
|{{mono|restricted}}
|{{No}}
|macOS
|File is protected by [System Integrity Protection](/source/System_Integrity_Protection); accompanied by the [extended attribute](/source/Extended_file_attributes) {{mono|com.apple.rootless}}; flag needs to be set or cleared from Recovery Mode
|-
!Compressed
|{{mono|compressed}}
|{{mono|}}
|{{No}}
|macOS
|File is [HFS](/source/HFS_Plus)-compressed (read-only flag); not available on [APFS](/source/APFS)-formatted volumes
|-
!Data Vault
|{{mono|}}
|{{mono|}}
|{{No}}
|macOS
|Hidden privacy flag since [macOS Mojave](/source/macOS_Mojave) set by the core system to prohibit any access without special entitlements
|-
!Offline
|{{mono|offline}}
|{{mono|offline, uoffline}}
|{{Yes}}
|FreeBSD
|File is offline
|-
!Snapshot
|{{mono|snapshot}}
|
|{{No}}
|FreeBSD, NetBSD
|File is a snapshot file (read-only flag)
|-
!Sparse
|{{mono|sparse}}
|{{mono|sparse, usparse}}
|{{Yes}}
|FreeBSD
|Writes of all zeroes may be written as "holes"
|-
!Must be archived
|{{mono|uarch}}
|{{mono|uarch, uarchive}}
|{{Yes}}
|FreeBSD
|File must be archived
|-
|}

== See also ==
* <code>[ATTRIB](/source/ATTRIB)</code> – analogous command in MS-DOS, OS/2 and Microsoft Windows
* <code>[chown](/source/chown)</code> – change file/directory ownership in a Unix system
* <code>[chmod](/source/chmod)</code> – change file access control attributes in a Unix system
* <code>[cacls](/source/cacls)</code> – change file access control lists in Microsoft Windows NT

== Notes ==

{{reflist}}

==References==

*{{man|1|chattr|die.net}}
*{{man|1|lsattr|die.net}}
*{{man|1|chflags|OpenBSD}}
*{{man|1|chflags|FreeBSD}}
*{{man|1|chflags|NetBSD}}
*{{man|1|chflags|Darwin}} (outdated; see [https://www.unix.com/man-page/osx/1/chflags/ newer version])
*[https://github.com/apple/darwin-xnu/blob/master/bsd/sys/stat.h#L466-L519 {{mono|stat.h}}] (flags section in the BSD system source code of the macOS [XNU](/source/XNU) kernel) 

{{Unix commands}}

Category:Unix file system-related software
Category:Linux-only free software

---
Adapted from the Wikipedia article [Chattr](https://en.wikipedia.org/wiki/Chattr) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Chattr?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
