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 private

The first column has ten characters:

  • Character 1 is the type: - for a file, d for a directory, l for 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

LetterOn a fileOn a directory
r (read)Read the contentsList the names inside
w (write)Modify the contentsCreate, rename or delete entries inside
x (execute)Run it as a program or scriptEnter 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 read

Numeric (octal) mode

Each permission has a value: r = 4, w = 2, x = 1. Add them for each of user, group and others:

NumberPermissionsTypical use
755rwxr-xr-xDirectories, executable scripts
644rw-r--r--Regular files, web pages
600rw-------Private keys, .env files
700rwx------Private directories such as ~/.ssh
640rw-r-----Config readable by a service group
chmod 600 ~/.ssh/id_ed25519
chmod 700 ~/.ssh

To 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 only

Add 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 session

Services 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/passwd uses it so normal users can update their password. It appears as s in 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. /tmp uses mode 1777, shown as t at the end.

Audit setuid files periodically, since they are a common target for privilege escalation:

sudo find / -perm -4000 -type f 2>/dev/null

Why 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

  1. Run ls -l on the file and namei -l /full/path/to/file to see permissions on every directory along the path.
  2. Check which user the process runs as, for example with ps aux | grep nginx.
  3. Fix ownership with chown before touching modes.
  4. 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.

Related guides