Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 37 ยท Users, Groups and Permissions

File Permissions, Ownership and umask

Read and change Linux permissions: rwx bits, octal modes, chmod, chown, umask defaults and the special setuid, setgid and sticky bits for shared directories.

Permissions decide who can read, write or run each file. They are the core of Linux access control and the first place to look when something works for root but fails for a service. This lesson covers the nine basic bits, the special bits and the default mask.

Reading ls -l

Terminal
ls -l /etc/passwd /usr/bin/passwd
# -rw-r--r-- 1 root root  ... /etc/passwd
# -rwsr-xr-x 1 root root  ... /usr/bin/passwd

The first character is the type (- file, d directory, l link). Then three triplets for user (owner), group and others. Each triplet is read, write, execute.

Bit On a file On a directory
r (4) Read contents List names
w (2) Modify contents Create, rename, delete entries (needs x too)
x (1) Run as a program Enter it and access entries by name

Add the numbers for an octal mode: rwxr-x--- is 750; rw-r--r-- is 644.

chmod

Terminal
chmod 640 secrets.conf          # numeric
chmod u+x script.sh             # symbolic: add execute for owner
chmod go-w file                 # remove write for group and others
chmod -R g+rwX /srv/project     # capital X = x only on directories and already-executable files

Use -R with care; run ls first to be sure of the path.

chown and chgrp

Terminal
sudo chown dana file
sudo chown dana:developers file
sudo chown -R www-data:www-data /var/www/site
sudo chgrp developers /srv/project

Only root can give a file to another user. The owner can change the group to one they belong to.

Special bits

Bit Octal On a file On a directory
setuid 4000 Runs with the file owner's rights Ignored
setgid 2000 Runs with the group's rights New files inherit the directory's group
sticky 1000 Ignored Only a file's owner may delete it
Terminal
chmod 2775 /srv/project      # setgid: files keep the group "developers"
chmod 1777 /tmp              # sticky: everyone writes, only owners delete
find / -xdev -perm -4000 -type f 2>/dev/null    # list setuid programs

Setuid programs such as passwd are a security risk if buggy, so audit them regularly (later chapter). A shared project directory usually combines a group, mode 2770 or 2775 and the right umask.

umask

The umask removes permission bits from newly created files. Files start from 666 and directories from 777; the umask is subtracted bit by bit.

Terminal
umask            # show, e.g. 0022
umask 027        # new files 640, directories 750
umask New file New directory
022 644 755
027 640 750
077 600 700
002 664 775

Set a persistent umask in /etc/login.defs (UMASK), /etc/profile, or per service with UMask= in a systemd unit.

flowchart LR
    A["Request: open file"] --> B{"Same owner?"}
    B -->|Yes| C["Check user bits"]
    B -->|No| D{"In file's group?"}
    D -->|Yes| E["Check group bits"]
    D -->|No| F["Check other bits"]

The kernel picks one triplet, not the most generous one.

Note that if you are the owner, only the owner bits apply, even if group or other bits would allow more.

Troubleshooting permission denied

  1. ls -ld the file and every directory above it; you need x on all of them.
  2. id to see the process's user and groups (they must be current).
  3. namei -l /path/to/file prints permissions for every path component.
  4. Check for ACLs (getfacl), a read-only mount, SELinux or AppArmor denials.
Terminal
namei -l /var/www/site/index.html
sudo -u www-data test -r /var/www/site/index.html && echo readable

A stand-in: octal converter

This model converts between rwx strings and octal. It is plain Python for practice.

Python Stand-in: rwx to octal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A stand-in: applying a umask

This model applies a umask to the default modes and shows the result.

Python Stand-in: what does umask do?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • chmod 777 to make a problem go away; it removes all protection.
  • Giving directories no x bit.
  • chmod -R 644 on a tree, which strips execute from directories.
  • Forgetting that new group membership needs a new login.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the octal mode for rwxr-x---?

    rwx is 7, r-x is 5, --- is 0.
  2. 2. What does a umask of 027 give a new regular file?

    666 with the 027 bits removed leaves 640.
  3. 3. Which special bit makes new files inherit a directory's group?

    setgid on a directory keeps the group for new entries.
  4. 4. Why does a directory need the x bit?

    Without x you cannot traverse the directory.

Summary

  • Permissions are r, w, x for user, group and others; octal adds 4, 2, 1.
  • chmod changes modes, chown and chgrp change owners.
  • setuid, setgid and sticky bits give special behavior; use setgid for shared folders.
  • umask sets defaults; namei -l finds blocked path components.

Next: sudo and Privilege Management