Fix Linux File Permission & Ownership Lockouts
Why 777 is never the answer, how execute bits work on directories, and how to fix ownership and SELinux context lockouts safely.
You'll learn why chmod 777 creates security holes without fixing service lockouts, how directory execute (x) bits actually work, and how to diagnose chown and SELinux context mismatches in under two minutes.
1. The big idea
When Nginx, systemd, or SSH refuses to read a file with Permission denied, the reflexive panic move is chmod -R 777 /var/www. That makes every file world-writable (which SSH and many daemons explicitly reject as insecure) and still fails if the parent directory is missing execute (x) permission or SELinux is blocking access.
2. How directory traversal and ownership actually work
On Linux, permissions are checked at every directory from / down to your target file:
- Files:
rlets you read contents,wlets you modify,xlets you run it as a program. - Directories:
rlets you list filenames (ls),wlets you create/delete entries, andxlets you traverse into the directory (cdor open a file inside it).
If /home/deploy/app/index.html is 644 owned by www-data, but /home/deploy is 700 owned by deploy, the www-data user gets Permission denied because it cannot traverse /home/deploy.
# Inspect every segment along a path in one shot
namei -l /var/www/app/public/index.htmlnamei -l prints the owner, group, and mode for every directory in the chain so you spot the exact blocked folder immediately.
3. Applying least-privilege chown and chmod
Instead of 777, give the service user ownership (or group ownership) and separate directory modes from file modes:
# 1. Assign ownership to the service user and group
sudo chown -R www-data:www-data /var/www/app
# 2. Directories need x (755), regular files do NOT (644)
sudo find /var/www/app -type d -exec chmod 755 {} +
sudo find /var/www/app -type f -exec chmod 644 {} +
# 3. Restore execute bit only on actual binaries or scripts
sudo chmod +x /var/www/app/bin/server4. Checking SELinux or AppArmor when Unix bits look fine
On RHEL, Fedora, Rocky, or AlmaLinux, ls -l can look completely right and still fail with Permission denied. Check the SELinux context with ls -Z:
# Compare the file's SELinux label
ls -Z /var/www/app/index.html
# Fix mislabeled web files moved from /home or /tmp
sudo restorecon -Rv /var/www/appWhen you mv a file from /home/user into /var/www, it carries its user_home_t label with it. restorecon resets the label to httpd_sys_content_t based on system policy—no need to disable SELinux.
Check that you never run chmod 777 on production paths, use namei -l to inspect parent directories, separate directory (755) and file (644) permissions, and run restorecon -Rv if SELinux is enforcing.
5. Common pitfalls
| Mistake | Why it fails | Better fix |
|---|---|---|
Running chmod -R 777 | Makes secrets world-writable; SSH rejects 777 keys | Use 755 for dirs, 644 for files, 600 for keys |
Forgetting parent x bit | Service cannot traverse /home/user to reach subfolder | Move app files to /srv or /var/www, check with namei -l |
Using mv from /tmp on SELinux | Preserves user_tmp_t context instead of httpd_sys_content_t | Run sudo restorecon -Rv <target> |
| Disabling SELinux globally | Removes mandatory access control for the whole host | Fix labels with restorecon or semanage fcontext |
6. What you learned
You now know how Linux checks directory traversal bits, how to apply separate 755 and 644 permissions cleanly with find, and how to spot and repair SELinux context mismatches without weakening host security.