Stop making your recursive permissions a security nightmare

Permissions & Security (Chmod/Chown/ACLs/SELinux/AppArmor)

Stop making your recursive permissions a security nightmare

🧩 The Challenge

Ever needed to fix permissions on a whole directory tree, say for a new web app or a Git repo clone? Too often, folks just `chmod -R 777 .` or `666 .` and call it a day. And then they wonder why their site is slow, or worse, compromised. It’s a mess, and it exposes things that shouldn’t be exposed.

💡 The Fix

Don’t nuke everything with one broad stroke. You need to apply sane defaults: read/execute for directories, read/write for files, with execution bit for the owner, but not the whole world. A couple of `find` commands can fix this precisely.

find /var/www/mysite -type d -exec chmod 755 {} +;
find /var/www/mysite -type f -exec chmod 644 {} +;
chown -R www-data:www-data /var/www/mysite

⚙️ Why It Works

These commands treat directories and files differently, which is how it should be. Directories need execute permissions (`x`) to be *traversed*, not necessarily to *run* anything. Files usually don’t need execute unless they’re actual scripts. The `+` at the end makes `find` pass multiple results to a single `chmod` call, making it way faster on large trees.

🚀 Pro-Tip: For scripts inside that tree, use `find . -type f -name “*.sh” -exec chmod +x {} +;` *after* setting the base file permissions.

Linux Tips & Tricks | © ngelinux.com | 11/3/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted