Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 95 ยท Working with Files

File Permissions and Ownership

Understand Linux file permissions: read ls -l, rwx for user, group and other, chmod in symbolic and octal form, umask, directory permissions, setuid, setgid and the sticky bit.

Linux is a multi-user system. Even your laptop has many users: you, plus system accounts that run services such as web servers and databases. Permissions decide who may read, change or run each file, and they are what keeps one user's mistakes (or one hacked service) from destroying everything else. They are also behind two of the most common error messages you will ever meet: Permission denied and command not found for a script you just wrote.

The model is small. Every file has an owner, a group and a set of permission bits. This lesson shows how to read them in ls -l, change them with chmod in both its symbolic and its octal form, understand umask, make scripts executable, and meet the three special bits (setuid, setgid, sticky). It follows links and is the foundation for Users, Groups and sudo.

Reading ls -l

TEXT
-rwxr-xr--  1 ada  staff  2048 Jan  1 12:00 deploy.sh
โ”‚โ””โ”ฌโ”˜โ””โ”ฌโ”˜โ””โ”ฌโ”˜    โ”‚   โ”‚      โ”‚
โ”‚ โ”‚  โ”‚  โ”‚     โ”‚   โ”‚      โ””โ”€โ”€ size in bytes
โ”‚ โ”‚  โ”‚  โ”‚     โ”‚   โ””โ”€โ”€ group
โ”‚ โ”‚  โ”‚  โ”‚     โ””โ”€โ”€ owner (user)
โ”‚ โ”‚  โ”‚  โ””โ”€โ”€ permissions for everybody else (other)
โ”‚ โ”‚  โ””โ”€โ”€ permissions for the group
โ”‚ โ””โ”€โ”€ permissions for the owner (user)
โ””โ”€โ”€ file type: - file, d directory, l symlink

The first character is the type; the next nine are three groups of three. Each group lists read, write and execute, with - for "not allowed". The three classes are:

  • user (u): the owner of the file.
  • group (g): members of the file's group.
  • other (o): everybody else.

The system checks these in order and applies only the first class that matches: if you are the owner, the user bits decide (even if the group or other bits would be more generous); otherwise, if you are in the group, the group bits decide; otherwise the other bits.

The three permissions mean different things for files and directories:

Bit On a regular file On a directory
r read the content list the names inside (ls)
w change the content create, delete and rename entries inside
x run it as a program enter it (cd) and access entries by name

Two consequences surprise beginners. To delete a file you need w on the directory containing it, not on the file itself. And a directory without x is unusable even if you can read it, as the next example shows.

Bash Reading permissions with ls and stat

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 read or cat), it gets the next line from this box.

(The chmod g-s . after cd demo is a sandbox detail: the temporary directory we run in has the setgid bit, which new subdirectories would inherit and which would show up as an s in their modes. We explain that bit at the end of the lesson. We pick umask 022 so that results do not depend on the machine; we cover umask below. The awk keeps only the mode and the name, because owner and group names differ from machine to machine; in the sandbox they are not even defined.)

chmod: changing permissions

chmod accepts two notations. They do the same thing; use whichever is clearer.

Symbolic mode

TEXT
chmod WHO OPERATOR PERMS file...
        u g o a   + - =   r w x

u+x adds execute for the owner, go-w removes write from group and other, a=r makes it read-only for all, u=rw,go=r sets exact values, and +x alone means "for all, subject to umask". The big advantage is that it changes only the bits you name and leaves the rest alone.

Octal mode

Each permission has a value: r = 4, w = 2, x = 1. Add them up for each class, and write the three digits in the order user, group, other.

TEXT
rwx = 4+2+1 = 7        r-x = 4+0+1 = 5        rw- = 4+2+0 = 6
r-- = 4                 --x = 1                --- = 0

chmod 755 file   โ†’  rwxr-xr-x     (programs, directories)
chmod 644 file   โ†’  rw-r--r--     (ordinary files)
chmod 600 file   โ†’  rw-------     (private files such as SSH keys)
chmod 700 dir    โ†’  rwx------     (private directories)
Bash chmod: symbolic and 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 read or cat), it gets the next line from this box.

Two details. chmod g=u copies the owner's bits to the group, a neat idiom. And the capital X is the answer to "I want directories enterable but plain files not executable": in chmod -R a+rX dir, X sets execute only on directories (and on files that are already executable for someone). That is much better than a blanket chmod -R 755, which makes every document executable.

If you remember only one rule: files 644, directories 755, private things 600/700, scripts 755. Never use chmod 777; it means everybody can modify the file, which on a server is almost always the wrong fix for a permission problem.

Executable scripts

A file can only be run as a program (./script.sh) if it has the execute bit. Without it, the kernel refuses with Permission denied, even though you can read it. A script also needs a first line starting with #! (the shebang) to say which interpreter to use (see Writing Your First Script).

Bash Making a script executable

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 read or cat), it gets the next line from this box.

