linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Simulator
  • Knowledge base
  • Interview prep
Index
Categories
All entries
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
home/linux/kb/File system/extended-attributes

kb/filesystem ── File system ── advanced

Extended attributes (xattr): arbitrary file metadata

xattr are key-value metadata on an inode beyond stat. 4 namespaces: user (open), trusted (root), system (ACL), security (SELinux, capabilities). getfattr reads, setfattr writes.

view as markdownaka: xattr, extended-attributes, getfattr, setfattr, user-xattr, security-xattr

Why xattr

A standard stat() gives the inode a fixed set of fields: size, owners, mtime, permissions. Sometimes you want to store something else next to the file: a signature, a hash, a tag, a mime-type, the Origin URL of a download. xattr is the standard mechanism for that.

Classic user xattr:

  • user.mime-type for GNOME / KDE icons
  • user.charset for apache mod_mime on static content
  • user.com.dropbox.attributes for Dropbox markers
  • user.checksum.md5 for custom hashes

System xattr are used heavily by the kernel:

  • security.selinux is the SELinux label ("user_u:object_r:..._t")
  • security.capability holds file capabilities (cap_net_bind_service)
  • system.posix_acl_access is where [[posix-acl|POSIX ACL]] are stored
  • system.posix_acl_default is the default ACL of a directory

Namespaces

NamespaceWho writesWho readsWhat
user.*anyone with write on the filethe owner and similarfree-form application data
trusted.*root only (CAP_SYS_ADMIN)rootsystem tools, FUSE drivers
system.*the kernelthe kernelACL and similar structures
security.*LSM (SELinux, AppArmor, IMA)LSMsecurity labels and capabilities

Limits:

  • Name ≤ 255 bytes
  • Value ≤ 64 KiB on plain ext4 without -O ea_inode (for larger values, a separate block)
  • Total per filesystem depends heavily on inode size; xfs with -i size=1024 fits more inline

getfattr, reading

bash
$ getfattr -d file.txt              # all user.* (the default)
# file: file.txt
user.mime-type="text/plain"
$ getfattr -d -m '.*' file.txt      # all namespaces (root needed for security/trusted)
# file: file.txt
user.mime-type="text/plain"
security.selinux="unconfined_u:object_r:user_home_t:s0"
$ getfattr -n user.mime-type file.txt
# file: file.txt
user.mime-type="text/plain"
$ getfattr -n security.capability /usr/bin/ping
# file: /usr/bin/ping
security.capability=0sAQAAAgAgAAAAAAAAAAAAAAAAAAAA

Options:

OptionWhat
-ddump, all attributes
-n NAMEa specific one
-m PATTERNregex for the name; default ^user\.
-hif a symlink, do not follow
-Rrecursive
--only-valuesvalue only, no header

setfattr, writing

bash
setfattr -n user.tag -v "important" file.txt
setfattr -x user.tag file.txt          # remove
setfattr -n user.json -v "$(cat data.json)" file

Binary values (hex):

bash
setfattr -n user.bin -v 0xdeadbeef file.txt

Real uses

File capabilities

Linux capabilities split root privileges into about 40 bits. A security.capability xattr on a binary gives it only what it needs:

bash
# Let ping send ICMP without setuid
setcap cap_net_raw+ep /usr/bin/ping
getcap /usr/bin/ping       # a frontend over getfattr
# The same directly
getfattr -n security.capability /usr/bin/ping

This is safer than setuid. See capabilities for more.

SELinux labels

bash
$ getfattr -n security.selinux /etc/passwd
# file: etc/passwd
security.selinux="system_u:object_r:passwd_file_t:s0"

With cp and no --preserve=context, the SELinux label is lost and the default for the target directory is applied instead. On a production server this is a common cause of "the file copied, but the program can't read it".

ACL

POSIX ACL are physically stored as xattr:

bash
$ getfattr -m '^system' file.txt
system.posix_acl_access=0sAgAAAAEABwD/...

Do not edit them directly. Use [[posix-acl|setfacl]].

IMA / EVM

