LearnWithShobhan

Linux file permissions explained: ls -l, chmod and chown (with diagrams)

Shobhan Biswas··7 min read
>_From the course CLI Mastery: 57 lessons, lifetime access.

"Permission denied" is one of the first walls everyone hits in the terminal. The fix is rarely hard. What makes it feel hard is that the output of ls -l looks like noise until someone explains it once, column by column.

So let's do exactly that. By the end you will be able to read any permission string, and fix it with chmod and chown.

Why Unix has users and groups at all

Back when computers were expensive, a university might have one computer with many terminals wired to it: one for the physics department, one for maths, one for administration. Inside physics alone there would be teachers and students, each needing their own files, and some shared ones.

One machine, many people. Unix solved this with users (every person has an account) and groups (a set of users who share access). That multi-user design is still how every Linux server, Mac and cloud VM works today.

Diagram: several terminals connected to one central computer, users grouped inside a department
One computer, many terminals and users. Groups let a whole department share access.

You can see who you are and which groups you belong to:

bash
$ whoami
shobhan
$ id -gn
staff

id -gn prints your primary group. Every user belongs to at least one group, and that primary group matters, as you will see in a second.

Read the output of ls -l, column by column

Desktop — zsh
$ ls -l
total 24
drwxr-xr-x 4 shobhan staff 128 Dec 1 14:19 animal
-rw-r--r-- 1 shobhan staff 2347 Nov 30 13:18 big-file.txt
-rw-r--r-- 1 shobhan staff 18 Dec 1 16:42 file-1.txt
lrwxr-xr-x 1 shobhan staff 14 Dec 2 22:02 sm-config -> ./dir-1/config
ColumnExampleMeaning
1st characterd, -, lFile type: d directory, - regular file, l symbolic link (b for block devices like a mounted drive)
Next 9 charactersrwxr-xr-xThe permissions (explained below)
Number4Hard link count. Directories usually have several, regular files usually 1
UsershobhanThe user who owns the file
GroupstaffThe group that owns the file
Size2347Size in bytes. Use ls -lh for human readable sizes like 2.3K
DateNov 30 13:18Last modification time
Namesm-config -> ./dir-1/configFile name. Symbolic links also show where they point
ls -l output with the owner column and group column circled
The two ownership columns: the user that owns the file, and the group that owns it.

Ownership: who owns a new file?

When you create a file, you become its owner, and your primary group becomes its group. Everyone else on the system is called others.

So every file has exactly three audiences:

  1. User (the owner)
  2. Group (members of the file's group)
  3. Others (everyone else)

Permissions: read, write, execute

Each audience can have three permissions:

  • r (read): see the contents of the file.
  • w (write): modify the file. Deleting also needs write permission.
  • x (execute): run the file as a program. In Unix everything is a file, including programs like ls itself, and you need x to run one.

Now the nine characters make sense. They are three blocks of three: user, then group, then others. A letter means the permission is granted, a dash means it is not.

-rw-r--r-- (a normal file)

  • ▸User: read + write
  • ▸Group: read only
  • ▸Others: read only

-rwxr-xr-x (a script or directory)

  • ▸User: read + write + execute
  • ▸Group: read + execute
  • ▸Others: read + execute

Change ownership with chown

The syntax is chown user:group path. Let's try to hand a file to root:

bash
$ chown root:everyone file-2.txt
chown: file-2.txt: Operation not permitted
$ sudo chown root:everyone file-2.txt
Password:

You cannot give your file away as a normal user, so you run it with sudo. After that, ls -l shows root everyone for that file.

Terminal: chown fails with Operation not permitted, then works with sudo
chown fails without sudo, then works with it. (sudo also remembers your password for a few minutes.)

Change only one of the two:

sudo chown bob file.txt          # only the user
sudo chown :developers file.txt  # only the group
sudo chown -R www-data:www-data /var/www/html   # a whole directory, recursively

Change permissions with chmod (symbolic mode)

chmod means "change mode". The symbolic form is: who (u user, g group, o others), then =, + or -, then the permissions.

Here is a real session. Notice how each command changes one part of the string:

Terminal: chmod u+w, o-r, o+x and u=rw,go=r each followed by ls -ld
Each chmod changes exactly the part you name.
chmod u=rwx file-1      # user gets exactly read, write, execute
chmod u+w file-1        # add write for the user
chmod o-r file-1        # remove read from others
chmod o+x file-1        # add execute for others
chmod u=rw,go=r file-1  # several changes in one go

Why would you add x? Because without it, running a script fails:

bash
$ ./file-1
zsh: permission denied: ./file-1
$ chmod u+x file-1
$ ./file-1

The shortcut: octal numbers like 755 and 400

Writing u=rwx,g=rx,o=rx every time gets old. Each block of three permissions is really a 3-bit binary number, and that number fits in a single octal digit:

Table mapping permission strings to binary and octal: --- 000 0 up to rwx 111 7
Each rwx block is 3 bits. r = 4, w = 2, x = 1. Add them up.

So chmod takes three digits: user, group, others.

CommandResultWhen you use it
chmod 700 filerwx------Private script or directory, only you
chmod 755 filerwxr-xr-xScripts and directories everyone may use
chmod 644 filerw-r--r--Normal files
chmod 600 key.pemrw-------SSH private keys (SSH refuses keys others can read)
chmod 400 filer--------Read-only, even for you
chmod 777 filerwxrwxrwxEveryone can do everything. Almost never what you want

You don't need to memorise every combination. 755, 644, 600, 700 and 400 cover nearly everything you will meet.

Fixing "Permission denied" in four steps

  1. Run ls -l file and read the three blocks.
  2. Run id to see your user and groups. Which block applies to you?
  3. Need to run it? You need x. Need to edit it? You need w.
  4. Fix it with chmod, or sudo chown if the file belongs to someone else.

Key takeaways

  • ✓The 10-character string is: file type, then rwx for user, group and others.
  • ✓New files belong to you and your primary group.
  • ✓chown changes who owns a file (usually needs sudo); chmod changes what they can do.
  • ✓Octal is the shortcut: r=4, w=2, x=1. 755, 644, 600 and 400 cover most real cases.

Learn the command line properly

Permissions are one section of CLI Mastery, a 57-lesson course that takes you from "what is a terminal" to writing shell scripts and configuring a web server over SSH on AWS. Every concept is drawn out first, like the diagrams above, then done live in the terminal.

Not sure yet? Watch a lesson first: Absolute Path vs Relative Path is free, no account needed.

Free download

Linux command line cheat sheet

One printable page: navigation, files, permissions, pipes, processes, scripts and SSH.

Occasional emails with new tutorials and courses. Unsubscribe any time.

Keep reading