Every "Permission denied" in a log is the same three words and almost never the same cause. EACCES is errno 13, and the kernel uses that one number for four different decisions. Once you know which four, the message stops being a dead end — and chmod 777 often still changes nothing, because it misses three of them.
I measured all four on one Linux host — 38 calls, each repeated five times, as uid 1000 on kernel 6.12.118 — and wrote the kernel's decision out as a small Python module so a program can say which bit will deny a call before making it. Here is the short version: what to check, and in what order.
The four rules behind one number
Of the 38 calls in the matrix, 21 answered 13 and 9 succeeded. Those 21 come from four rules:
-
The object's own bits deny the class that applies. A
0400file opened for write answers 13 — the owner class has now. -
An ancestor directory has no
x. A0444directory refused both theopenand thestatof a perfectly ordinary0644file inside it. The file's mode was never consulted, because the walk never arrived. -
The object belongs to another account and the other-class bits are clear. A
0755file owned by root gives uid 1000 nothing to write with: 13. -
What is missing is a privilege, not a bit. Ports below 1024 need a capability; a read-only mount refuses with
EROFS(30) before anyone looks at a mode.
Rules 1 to 3 are the same nine bits read three ways. Rule 4 is the one that makes people edit the wrong thing for an hour.
Start with one strace, not with chmod
The log line says what failed. One command says which call failed:
strace -Z -f -e trace=%file ./your-service --the-command-that-fails
-Z keeps only the calls that returned an error (-z is its opposite, only the successful ones; both arrived in strace 5.2 as aliases of -e status=). %file keeps the calls that take a path. The failing line is the kernel's own account:
openat(AT_FDCWD, "/srv/app/upload", O_WRONLY|O_CREAT, 0666) = -1 EACCES (Permission denied)
The flags beside the path are the access the process asked for, which a log line drops. What the trace still cannot say is which component of the path refused, because the kernel reports the path the call passed and not the step it stopped at. That gap is what the walk below fills.
One honest note from my own host: strace was not installed, and a binary unpacked from the Debian package was refused ptrace by the container's seccomp profile. The trace line above is quoted from the tool's documentation, not captured on that machine.
Rule 1: the class is chosen before a bit is read
The kernel never asks "is this file writable". It asks which third of the mode word belongs to your process: the owner bits if the effective uid matches the file's owner, the group bits if the gid or any supplementary group matches, otherwise the other bits. Only that triplet counts.
That ordering is why a mode can lock out its own owner. On this host, with a 0644 file inside each directory:
| Directory mode | A 0644 file inside it |
open(O_RDONLY) |
stat |
|---|---|---|---|
0777 |
untouched | 0 | 0 |
0077 |
untouched | 13 | 13 |
0700 |
untouched | 0 | 0 |
A directory at 0077 — group and other wide open, owner triplet cleared — refused the account that owned it. Nobody would ask a stranger in, but ownership selects which three bits are read, so clearing the owner triplet locks the owner out.
The file can do the same thing on its own: every directory above it at 0755, the file at 0077, and a read answered 13.
Both repair the same way — put the owner triplet back, do not widen anything:
chmod u+rwX /srv/app/upload # the bits the owner needs, nothing more
namei -l /srv/app/upload/report.csv # and every component on the way there
Rule 2: x on every directory in the path
Directory x is search permission, and it is asked for separately from anything about the file. Every component of the path needs it, including the last directory before the file.
| Directory mode | What was asked of it | Result |
|---|---|---|
0444 |
open a 0644 file inside |
13 |
0111 |
open a 0644 file inside |
0 |
0444 |
stat that file |
13 |
0111 |
chdir into the directory |
0 |
0333 |
list its entries | 13 |
0555 |
list its entries | 0 |
A 0444 directory (readable, not searchable) is useless to the files inside it, while 0111 (searchable, not readable) lets them be opened but not listed. That is also why ls can print names where ls -l prints question marks — names need r, metadata needs x on the path.
Symbolic links are the third version of the same trap: a link carries 0777 and the kernel ignores it entirely. What counts is the directory holding the link and the mode of the target, so a link pointing at a 0644 file from inside a 0000 directory answers 13 — which reads like a problem with the target.
Rule 3: ownership, and the container case
The other class is where containers usually land: a volume mounted at 0755 and owned by another uid gives the service nothing to write with, and the service reports 13 on its own working directory. Before blaming the application, compare stat -c '%u:%g %a %n' /var/lib/app/data with id -u. If the uid differs, chown -R the mount to the runtime uid or mount it with that uid — no chmod on the child files fixes a class problem.
One distinction worth keeping: chmod by a non-owner answers EPERM (1), not 13, because ownership is checked before any bit.
Rule 4: when no bit is the answer
| Number | What it actually is | The call that answered it |
|---|---|---|
| 13 | the object's bits deny the class | read a 0400 file for write |
| 13 | an ancestor has no x
|
open a 0644 file through a 0444 directory |
| 13 | other-class bits are clear | write a root-owned 0755 file as uid 1000 |
| 13 | a privilege, not a mode | bind port 80 as a service user |
| 1 | ownership |
chmod a root file as uid 1000 |
| 30 | the mount is read-only | create in /usr
|
| 2 | the name does not exist | open a name that was never there |
| 17 | the name already exists |
O_CREAT plus O_EXCL on an occupied name |
| 8 | executable, but not in a format the kernel can run |
execve a text file with x set |
EROFS changes the order of your checks. /usr on that host is a read-only overlay, so a create there answered 30, and a chmod under it answered 30 rather than the 1 it gives on a writable filesystem. The mount comes before ownership, and ownership before any mode — a runbook that starts with chmod starts in the wrong place. ENOEXEC (8) is the quiet one: a script without a shebang, made executable, answers 8 rather than 13.
Delete is a directory permission
Creating, renaming and deleting are operations on a name, and names live in directories. Each needs w and x on the directory that holds the name. The file's own mode never enters the decision:
| Operation | Directory mode | Result |
|---|---|---|
unlink a 0444 file |
0755 |
0 |
unlink a 0644 file |
0555 |
13 |
| create a file | 0555 |
13 |
mkdir |
0555 |
13 |
The sticky bit narrows that rule further: with it set (1777, like /tmp), a name may be changed only by the name's owner, the directory's owner, or a process with the capability to override both. My attempt to isolate it failed honestly — a rename out of /tmp answered 13, but the name lives in / at 0755 root, so the parent refused it before the sticky bit was read. That rule is quoted from the manual, not measured.
Naming the component, not the number
The trace names the call; the kernel will not name the component. A program can, because every component is stat-able and the decision is a function of modes and ids visible from outside. The core of it is one function:
def granted(mode: int, klass: str) -> tuple[bool, bool, bool]:
"""The (read, write, execute) bits that apply to one identity class."""
shift = {"owner": 6, "group": 3, "other": 0}[klass]
return (bool(mode & (4 << shift)),
bool(mode & (2 << shift)),
bool(mode & (1 << shift)))
Pick the class, map the operation onto the bits it needs, walk the path in order, and report the first component that refuses with its mode, its class and the bit it wanted:
from permwhy import Want, explain
result = explain("/srv/app/upload/report.csv", Want.WRITE)
print(result.first.line() if result.first else "nothing refuses")
# /srv/app/upload: no write permission on the directory (mode 0555, w+x)
Against the kernel that walk agreed on 17 of 17 in-scope arms — including the case with two closed components, where the kernel answers one number and a reader would otherwise debug the wrong directory. It is a prediction, not a guarantee: ACLs, SELinux labels, the immutable attribute, a read-only mount and NFS root_squash are all checked after these nine bits.
One measurement worth keeping
A refusal is not slow: a refused open took a median 1.74 µs against 1.32 µs for the same call that succeeded, so a service that retries one burns almost the CPU of a successful call and surfaces the failure as load instead of an error rate. A quarter second of retrying made 151,052 attempts and succeeded 0 times. The bits do not change because a process asks again — a backoff only spreads out the certainty.
The checklist
- Read the errno: 13, 1, 30, 2 and 8 mean five different things.
- Check the mount (
ro?) and the ownership before touching a mode. - Walk the path, not the file:
namei -l, andxon every directory. - Read the class first — owner, group, other. Widening the wrong triplet changes nothing.
- For delete, create and rename, look at the directory.
- Fix the first component that refuses, with the smallest bit that fixes it.
Full benchmark tables, 91 tests, and appendix code for the walk: EACCES errno 13: the Bit, the Directory, and the chmod That Ends It
Top comments (1)
Good point about checking the whole path with namei -l instead of just running chmod on the file. The directory x bit trips up lots of beginners, and the 0077 owner lock-out is a odd one.