- Linux checks owner, then group, then everyone else, and stops at the first match.
- So a file set to 604 blocks its own group while everyone else can read it.
- Your primary group owns the files you create; secondary groups only grant access.
- umask removes permissions from new files. 022 gives 644 files and 755 directories.
- setgid on a directory makes new files take its group, which keeps a shared site folder consistent.
How does Linux decide who can access a file?
Linux asks three questions in a fixed order. Are you the file's owner? Then the owner permissions apply and the check stops. If not, are you in the file's group? Then the group permissions apply and it stops. Otherwise the permissions for everyone else apply. The first match wins, and nothing later can grant more.
The first match wins, which gives one odd result
A file set to 604 (owner read and write, group nothing, others read) cannot be read by a member of its group, yet a stranger can read it. Linux matched the group, found no permission and stopped. It never falls through to the permissions for others.
$ ls -l /var/www/example.com/wp-config.php
-rw-r----- 1 deploy www-data 3021 Jul 29 09:14 wp-config.php
# - rw- r-- ---
# | | | |
# | | | +-- others: nothing
# | | +------------- group www-data: read
# | +------------------------ owner deploy: read and write
# +----------------------------- file type
# That is 640. Exactly what a config file holding a password should be.What is the difference between a user and a UID?
The username is for people. The kernel (the core of the operating system) only works with numeric user IDs, or UIDs. Ownership and access are decided on the number, and the name is looked up in /etc/passwd for display. That matters when you move files between machines.
# Your numeric identity, and every group you are in
id
# uid=1001(deploy) gid=1001(deploy) groups=1001(deploy),33(www-data)
# Show numeric IDs instead of names
ls -ln /var/www/example.com
# Files whose owner has no matching account (orphaned UIDs)
sudo find /var/www -nouser -o -nogroup 2>/dev/nullWhy ownership can change when you restore an archive
A tar archive records each file's owner by name and by number. Extracted as root, tar uses the name if that account exists on the new server; if it does not, tar falls back to the number, which may belong to someone else there. --numeric-owner makes tar use the numbers only. Always check ownership after restoring on a different server.
| UID range | Purpose |
|---|---|
| 0 | root: permission checks are skipped |
| 1 to 999 | Service accounts: www-data, mysql, sshd |
| 1000 and above | Human accounts |
| 65534 | nobody: the deliberately powerless fallback |
root's power does not come from its permissions: the kernel skips the check for UID 0.
How do primary and secondary groups differ?
Every user has exactly one primary group, recorded in /etc/passwd, and it becomes the group of every file they create. Secondary groups, recorded in /etc/group, grant access to things but never decide the group of anything new.
$ id deploy
uid=1001(deploy) gid=1001(deploy) groups=1001(deploy),33(www-data),1005(webdev)
# ^^^^^^^^^^^^^^^^ primary ^^^^^^^^^^^^^^^^^^^^^ secondary
# deploy is in www-data, so it can READ what www-data can read.
# But new files it creates are owned by group deploy, not www-data,
# so the web server cannot read them.
$ sudo -u deploy touch /var/www/example.com/new.php
$ ls -l /var/www/example.com/new.php
-rw-r--r-- 1 deploy deploy 0 Jul 29 10:02 new.php
# ^^^^^^ not www-data: this is the classic surpriseYou can change the user's primary group, which affects everything they create anywhere. Better, set the setgid bit on the directory, which affects only that folder.
# Set the group on the tree, then make it inherit
sudo chgrp -R www-data /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod g+s {} \;
# Now anything created inside belongs to www-data automatically
sudo -u deploy touch /var/www/example.com/another.php
ls -l /var/www/example.com/another.php
# -rw-r--r-- 1 deploy www-data 0 Jul 29 10:05 another.phpdrwxr-sr-x is setgid. New folders inherit it, so the setup holds without anyone maintaining it.How do chmod and chown actually work?
chmod changes what can be done with a file. chown changes who it belongs to. chmod takes either numbers, which set everything at once, or letters, which change one thing and leave the rest alone.
# Octal: sets all three sets at once
chmod 644 file.txt # rw- r-- r--
chmod 755 script.sh # rwx r-x r-x
chmod 600 ~/.ssh/id_ed25519
# Symbolic: changes one thing, leaves the rest
chmod u+x script.sh # add execute for the owner
chmod g+w shared/ # add write for the group
chmod o-rwx private/ # remove everything for others
chmod a+r public.txt # add read for all three sets
# Directories only, files only: the pattern that matters
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;# Owner only
sudo chown deploy file.txt
# Owner and group
sudo chown deploy:www-data file.txt
# Group only (two equivalent forms)
sudo chown :www-data file.txt
sudo chgrp www-data file.txt
# Recursively across a tree
sudo chown -R deploy:www-data /var/www/example.com
# Copy ownership from an existing file, which avoids typos
sudo chown --reference=/var/www/example.com/index.php newfile.php--reference when you are unsure what the ownership should be: it copies it from a file that is already right.chmod -R 755 on a website is a mistake
It sets the execute bit on every file, images, CSS and PHP included, and a file that genuinely should run becomes impossible to spot. Set directories and files separately with find, as above.
What is umask and why do my new files have the wrong permissions?
umask is a setting that removes permissions from every new file. New files start from 666 and new directories from 777, then the umask is taken away. The usual umask of 022 removes write for the group and others, which is where 644 and 755 come from.
| umask | New files | New directories | Use for |
|---|---|---|---|
| 022 | 644 | 755 | Default: only the owner can write |
| 002 | 664 | 775 | Team directories where the group must write |
| 077 | 600 | 700 | Private: nothing visible outside the owner |
| 027 | 640 | 750 | Owner writes, group reads, others excluded |
umask 022 never removes execute, which is why directories end up 755 rather than 644.
# Current value
umask
# In symbolic form, easier to read
umask -S
# Set for this shell only
umask 002
# Persist for one user
echo 'umask 002' >> ~/.bashrc
# Persist for a systemd service
# In the unit file, under [Service]:
# UMask=0002If files from a deploy script keep coming out unreadable to the web server, check the umask of the process running the script, not the shell you tested in. They are often different.
What do setuid, setgid and the sticky bit do?
Three extra bits sit above the usual nine. You will use setgid on shared directories, see the sticky bit on /tmp, and should check for setuid rather than set it.
| Bit | Octal | On a file | On a directory |
|---|---|---|---|
| setuid | 4000 | Runs as the file's owner, not you | No effect on Linux |
| setgid | 2000 | Runs as the file's group | New files inherit the directory's group |
| sticky | 1000 | No effect | Only the owner of a file can delete it |
setgid on a directory is the useful one. You mostly just need to recognise the other two.
# /tmp: anyone can write, only the owner can delete their own files
ls -ld /tmp
# drwxrwxrwt 12 root root 4096 Jul 29 10:14 /tmp
# ^ the t is the sticky bit
# passwd is setuid root: it must edit /etc/shadow on your behalf
ls -l /usr/bin/passwd
# -rwsr-xr-x 1 root root 68208 ... /usr/bin/passwd
# ^ the s is setuid
# Audit every setuid binary on the system. Do this on any
# server you have inherited.
sudo find / -perm -4000 -type f -ls 2>/dev/nullNever set setuid on your own scripts
Linux ignores setuid on shell scripts anyway, for good reason, and a setuid program that accepts user input is a route to root. If a task needs root, grant a sudo rule for that exact command instead.
For the exact numbers a WordPress install needs, see Linux file permissions for web servers and WordPress. If you would rather not manage server accounts at all, our managed WordPress hosting runs each site in its own isolated container: you look after your site's files and we look after the server.
Frequently asked questions
What is the difference between a primary and secondary group in Linux?
A user has exactly one primary group, and it becomes the group of every file they create. Secondary groups grant access to existing files but never decide the group of new ones. That is why adding someone to www-data does not make their new files readable by the web server.
Why can't a group member read a file that others can read?
Because Linux stops at the first match. If you are in the file's group, the group permissions apply and nothing falls through to the permissions for others. That is why 604 blocks group members but not strangers.
What does umask 022 mean?
It removes write permission for the group and others from anything new. Files start from 666 and become 644; directories start from 777 and become 755. Use 002 when a team shares a directory and everyone needs to edit each other's files.
What does the setgid bit do on a directory?
New files and folders created inside take the directory's group, not the creator's primary group. On a shared site folder that keeps ownership consistent whoever uploads what, so permissions stop drifting. Set it with chmod g+s.
Should I use chmod 777 to fix a permission problem?
No. 777 lets every account and process on the machine rewrite the file. When something only works at 777, the process needs to be in the file's group: fix it with chown and a group-writable 775 folder.
How do I find files with unusual permissions?
sudo find / -perm -4000 -type f -ls lists every setuid file, and find /var/www -perm -o+w -type f lists anything that everyone can write under your web root. Run both on any server you have just taken over.
About G7Cloud Engineering
Articles written by the engineers who build and run G7Cloud: UK managed hosting and the AI Website Builder. We write about what we operate every day: containers, backups, databases, and the small-business websites that run on them.
More about G7Cloud →