Catalog My learning Blog
Linux Command Line Tutorial for Developers · Phase 4 · Lesson 25 · 3 min read

Fix Linux Permission Denied Errors

On this page
  1. How to fix permission denied: a method
  2. Case 1: a directory blocks the path (namei)
  3. Case 2: the running service still can't read the file
  4. Case 3: owner and permissions fine, still denied
  5. Other causes: ACLs, SELinux, AppArmor, read-only mounts
  6. Why chmod 777 is never the fix
  7. Check yourself

How to fix permission denied: a method

A service can't read a file. The usual reaction is chmod 777, which hides the cause and opens a security hole. Instead, ask in order:

  1. Who is the process running as, with which groups?
  2. Every directory on the path: does that user have execute (traverse) permission?
  3. The file itself: read or write for that user?
  4. Anything beyond classic permissions: sandboxing, ACLs, SELinux or AppArmor, read-only mounts?

Case 1: a directory blocks the path (namei)

Shell
sudo -u myapp cat /srv/data/reports/q3.csv
Code
cat: /srv/data/reports/q3.csv: Permission denied

The file is -rw-r--r--, readable by everyone. namei -l checks every component of the path:

Code
namei -l /srv/data/reports/q3.csv
Code
f: /srv/data/reports/q3.csv
drwxr-xr-x root root /
drwxr-xr-x root root srv
drwxr-x--- root root data
drwxr-xr-x root root reports
-rw-r--r-- root root q3.csv

data is drwxr-x---: others have no execute, so myapp can't pass through it. Rather than opening it to everyone, give a group access:

Shell
sudo groupadd reports
sudo chgrp reports /srv/data
sudo usermod -aG reports myapp
sudo -u myapp cat /srv/data/reports/q3.csv
Code
q3

Case 2: the running service still can't read the file

The command works, yet the service still gets permission denied. Check the groups of the running process:

Shell
grep Groups /proc/$(systemctl show -p MainPID --value myapp)/status
Code
Groups:	987

Group membership is read when a process starts (Day 5). The service started before myapp joined reports. After systemctl restart myapp:

Code
Groups:	987 1004

Now it includes 1004 (reports). Whenever you change a service user's groups, restart the service.

Access requires the right user and groups in the running process, execute permission on every directory in the path, permission on the file itself, and no sandbox or security policy blocking it.
Access requires the right user and groups in the running process, execute permission on every directory in the path, permission on the file itself, and no sandbox or security policy blocking it.

Case 3: owner and permissions fine, still denied

The service owns /var/lib/myapp, yet writes fail. Testing under the same sandbox settings as the unit (Day 19):

Shell
sudo systemd-run --pipe --wait -q -p User=myapp -p ProtectSystem=strict \
  /bin/sh -c 'touch /var/lib/myapp/x'
Code
touch: cannot touch '/var/lib/myapp/x': Read-only file system

Not "permission denied": read-only file system. ProtectSystem=strict mounts the filesystem read-only inside the service's sandbox. Adding ReadWritePaths=/var/lib/myapp:

Code
write ok

The exact error message is a clue: "Read-only file system" means a mount or sandbox, not file permissions.

Other causes: ACLs, SELinux, AppArmor, read-only mounts

Symptom or clueLikely causeTool
Permissions look right, still deniedACLsgetfacl; ls -l shows a +
Denied for a service, fine as root in a shellSELinux (RHEL family) or AppArmor (Ubuntu)ausearch -m avc, journalctl -k, aa-status
Works in your shell, not as the serviceDifferent user, groups, or environmentsystemctl show -p User,Group, /proc/<pid>/status
"Operation not permitted" when changing ownershipOnly root can chownsudo
Can't delete a file you ownDirectory permissions (needs write on the directory), or the sticky bitls -ld dir

strace -f -e trace=openat,access (Day 1) shows the exact path and error the program hit.

Why chmod 777 is never the fix

chmod -R 777 makes everything writable by every user and process on the machine. It doesn't tell you what was wrong, and leaves a hole that's easy to forget. Fix the specific component, with the narrowest grant: a group, an ACL, or a ReadWritePaths entry.

Check yourself

Execute permission on every directory in the path, using namei -l /full/path, which shows the permissions of each component. Then check whether the service runs as the user you think.

Running processes keep the groups they started with; group changes only apply to new processes. The service needed a restart to pick up the new membership.

The block comes from a mount or sandbox, such as a read-only mount or systemd's ProtectSystem, not from file ownership or mode bits. Changing chmod or chown won't help; check the mount options or the service's unit file.

It gives every user write access, hides the real cause, and often still fails because the problem was a directory, mount or security module.