Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

When You Need an ACL

Standard permissions know one owner, one group, and everyone else. That stops working when two accounts from different groups need different access to the same directory, for example a deploy user who writes the site and a web server user who only reads it. An ACL lets you add exactly those entries without opening the directory to everyone.

The tools are in the acl package. If setfacl is missing, install it with sudo apt install acl on Debian and Ubuntu or sudo dnf install acl on AlmaLinux and Rocky Linux. ext4 and XFS support ACLs without extra mount options.

Read ACLs with getfacl

getfacl /var/www/example.com
# file: var/www/example.com
# owner: deploy
# group: deploy
user::rwx
user:www-data:r-x
group::r-x
mask::r-x
other::---

Lines with an empty name are the classic owner, group, and other permissions. user:www-data:r-x is an extra entry. In ls -l, a file or directory with an ACL shows a plus sign after the permission string, such as drwxr-x---+.

Grant Access with setfacl -m

sudo setfacl -m u:www-data:rx /var/www/example.com
sudo setfacl -m g:developers:rwx /srv/project
EntryApplies to
u:deploy:rwxThe user deploy
g:developers:rxThe group developers
o::rEveryone else
m::rxThe mask

Separate several entries with commas. Add -R to apply them to a whole tree, and use a capital X to give execute only to directories and to files that are already executable: setfacl -R -m u:www-data:rX.

Default ACLs for New Files

A normal ACL covers files that exist now. A default ACL on a directory is copied to everything created inside it later:

sudo setfacl -d -m u:www-data:rx /var/www/example.com

Set both: the normal entry for what is there, the default entry for what comes. When a directory has a default ACL, it replaces the umask for new files in it (see umask). New files still get execute only when the program creating them asks for it.

The Mask

The mask is the upper limit for every named user and group entry and for the owning group. If the mask is r-x, an entry of rwx works as r-x; getfacl then marks the line with #effective:r-x. setfacl recalculates the mask when you add entries. Be careful with chmod on a file with an ACL: changing the group bits changes the mask, which can quietly reduce what the named entries allow.

Example: A Deploy User and the Web Server

deploy owns the site and writes it; the web server reads everything and writes only to the uploads directory. On Debian and Ubuntu the web server user is www-data; on AlmaLinux and Rocky Linux it is nginx or apache.

sudo chown -R deploy:deploy /var/www/example.com
sudo setfacl -R -m u:www-data:rX /var/www/example.com
sudo setfacl -R -d -m u:www-data:rX /var/www/example.com
sudo setfacl -R -m u:www-data:rwX /var/www/example.com/uploads
sudo setfacl -R -d -m u:www-data:rwX /var/www/example.com/uploads

Check the result with getfacl on a directory and on a file inside it. Ownership changes are explained in chown recursive. On AlmaLinux and Rocky Linux, SELinux labels are checked as well; an ACL does not change them.

Remove ACL Entries

  • One entry: sudo setfacl -x u:www-data /var/www/example.com.
  • All default entries: sudo setfacl -k /var/www/example.com.
  • Every ACL entry: sudo setfacl -b /var/www/example.com; the classic permissions stay.

Copies and Backups

ACLs are lost when a copy tool does not carry them. cp -a keeps them, rsync needs -A, and tar needs --acls. To save the ACLs of a tree on their own, write them to a file that only root can read:

sudo sh -c 'getfacl -R /var/www > /root/acl-backup.txt'

getfacl stores the paths without the leading slash, so restore from the root directory: cd / && sudo setfacl --restore=/root/acl-backup.txt.

Was this answer helpful?
Loading...