1. The Most Common Cause: Wrong File Permissions (chmod)
I can't count the times a junior admin has come to me with a 'permission denied' error and it turned out the file mode just didn't allow the user to read or write. The error is plain: Permission denied when you try to cat a file, or bash: file.txt: Permission denied when you try to execute a script that isn't executable.
Here's the deal: Linux checks three sets of permissions — owner, group, and other. If you're not the owner and not in the group, you get the 'other' bits. If any of those don't have the read (r), write (w), or execute (x) bit you need, boom, EACCES.
First step is always to check who you are and what the file expects:
whoami
ls -l /path/to/file
If you see something like -rw-r--r-- 1 root root and you're not root, you can't write to it. That's by design. Don't panic.
To fix, either change the owner or the mode. If you own the file, just do:
chmod u+w file.txt # add write for you
chmod +x script.sh # add execute for everyone
If you don't own it and you have sudo rights, you can change ownership:
sudo chown youruser:yourgroup /path/to/file
But don't go chmod 777 everything. That's lazy and a security hole. Only give what's needed.
One sneaky variant: when you edit a file with vim or nano and it's owned by root, but you're in the sudo group, you'll get a 'permission denied' on write. The fix is simple: sudo nano file or sudo vim file. That one trips people up a lot.
2. The Second Most Common: Wrong Ownership (chown) After Copying or Extracting
You extract a tarball you downloaded, and suddenly everything is owned by some other user. Or you copy files from an NTFS or FAT drive that was mounted with root ownership. That's a classic.
When you unpack a tar file that was created by someone else, the ownership comes along as numeric UID and GID. If the UID doesn't match any user on your system, it defaults to nobody or some high number. Then you can't touch those files.
The fix is to give ownership back to yourself:
sudo chown -R youruser:yourgroup /path/to/directory
Use the -R flag to do it recursively. That's the whole deal. I've seen this happen with extracted Android SDKs, node_modules, and Docker volumes. It's always the same line.
If you're dealing with a mounted USB drive, you can avoid the headache by mounting with the right options, like:
sudo mount -o uid=1000,gid=1000 /dev/sdb1 /mnt/usb
Replace 1000 with your user's UID (check with id -u). That makes files appear owned by you right off the bat.
3. The Third: SELinux or AppArmor Blocking You (Yes, It Happens)
Okay, this one is the subtle one. You've checked permissions, you own the file, you're root, and still can't write. On RHEL, CentOS, Fedora, and some other distros, SELinux is in the way.
SELinux labels processes and files with contexts. If the context is wrong, access is denied even with 777 permissions. The error messages can be cryptic, but if you see Permission denied and you're absolutely sure the mode is right, check SELinux first:
getenforce
If it says Enforcing, that's your suspect. Check the audit log:
sudo ausearch -m avc -ts recent
You'll see entries like denied { read } for pid=... comm="httpd". That tells you exactly what's blocked.
The quick fix is to restore the correct context:
sudo restorecon -Rv /path/to/directory
If you moved a file from another location, the context might be wrong. restorecon resets it to the policy default. That solves 90% of SELinux-related permission issues.
If you're running a web server and it can't read files in /var/www/html, also check that the directory isn't in a different context. The correct one is httpd_sys_content_t. You can change it with:
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?"
sudo restorecon -Rv /var/www/html
Don't just disable SELinux. That's a sledgehammer. It rarely helps and often causes more problems later. Fix the context.
On Ubuntu and Debian, AppArmor does similar things, but it's less common to hit. Check dmesg for apparmor="DENIED" messages, and if you see one, adjust the profile or add an alias.
Quick Reference Table
| Cause | Symptom | Fix |
|---|---|---|
| Wrong file mode (chmod) | Cannot read/write/execute, ls -l shows obvious missing bits | chmod u+rwx file |
| Wrong ownership (chown) | Files owned by root or unknown UID after extraction/copy | sudo chown -R user:group dir |
| SELinux context | Permission denied even with 777, getenforce says Enforcing | sudo restorecon -Rv dir |