Notice bash hello.sh: when you pass the file to an interpreter, the interpreter only has to read it, so the execute bit is not needed. This is also why a script on a filesystem mounted noexec can still be run with bash script.sh.

Directory permissions in action

Directory behaviour is the place where intuition fails most often, so let's test every combination of r, w and x.

Bash What r, w and x do on a directory

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 read or cat), it gets the next line from this box.

The r-x case is the most common: you can enter and read, but not create or delete. The last lines show the rule from earlier: with w on the directory, the file's own mode (even 000) does not stop you from removing it, because removing means editing the directory.

umask: default permissions for new files

New files are not created with arbitrary modes. A program asks for a mode (usually 666 for files and 777 for directories) and the kernel subtracts the bits in the process's umask. The common umask 022 removes write for group and other, giving 644 files and 755 directories. umask 077 makes everything private (600/700); umask 002 suits shared groups (664/775).

TEXT
requested   666  =  110 110 110        directories: 777
umask       022  =  000 010 010
                    --------------- remove the umask bits
result      644  =  110 100 100        directories: 755
Bash umask changes what new files get

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 read or cat), it gets the next line from this box.

The loop uses a subshell (parentheses) so that each umask only affects that group of commands; see Subshells, Groups and Environment. Your login shell's umask normally comes from /etc/profile or ~/.profile.

Notice that umask only removes bits. A new file never gets the execute bit by default, even with umask 000; you have to chmod +x it.

Ownership: chown and chgrp

chown USER:GROUP file changes the owner and group; chgrp GROUP file changes only the group. Only root can give a file away or change its owner, which stops users from filling each other's quota. An ordinary user may change the group of their own files to a group they belong to.

CONSOLE
$ sudo chown www-data:www-data /var/www/html/index.php
$ sudo chown -R ada: project/          # owner ada, her default group, whole tree
$ chgrp developers shared.txt          # allowed if you are in "developers"

These commands are shown read-only, because the sandbox has no root and no such users. What we can see is the refusal, which is also what you would get on your machine as an ordinary user.

Bash chown without being root (fails on purpose)

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 read or cat), it gets the next line from this box.

The first message, Operation not permitted, is what every ordinary user gets when trying to give a file to somebody else. To see who you are and which groups you belong to, run id; the lesson Users, Groups and sudo goes deeper.

Special bits: setuid, setgid and sticky

Above the nine ordinary bits there are three more, shown in the x positions of ls -l.

Bit Octal On a file On a directory
setuid 4000 Program runs with the owner's identity (s in the user x slot) ignored
setgid 2000 Program runs with the group's identity (s in the group x slot) New files inherit the directory's group
sticky 1000 ignored (historic) Only the file's owner (or the directory's owner or root) may delete or rename entries (t in the other x slot)

Real-world examples: /usr/bin/passwd is setuid root, so ordinary users can change their own password even though /etc/shadow is root-only; /tmp has the sticky bit (drwxrwxrwt) so that everybody can create files there but nobody can delete other people's files; shared project directories use setgid so that new files keep the project's group.

A capital S or T means the special bit is set but the matching x is not.

Bash Special bits in ls -l

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 read or cat), it gets the next line from this box.

setuid programs are the most security-sensitive things on a system, because a bug in them is a bug running as root. The kernel ignores setuid on interpreted scripts (#! files) for exactly that reason. You rarely set these bits yourself, but knowing how to spot them is useful: find / -perm -4000 -type f 2>/dev/null lists the setuid files on a machine, and security audits start with this list.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does chmod 640 report.txt give?

    6 = rw- for the owner, 4 = r-- for the group, 0 = --- for others, so the result is rw-r-----.
  2. 2. You can read a directory (r) but it has no x bit. What happens with cat dir/file.txt?

    Using a name inside a directory needs x (search) on that directory. With r alone you can list the names, but not open the files.
  3. 3. Which permission do you need on the DIRECTORY to delete a file inside it?

    Deleting removes a directory entry, so you need w and x on the directory. The file's own permissions are irrelevant (except with the sticky bit).
  4. 4. With umask 027, which mode does a new ordinary file get (assuming the program asks for 666)?

    027 removes write from the group (2) and everything from others (7): 666 minus those bits gives 640.
  5. 5. chmod -R a+rX dir differs from chmod -R 755 dir because...

    The capital X adds execute only for directories and for files that already have an execute bit. Plain 755 would make every regular file executable.

Summary

  • Each file has an owner, a group and three classes of bits (user, group, other) for read, write and execute; the first matching class decides.
  • On directories, r lists, w changes entries (create, delete, rename) and x allows entering and using names.
  • chmod takes symbolic (u+x, go-w, a=r, X) or octal (r=4, w=2, x=1) modes; typical modes: files 644, directories and programs 755, private 600/700.
  • A script needs x to run as ./script, but bash script only needs read.
  • umask removes bits from the default 666/777.
  • Only root can change an owner; chgrp works for groups you belong to.
  • setuid, setgid and sticky are the special bits (s, s, t in ls -l).

Next: Archives and Compression