The 30-second fix: Check owner, permissions, and file flags
You're in a directory, you see myfile.txt, and cat myfile.txt throws Permission denied. I've been there—it's maddening when the file clearly exists. Let's rule out the obvious first.
- Run
ls -la myfile.txt. Look at the first column (e.g.,-rw-r--r--). If the owner isrootand you're not root, that's your problem. Also check if there's ani(immutable) flag in the output—e.g.,-rw-r--r-- 1 root root 0 Jan 1 12:00 myfile.txtwith no extra flag? Good. If you see anisomewhere in the permissions line (sometimes shown withlsattr), that file is locked down. - Check the directory permissions:
ls -ld .. If the directory lacks execute (x) for you, you can't access any files inside, even if the file itself is readable. That one tripped me up the first time too. - Quick fix:
chmod u+r myfile.txtif you're the owner. If not,sudo chown $USER: myfile.txt(only if you have sudo).
If that didn't work, move to the moderate fix—it's probably ACLs or a weird mount.
5-minute fix: Check ACLs and mount options
Standard permissions look right? The file shows -rw-r--r-- and you're the owner? Then ACLs are the usual suspect. I've seen this on shared NFS drives or ext4 partitions with custom ACLs.
- Run
getfacl myfile.txt. If you see entries likeuser:someuser:---ormask::---, ACLs are blocking you. Example output:
The# file: myfile.txt # owner: you # group: you user::rw- user:someuser:--- mask::--- other::r--maskentry overrides standard permissions for named users/groups. A mask of---means no one but the owner can read it. - Fix:
setfacl -m mask::r-- myfile.txtto allow read. If you want to remove all ACLs:setfacl -b myfile.txt. - Still broken? Check mount options:
mount | grep $(df . | tail -1 | awk '{print $1}'). Look fornoexec,nosuid,nodev. If you seenoexec, you can still read and write files, but you can't execute them—but that shouldn't block reading. However, anro(read-only) mount will. If it'sro, remount withsudo mount -o remount,rw /mount/point.
If you're still getting denied after this, it's time for the deep fix.
15+ minute fix: SELinux, AppArmor, or LSM interference
When permissions and ACLs look perfect but the system still says no, it's a mandatory access control system. I've spent hours on this with RHEL and Ubuntu servers. Here's the drill.
- Check if SELinux is enforcing:
getenforce. If it saysEnforcing, runls -Z myfile.txt. You'll see something likesystem_u:object_r:etc_t:s0. If that context doesn't match what the process expects (e.g., a web server needshttpd_sys_content_t), you get denial. - View the audit log:
sudo ausearch -m avc -ts recentorsudo journalctl -x | grep -i denied. Look fordenied { read }for your file. - Fix the context:
sudo chcon -t httpd_sys_content_t myfile.txt(replace with the correct type). Or usesemanage fcontextfor a permanent fix:sudo semanage fcontext -a -t httpd_sys_content_t /path/to/myfile.txt && sudo restorecon -v /path/to/myfile.txt. - For AppArmor (Ubuntu/Debian), check with
sudo aa-status. If a profile is blocking access, either adjust the profile in/etc/apparmor.d/or set the profile tocomplainmode temporarily:sudo aa-complain /path/to/binary. - As a last resort—and I mean last—you can disable SELinux temporarily:
sudo setenforce 0. Don't leave it off. Re-enable withsudo setenforce 1and fix the context properly.
| Check | Command | What to look for |
|---|---|---|
| Permissions | ls -la | Owner, group, rwx bits |
| ACLs | getfacl | Mask or named user entries |
| Mount flags | mount | ro, noexec |
| SELinux | ls -Z, getenforce | Context mismatch or enforcing mode |
I know this error is infuriating, especially when the file stares you in the face. Nine times out of ten, it's either a directory missing execute permission or an ACL mask. Try the 30-second fix first. If that fails, jump to ACLs. Only go to SELinux if you're on a security-hardened distro like RHEL or CentOS. You'll have that file open in no time.