What the Three Digits Mean
Every file and folder has three sets of permissions: for its owner, for its group, and for everyone else. Each digit is a sum of read (4), write (2), and execute (1):
| Digit | Letters | Meaning |
|---|---|---|
| 7 | rwx | Read, write, execute |
| 6 | rw- | Read and write |
| 5 | r-x | Read and execute |
| 4 | r-- | Read only |
| 0 | --- | Nothing |
So 777 is rwxrwxrwx: the owner, the group, and every other account get all three. On a folder, execute means "may enter it", and write means "may create, rename, and delete any file inside it", including files that belong to other users.
ls -l /var/www/example.com
drwxr-xr-x 5 deploy www-data 4096 Nov 29 10:12 public
-rw-r--r-- 1 deploy www-data 812 Nov 29 10:12 index.php
The first character is d for a directory; the next nine are the three sets. Here the folder is 755 and the file is 644. The columns are explained in the ls command guide.
Why 777 Is Dangerous on a Server
- Anyone can change your code: every account and service on the server, including a compromised application of another site, can rewrite the files.
- Uploaded scripts can run: an attacker who finds any upload hole can drop a script into a world-writable folder the web server executes from.
- Jobs run what others wrote: a world-writable script that cron later runs as root lets any user become root.
- Some tools refuse it: with its default
StrictModessetting, the SSH server refuses key logins when the home folder or the.sshfolder can be written by others. - It hides the cause: the real problem, the file's owner, returns with the next deployment.
Find Out Why the Error Happened
A "permission denied" on a web server nearly always means the process runs as a user that is neither the owner of the file nor in its group. Find that user and look at the path:
ps -eo user,comm | grep -E "nginx|apache|httpd|php-fpm"
namei -l /var/www/example.com/storage/logs
Nginx and Apache workers run as www-data on Debian and Ubuntu, and as nginx or apache on AlmaLinux and Rocky Linux. PHP-FPM runs as the user in its pool file. namei -l shows every folder in the path; one without execute permission blocks everything below it.
The Right Fix: Ownership, 755, and 644
Give the files to the account that deploys them and the group to the web server, then set folders to 755 and files to 644:
sudo chown -R deploy:www-data /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 755 {} +
sudo find /var/www/example.com -type f -exec chmod 644 {} +
Folders and files get separate modes because a plain recursive chmod would mark every file as executable. Files with passwords, such as .env, should be 640. Details are in chown recursive and chmod recursive.
When the Web Server Must Write: Group Write
Upload, cache, and log folders need write access for the web server. Give it to the group, and only on those folders:
sudo find /var/www/example.com/storage -type d -exec chmod 2775 {} +
sudo find /var/www/example.com/storage -type f -exec chmod 664 {} +
775 and 664 let the owner and the group write while others can only read. The leading 2 sets the setgid bit on folders, so new files inherit the folder's group. If the deploy account also needs to write files the web server created, add it to the group. Code folders stay 755 and 644.
On AlmaLinux and Rocky Linux, SELinux can still deny the write even when the mode is right. Label the folder as writable web content instead of turning SELinux off. If semanage is missing, install the policycoreutils-python-utils package:
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/example.com/storage(/.*)?"
sudo restorecon -Rv /var/www/example.com/storage
Find Files That Are Already World-Writable
sudo find /var/www -perm -0002 -type d
sudo find /var/www -perm -0002 -type f
-perm -0002 matches anything that others can write to. Set those paths back with the ownership and modes above. If you find files you did not create, check the site for unknown scripts, as described in how to scan a Linux server for malware.
Is World Write Ever Normal?
Only for shared temporary folders such as /tmp, which carry the sticky bit (mode 1777): anyone may create files, but each user may delete only their own. If something works only with 777, the owner or the group is wrong; fix that instead.