Permission problems on Linux usually come from not knowing which rule the kernel checked first. A file shows rwx for a group, getfacl lists the grant, and the user still gets “Permission denied.” This guide shows how mode bits, chown, ACL masks, umask, and the special bits combine into one access decision. It then works through two practical setups: a shared team directory (setgid plus a default ACL) and a systemd service that writes into a shared path (UMask=).
The short answer: Linux checks owner, then named ACL users, then groups (owning plus named, all capped by the ACL mask), then “other,” and only the first matching category counts. For shared folders, combine a setgid directory with a default ACL. For services, set UMask= in the systemd unit instead of relying on a shell umask. SUID exploitation and sudoers auditing are covered in TBRT’s Linux privilege escalation hardening guide.
What Are Linux File Permissions?
Every file and directory has an owner, an owning group, and a mode. The mode holds three permission triads: owner, group, and other. Each triad combines read (r), write (w), and execute (x). On top of that sit three special bits (setuid, setgid, sticky). There’s also an optional POSIX Access Control List (ACL) for extra named users and groups.
Run ls -ld and stat on a file and a directory:
ls -ld /srv/data/report.csv /srv/data/projects
stat -c '%A %a %U:%G %n' /srv/data/report.csv
Example output (names, sizes, and dates will differ on your system):
-rw-r----- 1 alice devteam 2048 Oct 27 09:14 /srv/data/report.csv
drwxr-x--- 2 alice devteam 4096 Oct 27 09:14 /srv/data/projects
-rw-r----- 640 alice:devteam /srv/data/report.csv
The first character is the type (- for a file, d for a directory). Next come the three triads. stat turns rw-r----- into octal 640: owner 6 (r+w), group 4 (r), other 0. A trailing + after the mode (for example -rw-rw----+) means the file has an extended ACL.
The Problem It Solves
The classic three-tier model knows only one owner and one group. Real systems need more. Two teams share a folder. A backup user needs read-only access. A web service writes files that a deploy user must later edit. POSIX ACLs, setgid directories, and a deliberate umask fill those gaps without handing out root or chmod 777.
Key Features
Mode Bits and chmod (Numeric, Symbolic, and Capital X)
chmod sets permissions in two ways. Numeric mode sets everything at once: chmod 750 dir. Symbolic mode makes relative changes: chmod u+rw,g-w,o= file.
Capital X is the flag that saves recursive changes. According to GNU coreutils, it adds execute only to directories and to files that already have execute for some user. That makes chmod -R u=rwX,g=rX,o= /srv/data much safer than lowercase x on a tree of mixed data and scripts. Data files stay non-executable, and directories stay traversable.
It still rewrites the mode of every file and directory in the tree, and on ACL’d paths the group bits change the ACL mask, so inspect the tree first (ls -lR or getfacl -R) and try it on a copy before running it on live data. Lowercase x would mark every CSV and log file executable.
chown and chgrp
chown user:group path changes ownership. chgrp group path changes only the group. On standard Linux, only root (or a process with the right capability) can give a file to another user. Regular users can chgrp their own files only to groups they belong to. Use sudo for ownership changes. On network filesystems, the server may still refuse the change even when the client runs as root.
umask and systemd’s UMask=
The umask removes bits from the default creation mode. That’s usually 666 for files and 777 for directories. A umask of 022 gives 644 files and 755 directories. A umask of 002 gives 664 and 775.
The default differs by distribution and by whether user-private groups are in use. Check yours instead of assuming:
umask # numeric, e.g. 0022 or 0002
umask -S # symbolic, e.g. u=rwx,g=rx,o=rx
A systemd service doesn’t inherit the umask of the shell you typed systemctl in. systemd sets the service’s umask: from the UMask= directive in the unit’s [Service] section if you set one, otherwise from systemd’s default, which systemd.exec documents as 0022 for system services. The service process can also change its own umask after it starts. See man systemd.exec for your systemd version’s documented default.
Special Bits: setuid, setgid, Sticky
- setuid (
4xxx,u+s) on an executable runs it as the file’s owner. Auditing it is out of scope here. - setgid (
2xxx,g+s) on a directory makes new files and subdirectories inherit the directory’s group instead of the creator’s primary group. New subdirectories also inherit the setgid bit itself. - Sticky (
1xxx,+t) on a directory means only the file’s owner, the directory’s owner, or root can delete or rename entries, even when others have write access./tmpis the standard example.
POSIX ACLs and the Mask
setfacl adds named entries like user:bob:rw- or group:auditors:r-x. getfacl displays them. The key concept is the mask (mask::). It caps the effective permissions of every named user, every named group, and the owning group. The file owner and “other” sit outside the mask. When the mask removes a bit, getfacl adds an #effective: note (getfacl(1), acl(5)).
Default ACLs on Directories
Entries prefixed with d: (for example setfacl -m d:g:devteam:rwx dir) apply only to things created inside the directory. They’re a template that new files and subdirectories copy at creation time (setfacl(1)). The kernel ignores them when checking access to the directory itself. That’s how a shared folder hands out the right access automatically.
Who Should Use This?
- System administrators running multi-user servers, shared project directories, or service accounts that write into common paths.
- Homelab users exporting a shared folder from a NAS drive or a Raspberry Pi file server, where several local accounts or services need different access levels.
- Developers wearing ops hats who deploy apps under dedicated users and need deploy scripts, web servers, and humans to share the same files.
If your system has a single user and a few services that each own their own directories, plain mode bits are enough. ACLs earn their keep once you need more than one group per path.
How It Works
The Access Evaluation Order
The acl(5) man page defines the algorithm. The kernel walks it in order and stops at the first matching category:
- Owner. If the process’s effective UID owns the file, the owner entry decides. Nothing else is checked.
- Named user. If an ACL entry names this UID, that entry decides, capped by the mask.
- Groups. If any of the process’s groups match the owning group or a named group entry, all matching group entries are considered together. Access is granted if any one of them (capped by the mask) holds the requested permission, so one named group entry doesn’t shadow another. If none does, access is denied.
- Other. Only if nothing above matched.
Without an ACL, step 2 and the “named group” part of step 3 don’t exist. The group check then uses the plain group triad. Root (or CAP_DAC_OVERRIDE) bypasses most of this.
The “first match wins” rule creates some odd results. If a file is ---r--r-- and you own it, you can’t read it, even though “other” can. The first match replaces every later rule.
Why an ACL Grant Seems to Do Nothing
This is the most common ACL surprise. You grant rwx, getfacl lists it, and writes still fail:
touch /srv/data/build.log
setfacl -m g:developers:rwx /srv/data/build.log
setfacl -m m:r-x /srv/data/build.log # mask explicitly narrowed
getfacl /srv/data/build.log
Expected output (owner and group will reflect your system):
# file: srv/data/build.log
# owner: alice
# group: alice
user::rw-
group::rw- #effective:r--
group:developers:rwx #effective:r-x
mask::r-x
other::r--
getfacl also prints Removing leading '/' from absolute path names to stderr. That message is harmless.
In real life, the mask usually gets narrowed by accident. On a file with an ACL, chmod g=... changes the mask and leaves the owning-group entry alone. So a routine chmod 640 quietly caps every named grant at r--. When an ACL exists, ls -l shows the mask in the group triad, which adds to the confusion. setfacl recalculates the mask when you add entries, unless you pass -n.
Directory Semantics: r, w, x Mean Different Things
A directory is a list of names. Its bits control that list:
| Bit | On a directory it lets you… | Without it… |
|---|---|---|
r | List the names inside (ls) | You can still open a file if you know its name (with x) |
x | Traverse into it and access entries by name | Nothing inside is reachable, even with r. ls shows names but ????????? for metadata |
w | Create, delete, and rename entries (also needs x) | You can still edit an existing file’s contents if the file allows it |
Two points matter in practice. First, deleting a file is a write to the directory. Second, every parent directory in the path needs x for the user. A missing parent x is why a 777 file can still return “Permission denied.” namei -l /full/path/to/file shows every component at once.
How New Files Get Their Permissions
When a process creates a file, one of two rules applies:
- The parent has no default ACL: requested mode minus the process umask.
- The parent has a default ACL: the new file’s ACL is built from the default ACL, limited by the mode the creating program requests, and the process umask is not applied (acl(5)). A default ACL can’t add permissions the program didn’t request. For example,
touchrequests666, so new files still get no execute bits.
That second rule is why default ACLs beat umask tweaks for shared folders. The result doesn’t depend on each user’s or service’s umask. It still depends on the program, though: an application that requests a restrictive mode, or runs chmod on its files after creating them, ends up with those tighter permissions.
Getting Started
These commands target current Ubuntu LTS releases and RHEL-family systems (RHEL, Rocky Linux, AlmaLinux). They describe GNU coreutils behavior. If your distribution ships an alternative implementation such as uutils/coreutils, confirm flag support with --help. The outputs shown are what GNU coreutils and the Linux acl utilities print. Check your versions with chmod --version, getfacl --version, and systemctl --version, since output formats and defaults can differ between versions and implementations.
1. Confirm the ACL Tools Are Installed
getfacl and setfacl come from a separate acl package. Whether it’s installed by default depends on the distribution and install profile, so check first:
command -v getfacl setfacl
If the command prints nothing, install the package:
# Ubuntu / Debian
sudo apt install acl
# RHEL / Rocky Linux / AlmaLinux
sudo dnf install acl
2. Confirm the Filesystem Supports ACLs
findmnt -T /srv
ext4 and XFS (the RHEL default) support POSIX ACLs. If setfacl returns “Operation not supported,” check the filesystem type and mount options before going further.
3. Build a Shared Team Directory (setgid + Default ACL)
Before this setup, a file that alice creates in a plain 770 directory gets her primary group. Its mode depends on her umask. Teammates often can’t edit it.
Warning: Run these as root or with
sudo. Group changes fromusermod -aGonly apply to new login sessions, so log out and back in (or usenewgrp devteam) before testing.
sudo groupadd devteam
sudo groupadd auditors
sudo usermod -aG devteam alice
sudo mkdir -p /srv/projects
sudo chown root:devteam /srv/projects
sudo chmod 2770 /srv/projects # 2 = setgid; owner+group rwx; other none
sudo setfacl -m g:auditors:rx,d:u::rwx,d:g::rwx,d:g:auditors:rx,d:m:rwx,d:o::--- /srv/projects
# d: entries are the template for new files and subdirectories;
# every default base entry is set explicitly so the result is predictable
getfacl /srv/projects
Expected output:
# file: srv/projects
# owner: root
# group: devteam
# flags: -s-
user::rwx
group::rwx
group:auditors:r-x
mask::rwx
other::---
default:user::rwx
default:group::rwx
default:group:auditors:r-x
default:mask::rwx
default:other::---
Add each auditor account to the group the same way (sudo usermod -aG auditors <user>). Creating the group and the ACL entry grants nothing until someone is a member, and the membership also takes effect only at the next login.
After: create a file as alice and inspect it.
touch /srv/projects/plan.txt
ls -l /srv/projects/plan.txt
getfacl /srv/projects/plan.txt
Expected output (date will differ):
-rw-rw----+ 1 alice devteam 0 Oct 27 10:02 /srv/projects/plan.txt
# file: srv/projects/plan.txt
# owner: alice
# group: devteam
user::rw-
group::rwx #effective:rw-
group:auditors:r-x #effective:r--
mask::rw-
other::---
The group is devteam because of setgid, and the auditors’ read access came from the default ACL on /srv/projects. touch requested 666, so the mask became rw-—correct, since data files don’t get execute bits.
Note that plan.txt is a regular file, so it carries only an access ACL, not a default ACL; only directories retain default: entries. The named-user and named-group permissions shown here are the access-ACL copy the kernel generated from the parent directory’s default ACL at creation time (acl(5)).
4. Set a umask for a systemd Service
For a service that writes into a shared path, set the umask in the unit file and leave the shell out of it:
sudo systemctl edit myapp.service
Add this to the override:
# /etc/systemd/system/myapp.service.d/override.conf
[Service]
UMask=0007
Then restart and verify:
sudo systemctl restart myapp.service
systemctl show -p UMask myapp.service
Expected output:
UMask=0007
To see the effect on a real file, run a one-off transient service with the same umask, create a file in a directory that has no default ACL, and inspect it:
sudo mkdir -p /srv/umask-test
sudo systemd-run --wait -p UMask=0007 /usr/bin/touch /srv/umask-test/from-service.txt
stat -c '%a %n' /srv/umask-test/from-service.txt
sudo rm -r /srv/umask-test
Expected output:
660 /srv/umask-test/from-service.txt
touch requests 666, and the 0007 umask removes all bits for “other,” leaving 660. Your own service’s files show the same pattern once it writes them.
If the service writes into a directory with a default ACL, the default ACL (limited by the mode the service requests) is used instead of the umask. Pick one model per directory and write it down.
5. Fix Tree Permissions Safely
Warning: Recursive
chmodandchownon the wrong path (/,/etc,/usr) can break a system. Double-check the target before pressing Enter.
sudo chmod -R u=rwX,g=rwX,o= /srv/projects # X: execute only for dirs and already-executable files
sudo find /srv/projects -type d -exec chmod g+s {} + # re-assert setgid on every directory
If you need explicit control instead:
sudo find /srv/projects -type d -exec chmod 2770 {} +
sudo find /srv/projects -type f -exec chmod 660 {} +
Both versions overwrite the mode of every file and directory in the tree, including intentional per-file exceptions. Save the current state first with getfacl -R /srv/projects > /tmp/before.acl so you can compare or restore it.
On paths with ACLs, the group bits you set here become the ACL mask, which caps every named entry (such as auditors). Re-check with getfacl afterward. In the shared-directory setup, keep setgid on directories: the g+s line above does that for the recursive version, and chmod 2770 does it in the find version.
6. Preserve ACLs When Copying or Backing Up
Plain cp and many archive defaults silently drop ACLs. With GNU cp, rsync, and GNU tar, use the ACL-aware flags below. Other implementations (BusyBox, BSD tar, uutils) may differ, so check --help. Preservation also needs ACL support on both filesystems and enough privilege at the destination to set ownership and ACLs.
The lines below are alternatives, not a sequence. Create the destination first (sudo mkdir -p /backup) so the cp and rsync forms both produce /backup/projects:
cp -a /srv/projects /backup/ # GNU cp: -a keeps mode, ownership, xattrs, ACLs where both sides support them
rsync -aAX /srv/projects/ /backup/projects/ # -A preserves ACLs; -X preserves extended attributes
sudo tar --acls -cpf /backup/projects.tar -C /srv projects
sudo tar --acls -xpf /backup/projects.tar -C /restore # --acls is needed on extract too
Check the result instead of trusting it:
getfacl -R /srv/projects > /tmp/src.acl
getfacl -R /backup/projects > /tmp/dst.acl
The # file: paths will differ, but the entries should match. A plain-text dump also makes a good disaster-recovery fallback. Run setfacl --restore=/tmp/src.acl from the right working directory to reapply it.
The same applies to an external USB drive or a NAS. If the target is FAT/exFAT or an SMB share without POSIX ACL support, the ACLs won’t survive.
Cost and Availability
There’s nothing to buy. chmod, chown, and chgrp ship in GNU coreutils on practically every distribution. ACL tools ship separately.
| Component | Package | Notes |
|---|---|---|
chmod, chown, chgrp, stat | coreutils | Installed on standard Ubuntu and RHEL-family systems. Minimal images may use BusyBox or uutils instead |
getfacl, setfacl | acl | May need installing. Check with command -v |
UMask= | systemd | Per-unit, documented in systemd.exec(5) |
namei | util-linux | Path-walk permission view |
Alternatives
- SELinux / AppArmor: These mandatory access control layers run in addition to mode bits and ACLs. A denial can happen even when
getfacllooks perfect. On RHEL-family systems, checkls -Zand the audit log before loosening anything. - Groups and setgid only, no ACLs: If one group per directory is enough, skip ACLs entirely. You get fewer moving parts and nothing to lose during copies. The trade-off is that you can’t add a second read-only group.
- Per-service directories: Systemd can manage service-owned state directories. For many single-service cases, that’s cleaner than sharing a path.
The Bottom Line
The model is small but strict: owner, then named users, then masked groups, then other, and the first match ends the check. Most “it should work” mysteries come down to the mask, a missing parent x, or a service umask that differs from yours. A setgid directory plus a default ACL is the most reliable way to keep shared folders in line without regular cleanups.
| Pros | Cons |
|---|---|
| Built into every distribution at no cost | The mask is easy to narrow by accident with an ordinary chmod |
| Predictable once you know the evaluation order | ACLs disappear quietly with non-ACL-aware copy tools or filesystems |
| Default ACLs make shared folders self-maintaining | ls -l only shows a +, so you need getfacl to see the full picture |
getfacl -R and setfacl --restore make permissions scriptable and reproducible | MAC layers like SELinux can override everything here and are debugged separately |
Wrapping Up
You can now predict an access decision instead of guessing. You can also build a shared directory that gives every new file the right group and ACLs, and give a service its own umask. My take: use default ACLs for anything shared, make getfacl -R part of every backup job, and run namei -l before blaming the file.
| Step | Action | Applies To |
|---|---|---|
| 1 | Install/verify acl package | Ubuntu LTS, RHEL family |
| 2 | Confirm ACL support with findmnt -T | ext4, XFS |
| 3 | chmod 2770 + default ACL | Shared team directories |
| 4 | UMask= in a unit override | systemd services |
| 5 | chmod -R ...X or split find | Existing trees |
| 6 | cp -a, rsync -aAX, tar --acls + verify | Backups and migrations |