Integrity Measurement Architecture stores signatures in security.ima and security.evm. It is used on hardened systems for measured boot and runtime integrity.

xattr and cp / tar / rsync

By default they are not carried over. This is the most common bug:

  • cp -a (archive) carries xattr
  • cp without flags does not carry them
  • tar needs --xattrs (and --xattrs-include='*.*' to capture every namespace, otherwise only user.*)
  • rsync needs -X (--xattrs); also add -A for ACL

Copy the SELinux context with cp:

bash
cp --preserve=context file new
# or for a whole tree
cp -a /src /dst         # -a implies -p with all attributes

Filesystem support

Filesystemxattr
ext4yes; for large values -O ea_inode
xfsyes; fit in a large inode
btrfsyes
tmpfsyes (kernel >= 6.0 for the security namespace)
nfsNFSv4, partial
fat/vfatno
iso9660no (older RR extension, partial)

With mount -o nouser_xattr the user namespace is off, the rest still work.

Size and performance

  • In the space reserved inside the inode, it is free (part of the RAM/IO for the inode)
  • Large values go to a separate block = +1 IO on read
  • With very many xattr on one file you get a noticeable ls -l slowdown, because the kernel may check each one

When something goes wrong

  • Operation not supported means the mount has no user_xattr, or the filesystem can't do it (vfat). On ext this has been the default for a long time, but thin FUSE mounts may not support it.
  • Permission denied on setfattr -n trusted.* or security.* means you need root and the matching capability (CAP_SYS_ADMIN for trusted).
  • xattr vanished after cp/tar/rsync means the right flag was missing.
  • SELinux denied after a copy means the labels were not copied. restorecon -Rv restores the defaults for the target directory.
  • No space left with free disk means ext4 may have hit the limit on ea blocks. tune2fs -l | grep -i ea. You may need -O ea_inode (which requires a remkfs).
  • Argument list too long means the name or value exceeded the limits.

Alternatives

  • Resource forks (macOS) are a similar idea, not carried into Linux
  • A side-car file (file.metadata.json) is simple and copies through cp/scp without flags, but it can drift out of sync
  • A database if there is a lot of metadata and it is queried often
  • xfs project IDs for subtree quotas, not for arbitrary tags

§ команды

bash
getfattr -d -m '.*' /etc/passwd

All xattr of a file including security/system, root needed

bash
setfattr -n user.tag -v 'review' file.txt

Set a user tag, the simple case

bash
setfattr -x user.tag file.txt

Remove a specific xattr, the whole entry, not just its value

bash
getcap /usr/bin/ping

File capabilities (via the security.capability xattr), what the binary may do without setuid

bash
rsync -aXA /src/ /dst/

Copy with xattr (-X) and ACL (-A), otherwise both are lost

bash
tar --xattrs --xattrs-include='*.*' -cf backup.tar /etc

tar carrying ALL xattr namespaces (the default is only user.*)

bash
find / -xdev -exec getfattr -h -d -m security {} \; 2>/dev/null | head

Find every file with a security xattr on the root filesystem, an SELinux/IMA audit

§ см. также

  • posix-aclPOSIX ACL: extended access permissionsPOSIX ACL extends the classic rwx permissions: you can grant access to many users and groups without reshuffling the owner and group. setfacl writes, getfacl reads. A default ACL on a directory is inherited by its children.
  • file-permissionsFile permissions: rwx and chmodEvery file has three permission sets: for the owner, the group, and others. Each set is three bits: read (r), write (w), execute (x). You change them with `chmod`.
  • capabilitiesLinux capabilities: privilege bitsCapabilities split root's power into 40+ independent bits: NET_ADMIN, SYS_PTRACE, and so on. You can grant a process a slice of that power without making it full root.
  • selinux-apparmorSELinux and AppArmor: Mandatory Access ControlSELinux and AppArmor are MAC: a control layer on top of normal permissions. They stop a process from doing anything outside its profile or type.
  • inodeInodeAn inode is a filesystem record that holds metadata and pointers to a file's data blocks. The filename lives separately, in a directory, and simply points to the inode.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies