Mastering Linux Access Control Lists (ACL) with setfacl and getfacl

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

Context & Why Standard Permissions Fall Short

Traditional Linux file permissions rely on the classic UGO scheme: User (owner), Group, and Others. Each tier gets standard read (r), write (w), and execute (x) flags. That baseline works fine for isolated workloads or basic web roots. Put five engineers and two external contractors on the same codebase, however, and the model buckles.

Take a real-world scenario from our staging environment. A deployment directory is owned by deployer:devs. An external security consultant joins for a two-week audit. She needs write access to /var/www/shared/reports/, but must never read adjacent secrets in /var/www/shared/keys/. At the same time, Prometheus node exporters need read-only visibility into application health logs across the tree without exposing those files to every unprivileged user on the box.

Classic permissions leave you with bad compromises. You either create dozens of brittle supplemental groups or slap chmod 777 on shared folders out of frustration. POSIX Access Control Lists (ACLs) fix this design bottleneck. They let you pin explicit permissions to arbitrary users and groups directly on any inode, leaving base ownership intact.

Installation & Filesystem Support

Modern Linux kernels enable ACL support by default on ext4, XFS, and Btrfs filesystems. What often goes missing—especially inside lean cloud images or container hosts—are the userspace management utilities: setfacl and getfacl.

Install the package on Debian, Ubuntu, or Raspberry Pi OS:

sudo apt update && sudo apt install -y acl

On RHEL, AlmaLinux, Rocky Linux, or Fedora:

sudo dnf install -y acl

Check whether your active ext4 mount has ACL support compiled into its default superblock attributes:

tune2fs -l /dev/sda1 | grep "Default mount options:"

If you see user_xattr acl, you are ready. XFS partitions handle ACLs natively and do not need extra flags. On older distributions like CentOS 6 or legacy Debian installs where the filesystem mounted without ACL support, add the acl option to /etc/fstab and remount the target path:

sudo mount -o remount,acl /data

Configuring Access with setfacl

Managing ACLs comes down to four core concepts: individual user entries, secondary group entries, the effective rights mask, and default inheritance rules.

1. Granting Access to Specific Users and Groups

Apply the -m (modify) switch to add or update rules. Syntax follows u:username:permissions for user accounts and g:groupname:permissions for group accounts.

# Grant user 'sarah' read and write access to a sensitive environment file
sudo setfacl -m u:sarah:rw- /var/www/shared/app.env

# Grant the 'auditors' group read-and-execute rights on the application logs
sudo setfacl -m g:auditors:r-x /var/log/app/

Neither Sarah nor the auditors need their primary Unix groups changed. Their new permissions apply immediately without requiring a shell logout.

2. Managing the Effective Rights Mask

The moment you add a named user or secondary group to a file, Linux calculates an ACL mask. Think of the mask as an absolute ceiling. It caps the maximum effective permissions available to all named users, named groups, and the file’s primary group. The owning user and ‘others’ are exempt.

If Sarah has rwx in her user entry, but the mask drops to r--, her actual usable permission drops to read-only.

# Cap maximum effective permissions on app.env at read-only
sudo setfacl -m m::r-- /var/www/shared/app.env

Run getfacl after setting this. The utility marks restricted lines with an explicit indicator like #effective:r-- so you catch unintended lockdowns right away.

3. Default ACLs for Directories (Inheritance)

Shared folders frequently suffer from permission drift. Developer A runs a script with a tight umask 0077, and suddenly Developer B cannot open the resulting build artifacts. Default ACLs solve this problem permanently. Every new file and folder created inside an ACL-enabled directory inherits designated permissions on creation.

Prefix the rule with d: or pass the -d flag:

# Ensure contractor 'alex' automatically receives read-write rights on all new assets
sudo setfacl -d -m u:alex:rw- /var/www/projects/

# Apply access to the root directory itself (defaults only affect future children)
sudo setfacl -m u:alex:rwx /var/www/projects/

Switching to default ACLs pays immediate dividends. On a media processing node handling roughly 420,000 incoming uploads daily, we retired a messy five-minute cron job running chown -R. Disk I/O dropped from 94% spikes to near zero, and locked file write collisions stopped entirely.

4. Removing ACL Entries

Revoking temporary access takes a single command. Strip individual user or group assignments with -x, or wipe all extended rules using -b.

# Strip Sarah's custom rule
sudo setfacl -x u:sarah /var/www/shared/app.env

# Wipe all extended ACL entries and fall back to baseline chmod values
sudo setfacl -b /var/www/shared/app.env

Verification, Auditing, and Backups

Always verify rules after applying them. A misconfigured mask can silently block access or leak unexpected privileges.

Identifying Extended Permissions with ls

Run standard file listings to spot ACLs at a glance:

ls -l /var/www/shared/app.env

Sample output:

-rw-rwxr--+ 1 deployer devs 2480 May 12 10:30 /var/www/shared/app.env

Notice the plus sign (+) at the end of the mode string. That flag confirms extended ACL attributes exist on this inode. Be careful when reading the middle three characters (rwx): on files with extended ACLs, ls -l displays the active mask in that slot, not the owning group’s baseline permission.

Inspecting Granular Rules with getfacl

Inspect the complete permission matrix with getfacl:

getfacl /var/www/shared/app.env

Sample output:

# file: var/www/shared/app.env
# owner: deployer
# group: devs
user::rw-
user:sarah:rw-
group::r--
group:auditors:r--
mask::rw-
other::---

Backing Up and Restoring ACL Tables

Standard tar archives or rsync passes without specific flags (-A / --acls) will drop extended attributes during migrations. Export your permissions into an audit table before major maintenance:

# Dump all ACL definitions under /var/www to an audit snapshot
sudo getfacl -R /var/www > /root/var_www_acls.bak

# Restore permissions across the entire tree from the text dump
sudo setfacl --restore=/root/var_www_acls.bak

Text-based ACL backups are human-readable, track cleanly in Git, and let you restore complex enterprise permission structures across tens of thousands of files in under three seconds.

Share: