Skip to content
Loopedin Labs

Linux permissions

chmod 777 isn't a fix. Here's what's actually wrong.

Mini Rush · practice

Three questions before you read

Guessing first makes the answers stick. No sign-in.

Three digits, three groups of people, and one twist: directories read the bits differently.

The mental model

Every file has an owner, a group and a mode with three sets of bits: owner, group and other. The kernel checks only one set. If you own the file, it uses the owner bits; otherwise, if you're in the file's group, it uses the group bits; otherwise it uses the other bits. Root bypasses most checks.

In octal, r=4, w=2 and x=1, so 640 is rw-r-----.

The bits mean different things on directories:

BitOn a fileOn a directory
rread contentslist names
wmodify contentscreate, delete, rename entries (needs x too)
xexecuteenter and traverse to known names

Deleting a file is a change to its directory, so it depends on the directory's w and x, not on the file's mode.

ls -l file; ls -ld dir        # mode, owner, group
stat -c '%a %U:%G %n' file    # octal mode and owner
namei -l /var/app/data/out.log # permissions of every directory on the path
id appuser                    # the user's groups

Changing things

chmod 640 app.conf        # change mode bits
chmod u+x,go-w deploy.sh  # symbolic form
chown appuser:app data/   # change owner and group (root only for the owner)
chgrp app data/           # change the group to one you belong to
umask                     # e.g. 0022

chmod changes what is allowed. chown changes who the owner and group are. A normal user can't give a file to someone else; changing the owner needs root (CAP_CHOWN).

umask clears bits from the default mode at creation. Files start from 666 and directories from 777, so umask 022 gives 644 and 755, and 027 gives 640 and 750.

Special bits

  • setuid (4xxx, s in the owner slot): the binary runs with its owner's UID. passwd uses this. Linux ignores it on scripts.
  • setgid (2xxx): on a directory, new files inherit the directory's group. This is useful for shared team folders.
  • sticky (1xxx, t): in a shared writable directory such as /tmp, only a file's owner, the directory's owner or root can delete or rename it.

Gotchas

  • "Permission denied" on /var/app/data/out.log can come from any parent directory missing x. Use namei -l to see the whole path.
  • Adding a user to a group doesn't affect shells that are already logged in. Log in again, or run newgrp.
  • SSH refuses private keys that group or other can access (any bit in 077, so even 640 fails). Use chmod 600 on the key and 700 on ~/.ssh.
  • In containers, the UID matters, not the user name. A bind mount owned by UID 1000 on the host is still UID 1000 inside the container.

Common wrong fix

chmod -R 777 /var/app makes the error go away, but now every local user and every compromised process can rewrite your code and config. It also sets x on every data file. And it often hides the real cause: the service runs as a different user than you assumed. Find the process user (ps -o user= -p <pid>), then fix ownership with chown -R appuser:app and set tight modes, such as 750 for directories and 640 for files.