TExcelTAD
Home › Linux Server Administration › Shared Directories: setgid & ACLs

Shared Directories: setgid & ACLs

A common server request: "the deploy user uploads releases to /srv/app, the app user must read them, the web server must read the static files, and nobody else should." Plain owner/group/other bits (The Permissions Model) give you one owner and one group, which is rarely enough. Two tools close the gap: the setgid bit on directories and POSIX ACLs.

setgid directories: a shared group that sticks

Normally a new file belongs to the creator's primary group. On a directory with the setgid bit (chmod g+s, the s in drwxrwsr-x), new files and subdirectories inherit the directory's group instead, and new subdirectories inherit the setgid bit too:

bash
# Sample session (root): a team directory for the deploy group
$ sudo groupadd deployers
$ sudo usermod -aG deployers ada && sudo usermod -aG deployers ben
$ sudo install -d -o root -g deployers -m 2775 /srv/releases
$ ls -ld /srv/releases
drwxrwsr-x 2 root deployers 4096 Jan 15 10:00 /srv/releases
$ sudo -u ada touch /srv/releases/v1.4.0.tar.gz
$ ls -l /srv/releases
-rw-r--r-- 1 ada deployers 0 Jan 15 10:01 v1.4.0.tar.gz

The file is in deployers, so Ben can read it. But with the default umask 022 it is rw-r--r--: Ben can't overwrite it. Either the team uses umask 002 (in their shell profile, or UMask=0002 in a service unit), or you use default ACLs, which don't depend on anybody's umask.

ACLs: permissions for more than one user or group

An access control list adds entries such as "user www-data may read" to a file, on top of the classic bits. setfacl changes them, getfacl shows them, and ls -l marks such files with a +. ACLs work on the usual server filesystems (ext4, XFS, Btrfs) without special mount options. A user can set ACLs on files they own, so this session is real:

bash · try it yourself
mkdir -p site/static && echo "body{}" > site/static/app.css
chmod 750 site
setfacl -m u:www-data:rx site                 # the web server may enter and list
setfacl -R -m u:www-data:rX site/static       # and read everything below (X: x only on directories)
ls -ld site | cut -c1-11
getfacl -p site
getfacl -p site/static/app.css | grep www-data
drwxr-x---+
# file: site
# owner: learner
# group: learner
user::rwx
user:www-data:r-x
group::r-x
mask::r-x
other::---

user:www-data:r--

Two details in that output:

  • mask::r-x is the upper limit for every named entry and the group entry. chmod g-x site would lower the mask and silently take x away from www-data too; that is the most common "the ACL is set but it doesn't work" surprise.
  • X (capital) means "execute only on directories (or files that already have an x bit)", so -R doesn't make every CSS file executable.

Default ACLs: inheritance for new files

The ACL above only covers files that exist now. A default ACL on a directory (setfacl -d) is copied to every file and directory created inside it later, regardless of the creator's umask:

bash · try it yourself
mkdir uploads
setfacl -d -m u:www-data:rX uploads           # default: www-data may read new entries
setfacl -d -m g::rwX uploads                  # default: the group may write
umask 022
echo "release notes" > uploads/notes.txt      # created after the default ACL
mkdir uploads/v2
getfacl -p uploads | grep default
echo "--- a new file:"
getfacl -p uploads/notes.txt | grep -v '^#'
echo "--- a new directory inherits the defaults too:"
getfacl -p uploads/v2 | grep -c default
default:user::rwx
default:user:www-data:r-x
default:group::rwx
default:mask::rwx
default:other::r-x
--- a new file:
user::rw-
user:www-data:r-x	#effective:r--
group::rwx	#effective:rw-
mask::rw-
other::r--

--- a new directory inherits the defaults too:
5

Read the new file's entries closely:

  • www-data got read access without anyone running setfacl on the file.
  • The group can write (group::rwx #effective:rw-), although the umask was 022. When a directory has a default ACL, the umask is ignored; the mode the program asked for (666 for a new file) becomes the mask instead.
  • #effective: shows what an entry is really worth after the mask: no x on a regular file, because the creating program didn't ask for it. Default ACLs decide who gets access; they never add rights the creator didn't request.

That is the property that makes default ACLs better than "everybody please set umask 002": it works no matter which user, tool or service creates the file.

Copying, backing up and auditing ACLs

Task Command
Save all ACLs below a directory getfacl -R -p /srv/app > acl-backup.txt
Restore them setfacl --restore=acl-backup.txt
Remove all ACL entries setfacl -b file (-k removes only the default ACL)
Copy with ACLs cp -a, rsync -aAX (-A ACLs, -X extended attributes), tar --acls
Find files with ACLs getfacl -R -s -p /srv 2>/dev/null | sed -n 's/^# file: //p'

rsync -a alone does not copy ACLs, so a restored backup can work "almost": the files are there, but the web server gets 403 Forbidden. Backups with rsync uses -aAX for that reason.

Test yourself

What does the setgid bit do on a directory?

  1. Runs files with the group's rights
  2. New files and subdirectories get the directory's group instead of the creator's primary group
  3. Prevents deleting other users' files
  4. Makes the directory read-only

An ACL grants user:www-data:rwx, but getfacl shows #effective:r-x. Why?

  1. www-data is not a real user
  2. The ACL mask is r-x and limits every named entry
  3. ACLs don't support w
  4. The file system is read-only

Which rsync options preserve ACLs and extended attributes?

  1. -a
  2. -aAX
  3. -z
  4. --delete