Permissions and chmod

Beginner · 6 min

Decode rwx for owner, group and others, set modes with chmod in octal and symbolic form, and learn how directory permissions differ from file permissions.

Before thisListing and inspecting filesCreating, copying, moving and removing

Travis Media · 6 min

Permissions and chmod

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Travis MediaReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Each file has three permission sets, for the owner, the group and everyone else, and each set holds read (r), write (w) and execute (x).
  • In octal, r=4, w=2 and x=1, added per set. 755 is rwxr-xr-x, 644 is rw-r--r-- and 600 is rw-------.
  • chmod u+x file and chmod go-w file adjust single bits. chmod 755 file sets all nine at once.
  • On a directory, r lists names, x lets you enter it and reach files inside, and w lets you create or delete entries. Deleting a file depends on its directory's w, not the file's own mode.
  • A script without x fails with 'Permission denied' (status 126), even though sh script.sh still runs it. New files get their default mode from the umask.

Who may do what

A computer with more than one user has to decide who can read your files, and on a single-user laptop it still needs to stop an ordinary program from overwriting system files. Unix answers with a small table attached to every file: three kinds of people and three kinds of access.

The people are the owner, the group and others. The access types are read, write and execute. That makes nine on/off switches, which ls -l prints as ten characters:

$ ls -ld f d
drwxr-xr-x  d
-rw-r--r--  f

The first character is the type. Then come three triples: owner, group, others. -rw-r--r-- is a file where the owner can read and write, the group can read, and others can read. A dash means the permission is off.

Octal: three digits for nine bits

Each triple is a number: read is 4, write is 2, execute is 1, and you add them up. rw- is 4+2 = 6, r-x is 4+1 = 5, r-- is 4. So -rw-r--r-- is 644, and -rwxr-xr-x is 755. I applied several modes to one file and looked:

$ for m in 644 755 600 700 444 000; do chmod $m f; printf '%s -> ' $m; ls -l f | awk '{print $1}'; done
644 -> -rw-r--r--@
755 -> -rwxr-xr-x@
600 -> -rw-------@
700 -> -rwx------@
444 -> -r--r--r--@
000 -> ----------

(The @ is the macOS extended-attribute marker.) chmod accepts the octal number directly. It rejects nonsense with a message rather than guessing: chmod 999 f printed chmod: Invalid file mode: 999.

Symbolic mode: change one thing

Octal sets all nine bits. When you only want to flip one, name it: who (u, g, o, a for all), an operator (+, -, =), and what (r, w, x):

$ chmod 644 f; chmod u+x f; ls -l f | awk '{print $1}'
-rwxr--r--@
$ chmod go-r f; ls -l f | awk '{print $1}'
-rwx------@
$ chmod a+r,u=rw f; ls -l f | awk '{print $1}'
-rw-r--r--@

u+x added execute for the owner. go-r removed read from group and others. a+r,u=rw combined two edits, with = setting the owner’s bits exactly. A bare chmod +x f gave -rwxr-xr-x in my run. Plain +x respects the umask, covered below, and I saw it grant execute to everyone with a 022 umask.

Running a script needs the x bit

This is where most people first meet permissions. A script is just a text file until it is executable:

$ printf '#!/bin/sh\necho ran\n' > s.sh; chmod 644 s.sh
$ ./s.sh
bash: ./s.sh: Permission denied
$ echo $?
126
$ sh s.sh
ran
$ chmod u+x s.sh; ./s.sh
ran

./s.sh asks the kernel to execute the file, and the kernel refuses without x. Status 126 means “found, but couldn’t execute”, compared with 127 for “not found”. sh s.sh works because sh only needs to read the file. Making this stick is the first-script lesson.

Directories read differently

On a directory the bits mean something else. r lets you list names. x lets you pass through the directory to reach what’s in it (you need it for cd). w lets you create, rename or delete entries. I put a file containing hello in a directory and changed only the directory’s mode. This is a summary of four runs, with the output shortened:

mode 600 (rw-):  ls  -> lists "file" but prints "fts_read: Permission denied"
                 cat dd/file -> Permission denied
mode 300 (-wx):  ls  -> Permission denied     cat dd/file -> hello     touch dd/new -> works
mode 500 (r-x):  ls  -> file                  cat dd/file -> hello     touch dd/new -> Permission denied

With x but no r, you can reach a file if you know its exact name but can’t list the directory. With r but no x, you see names but can’t touch anything inside. Creating a file needs w and x.

The same logic means deleting a file depends on the directory, not the file. A file with mode 444 in a directory I owned was removed without trouble. (My shell had no terminal, so rm did not prompt. At a terminal rm asks “override r–r–r–?” first, which I did not see here.) To stop deletion, remove w from the directory.

Defaults come from the umask

Where does 644 come from for a new file? From the umask, a mask of bits to take away from 666 for files and 777 for directories. Mine was 0022, which gave -rw-r--r-- and drwxr-xr-x. Changing it in a subshell showed the effect:

$ (umask 077; touch private; mkdir pdir; ls -ld private pdir)
-rw-------  private
drwx------  pdir
$ (umask 002; touch group; ls -l group)
-rw-rw-r--  group

Who owns it

chown changes the owner and normally requires root. As an ordinary user I tried chown root f and got chown: f: Operation not permitted. I did not run it with sudo. Special bits such as setuid and the sticky bit are beyond this lesson.

About the video

Travis Media’s “Linux File Permissions in 5 Minutes | MUST Know!” was published 4 October 2023 and runs 6 minutes 3 seconds. The description lists chapters for permission groups, “Read, write, execute”, a reading example, chown (2:51), chmod (3:44), the “Linux scoring system” (3:53) and common permissions (5:02), which matches the octal section here. English per its title and description. I couldn’t read a transcript, so I haven’t verified details like whether it shows symbolic mode. The description has affiliate links and promotes his course and community. The directory behaviour and the umask are my additions.

What I did not run

sudo chown, chgrp, ACLs, setuid, setgid, the sticky bit, and anything on Linux. The rm prompt on a read-only file at a real terminal.

Next

Permissions say who can use a file. The next lesson shows how one file can have two names: hard and symbolic links.

Check yourself

  1. 01What does mode 640 mean, bit by bit?

    Owner has read and write (6), the group has read only (4) and others have nothing (0), so the listing shows rw-r-----.

  2. 02You write a script and run ./deploy.sh, and get 'Permission denied'. What is the fix?

    The file lacks the execute bit. Run chmod u+x deploy.sh (or chmod 755), and then ./deploy.sh will start.

  3. 03A file is mode 444 and you own the directory it is in. Can you delete it?

    Yes. Removing a file changes the directory's entry, so it needs write permission on the directory, not on the file.

  4. 04Why does cd fail on a directory that you can read but not execute?

    On directories, x means permission to enter and reach what is inside. With r but not x you can list names, but cd and opening files inside are denied.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark