Linux7 min readPublished: 19 Oct 2024Updated: 9 Nov 2026

Linux Users, Groups and Permissions Explained

How Linux users, groups and file permissions fit together: UIDs, primary and secondary groups, chmod and chown, umask and the special permission bits.

G7Cloud Engineering
Platform team
Share:
Abstract illustration representing linux users, groups and permissions explained.
  • 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.

Seeing the three sets
$ 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.
deploy can edit it, the web server can read it, and no other account on the machine can see the database password.

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.

Names are a display layer
# 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/null
Orphaned UIDs appear after restoring a backup from another server, or after deleting a user who owned files. Listing users on Linux covers finding the accounts behind them.

Why 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 rangePurpose
0root: permission checks are skipped
1 to 999Service accounts: www-data, mysql, sshd
1000 and aboveHuman accounts
65534nobody: 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.

The distinction in practice
$ 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 surprise
This is why a deploy that seemed to work can leave the web server unable to read the new files.

You 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.

setgid: the directory decides the group
# 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.php
The s in the group position of drwxr-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 and symbolic
# 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 {} \;
The letter form is safer on a live server: it cannot strip a permission you were not thinking about.
chown and chgrp
# 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
Use --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.

umaskNew filesNew directoriesUse for
022644755Default: only the owner can write
002664775Team directories where the group must write
077600700Private: nothing visible outside the owner
027640750Owner writes, group reads, others excluded

umask 022 never removes execute, which is why directories end up 755 rather than 644.

Checking and setting umask
# 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=0002
A service takes its umask from its unit file, not a shell profile. That catches people out with PHP-FPM and deploy agents.
Pro Tip

If 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.

BitOctalOn a fileOn a directory
setuid4000Runs as the file's owner, not youNo effect on Linux
setgid2000Runs as the file's groupNew files inherit the directory's group
sticky1000No effectOnly 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.

Where you meet them
# /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/null
That last command is a standard security check, worth running alongside a patching routine. An unexpected setuid file in /tmp or a home directory is serious.

Never 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.

Put this into practice on G7Cloud

Every site runs in its own dedicated container behind ScaleShield, with daily backups that are restore-tested every night. Start on the free plan, no card needed.

About G7Cloud Engineering

Platform team

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 →