Server Administration
Linux File Permissions Explained: chmod, chown, umask and Special Bits
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربي"Permission denied" is one of the first errors every Linux user meets, and chmod 777 is one of the worst ways to fix it. Permissions decide which user can read a config file, which process can write to an upload folder, and who can run a script. Once you understand the model, you can fix access problems precisely instead of opening everything to everyone.
Reading permissions with ls -l
Run ls -l in any directory:
-rw-r--r-- 1 www-data www-data 1024 Nov 22 10:00 index.php
drwxr-x--- 2 deploy deploy 4096 Nov 22 10:00 privateThe first column has ten characters:
- Character 1 is the type:
-for a file,dfor a directory,lfor a symbolic link. - Characters 2-4 are the owner (user) permissions.
- Characters 5-7 are the group permissions.
- Characters 8-10 are permissions for others (everyone else).
The third and fourth columns show the owning user and group.
What r, w and x mean
| Letter | On a file | On a directory |
|---|---|---|
r (read) | Read the contents | List the names inside |
w (write) | Modify the contents | Create, rename or delete entries inside |
x (execute) | Run it as a program or script | Enter it and access entries by name |
The directory column surprises many people. Deleting a file depends on write permission on the directory, not on the file. And without x on a directory, you cannot reach anything inside it even if the files themselves are readable.
Changing permissions with chmod
Symbolic mode
Symbolic mode combines who (u user, g group, o others, a all), an operator (+, -, =) and permissions:
chmod u+x deploy.sh # let the owner execute the script
chmod g-w config.yml # remove write from the group
chmod o= secrets.env # remove all permissions for others
chmod a+r public.txt # everyone can readNumeric (octal) mode
Each permission has a value: r = 4, w = 2, x = 1. Add them for each of user, group and others:
| Number | Permissions | Typical use |
|---|---|---|
755 | rwxr-xr-x | Directories, executable scripts |
644 | rw-r--r-- | Regular files, web pages |
600 | rw------- | Private keys, .env files |
700 | rwx------ | Private directories such as ~/.ssh |
640 | rw-r----- | Config readable by a service group |
chmod 600 ~/.ssh/id_ed25519
chmod 700 ~/.sshTo apply changes recursively, use -R, but be careful: files and directories usually need different modes. A safer pattern for a website is:
find /var/www/site -type d -exec chmod 755 {} +
find /var/www/site -type f -exec chmod 644 {} +Changing ownership with chown and chgrp
Ownership often matters more than mode. If a web server runs as www-data and needs to write to an uploads folder, give it ownership of that folder rather than opening permissions to everyone:
sudo chown -R www-data:www-data /var/www/site/uploads
sudo chown deploy file.txt # change owner only
sudo chgrp developers project/ # change group onlyAdd a user to a group with sudo usermod -aG developers alice. The user must log out and back in for the new group to apply. Check group membership with id alice.
Default permissions with umask
When a program creates a file, the system starts from 666 for files and 777 for directories, then removes the bits set in the umask. The common default umask 022 produces 644 files and 755 directories. A stricter 027 produces 640 and 750, keeping files hidden from other users.
umask # show the current value
umask 027 # set it for the current sessionServices managed by systemd can set their own value with UMask= in the unit file.
Special bits: setuid, setgid and sticky
A fourth, leading digit controls three special permissions:
- setuid (4) on an executable makes it run with the file owner's privileges.
/usr/bin/passwduses it so normal users can update their password. It appears assin the owner execute position. - setgid (2) on an executable runs it with the file's group. On a directory, new files inherit the directory's group, which is useful for shared team folders:
chmod 2775 /srv/shared. - sticky bit (1) on a directory means only a file's owner (or root) can delete it, even if others have write access.
/tmpuses mode1777, shown astat the end.
Audit setuid files periodically, since they are a common target for privilege escalation:
sudo find / -perm -4000 -type f 2>/dev/nullWhy chmod 777 is a bad idea
Mode 777 lets every user and every process on the server modify, replace or delete the file. On a web server, a single vulnerable plugin can then plant malicious code anywhere it can write. The right fix is almost always to set the correct owner or group and keep modes such as 755 and 644.
Troubleshooting checklist
- Run
ls -lon the file andnamei -l /full/path/to/fileto see permissions on every directory along the path. - Check which user the process runs as, for example with
ps aux | grep nginx. - Fix ownership with
chownbefore touching modes. - On AlmaLinux and Rocky, remember that SELinux can deny access even when permissions look correct; check with
ls -Z.
Permissions go hand in hand with SSH hardening, so continue with our guide on securing SSH with keys. If you are just starting, review our Linux basics for beginners first.
FAQ
What permissions should website files have?
A common, safe baseline is 755 for directories and 644 for files, owned by your deploy user, with only the folders the application must write to (uploads, cache) owned by or writable for the web server user. Sensitive files such as .env should be 600 or 640.
What is the difference between chmod and chown?
chmod changes what the owner, group and others are allowed to do. chown changes who the owner and group are. Many permission problems are solved by correcting ownership rather than loosening the mode.
Why do I get permission denied even as the file owner?
Usually because a parent directory lacks execute permission for you, or because SELinux or AppArmor is blocking access. Use namei -l to inspect the whole path, and check security module logs if the permissions look correct.