Lesson 4 of 12 · 10 min · Users and access

Permissions and ownership

Covers: Ubuntu 24.04 LTS, bash 5.2, GNU coreutils 9.4. Updated 13 Nov 2026. Part of the DevOps Engineer roadmap.

What you will learn

  • How to read the ten characters at the start of ls -l
  • What read, write and execute mean for a file and for a directory
  • chmod in symbolic (u+x) and numeric (640) form, and chown
  • What umask does and why new files are 644 and new directories 755
  • The special bits: setuid, setgid and the sticky bit

In simple words

Every file in Linux has a lock with three sets of keys: one for its owner, one for a group of people, and one for everybody else. Each set says whether that person may read the file, change it, or run it. This lesson teaches you to read those locks and change them safely.

Words to know

  • Owner: the user a file belongs to.
  • Group: a named set of users who share the same access to a file.
  • Permission: what someone may do with a file: read it (r), write to it (w) or run it (x).
  • chmod: the command that changes a file's permissions.

Every file has an owner, a group and three sets of permissions

touch f
ls -l f
-rw-r--r-- 1 root root 0 Nov 12 12:28 f

Read it from the left:

-  rw-  r--  r--   1   root  root   0   Nov 12 12:28   f
|  |    |    |         |     |                          name
|  |    |    others     owner group
|  |    group
|  owner (user)
type: - file, d directory, l symlink

Linux checks who you are against these three classes, in this order: if you are the owner, the owner bits apply; otherwise if you are in the file's group, the group bits apply; otherwise the others bits apply. Only one class is used. The administrator, root, bypasses these checks.

Each class has three bits:

Bit On a file On a directory
r read read the contents list the names inside
w write change the contents create, delete and rename entries inside
x execute run it as a program enter it (cd) and reach things inside by name

Two consequences surprise beginners:

  1. To delete a file you need write permission on its directory, not on the file.
  2. A directory needs x for you to use anything inside it. r without x lets you list names but not open them.

Numbers and letters

Each bit has a value: r=4, w=2, x=1. Add them per class: rw- is 6, r-- is 4. So rw-r----- is 640.

chmod 640 f
stat -c '%A %a %U:%G %n' f       # %A symbolic, %a octal, owner:group, name
-rw-r----- 640 root:root f

Symbolic mode changes only what you say. u user (owner), g group, o others, a all; + adds, - removes, = sets:

chmod u+x,g-r f      # owner gets execute, group loses read
stat -c '%A %a' f
chmod o+r f          # others get read
stat -c '%A %a' f
-rwx------ 700
-rwx---r-- 704

Common numbers you should recognise: 644 (file everyone can read, owner can write), 755 (program or directory everyone can use, owner can change), 600 (private file such as an SSH private key), 700 (private directory), 664/775 (group-shared). 777 means anyone can change it, and it is almost never the right answer; if a deployment "needs 777", something else (the owner or group) is wrong.

chmod -R applies to a whole tree. Be careful: giving every file x is usually wrong. Directories need x, regular files mostly don't. The capital X helps: chmod -R u=rwX,g=rX,o= dir sets x only on directories (and files that already have it).

Changing the owner

sudo chown asha f            # change owner
sudo chown asha:devs f       # owner and group
sudo chown -R asha:devs app/ # a whole directory tree
sudo chgrp devs f            # group only

Only root can give a file away; ordinary users can change the group of their own files to a group they belong to. After chown asha:devs a.txt:

-rw-r--r-- 1 asha devs 8 Nov 12 12:28 a.txt

Why services care

A web server runs as a low-privilege user (for example www-data or nginx). If it cannot read its config or write its log directory, the problem is almost always an owner/group/mode mismatch, not the application. namei -l /path/to/file prints the permissions of every directory on the way, which finds the blocked one quickly.

umask: the default for new files

New files start as 666 and new directories as 777, then the umask removes bits:

umask
touch u; mkdir ud
stat -c '%a' u ud
0022
644
755

022 removes write from group and others: files become 644, directories 755. A stricter umask 077 makes new files private (600). Services and scripts often set it on purpose when they create secrets.

The special bits

mkdir /srv/team
chgrp devs /srv/team
chmod 2770 /srv/team
ls -ld /srv/team
drwxrws--- 2 root devs 4096 Nov 12 12:28 /srv/team
Bit Number Effect
setuid 4000 An executable runs with its owner's privileges. /usr/bin/passwd is root-owned and setuid so any user can change their own password. Shown as s in the owner's x slot.
setgid 2000 On an executable: runs with the file's group. On a directory: new files inside inherit the directory's group. That is how shared team folders work, as above (s in the group slot).
sticky 1000 On a directory: you may only delete or rename entries you own, even if you can write to the directory. /tmp has it: drwxrwxrwt (the t).

Setuid programs are a security risk if they have bugs, so audit them: find / -perm -4000 -type f 2>/dev/null.

Common mistakes

  • chmod 777 to make an error disappear. It hides a wrong owner. Fix the owner or group.
  • Forgetting directory x. "Permission denied" on a file you can read often means a parent directory lacks x for you.
  • chmod -R 644 on a tree. Directories lose x and become unusable.
  • SSH key permissions. ssh refuses a private key that others can read. Use chmod 600 ~/.ssh/id_ed25519 and chmod 700 ~/.ssh.

Try it yourself

  1. Create a file, set it to 640, and write down who can read it. Then change it with symbolic mode so the group can also write.
  2. As a normal user (root ignores these checks), make a directory with a file in it, remove x from your own permissions on the directory, and try cd into it, then ls it. Explain the results.
  3. Create /tmp/shared with the setgid bit and a group, create a file in it as another user (or just as yourself) and check that the file's group is inherited.
  4. Run ls -l /etc/passwd /etc/shadow and explain why the second file is stricter.

Check yourself

A file is -rw-r----- with owner asha and group devs. What can user ravi (in group devs) and user kiran (not in devs) do?

Ravi matches the group class, so he can read but not write. Kiran falls into "others", which has no permissions, so he can do nothing with it.

What does chmod 755 dir mean for a directory?

Owner can list, enter and change entries (7); group and others can list and enter but not change (5). It is the normal mode for shared directories and programs.

A user has write permission on a file but still cannot delete it. Why?

Deleting removes a name from the directory, so what matters is write (and execute) permission on the directory, plus the sticky bit if set.

Why is /tmp mode 1777?

Everyone must be able to create files there (777), and the sticky bit (1) prevents users from deleting each other's files.

Interview questions

Explain the permission string -rwxr-x--- 1 deploy www app.sh.

A regular file named app.sh, owned by user deploy and group www. The owner can read, write and execute; the group can read and execute; others have no access. In numbers, 750.

What is setuid, and give an example.

A bit on an executable that makes it run with the file owner's privileges instead of the caller's. passwd is the standard example: it must write /etc/shadow, which only root can.

A web server returns 403 on files you just uploaded. What do you check?

The files' mode and owner (does the web server's user have read?), execute on every parent directory (namei -l), and then any security layer such as SELinux or AppArmor.

Summary

  • ls -l shows type, owner, group and three permission sets; the first matching class applies.
  • On directories, w means create/delete entries and x means enter; file deletion depends on the directory.
  • chmod takes numbers (4/2/1) or symbolic changes; chown user:group sets ownership; prefer fixing ownership to opening permissions.
  • umask subtracts from the defaults; setuid, setgid and sticky solve privilege, shared-folder and /tmp problems.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on Linux for DevOps for DevOps Engineer and scores every answer, with what to fix.