Skip to content

Lesson 11 of 14 · 20m

Permissions, ownership, and umask without chmod 777

The three-slot model, setgid shared dirs, umask per layer, and setfacl for the cases POSIX can't express.

When a deploy dies with EACCES, the fix most people reach for is chmod -R 777 — which doesn't fix the problem, it deletes the problem, along with the only thing standing between "web process can read uploads" and "web process can execute whatever got uploaded." Permissions are a three-slot model plus three special bits, and nearly every incident involving them traces to someone not checking which slot was actually blocking.

The bits

rwx per slot — owner, group, other. On directories the bits mean different things: read lists entries, write creates and deletes entries, execute traverses. A directory can be listable but not traversable, which produces exactly the confusing half-permissions that make people give up and 777.

chmod takes octal (640, 2770) or symbolic (u+r, g-w, o=); chown sets owner:group. For shared directories, the mechanism people don't know exists is setgid: chmod 2770 /srv/app makes every file created inside inherit the group, so deploy members can write each other's files without a permanent fight.

Setuid (u+s, 4xxx) runs the binary with the file owner's privileges — that's the entire mechanism behind sudo and passwd, and also why a setuid binary that shells out is a privilege-escalation finding. find / -xdev -perm /6000 -type f lists every setuid+setgid file; baseline it once so a new one stands out. The sticky bit (+t, 1xxx) on shared dirs like /tmp means only a file's owner can delete it.

The permissions already protecting you are worth memorizing as a baseline: /etc/shadow is root:shadow 640, /etc/ssh/sshd_config is root:root 644, private keys under ~/.ssh/ are 600. If a package or a previous admin left any of those wider — a world-readable shadow is a find-it-in-audit classic — tighten it and ask why it was loosened.

umask: what things are born with

Files aren't created at 777 and locked down; they're created at 0666 & ~umask for files and 0777 & ~umask for directories. The default umask 022 yields 644/755 — world-readable everything. On a multi-service box or anywhere secrets land, umask 027 (group reads, other gets nothing) or 077 (owner only) is the correct default. Set it in the shell profile and the systemd unit (UMask=) and the app config, because each layer can silently widen it back.

When three slots aren't enough: ACLs

Two groups needing different access to one tree is where POSIX permissions end. setfacl -m g:qa:rx /srv/app/releases grants qa their own entry; getfacl shows the real picture — the + at the end of ls -l output is your hint that ACLs exist. Default ACLs (setfacl -d -m g:qa:rx ...) propagate to new files the way setgid propagates group ownership; combine them for shared trees.

Try this

groupadd deploy && useradd -m -G deploy alice && useradd -m -G deploy bob
mkdir -p /srv/shared && chgrp deploy /srv/shared && chmod 2770 /srv/shared
# as alice: touch /srv/shared/notes — check the group is deploy, not alice
setfacl -m g:qa:rx /srv/shared   # qa reads, deploy writes
getfacl /srv/shared
find / -xdev -perm /6000 -type f 2>/dev/null   # baseline your setuid/setgid set

Pitfalls

  • chmod -R blanket fixes: you just made every file in a config tree executable — and on a webroot, writable by every local process.
  • Recursive chown on paths containing symlinks follows them into places you didn't mean (chown -h for links).
  • ~/.ssh at 755 or authorized_keys owned by root: sshd ignores unsafe key files and the failure looks like "key rejected."
  • umask set in the shell but not the unit: the daemon's files come out with different permissions than the operator's.
  • NFS root_squash: remote root maps to nobody; permissions look right and still fail.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.