ubuntu, updated

How permissions work

Read rwx and octal permissions, change them with chmod and chown, and fix the classic Permission denied error.

Reading ls -l

-rw-r----- 1 anna developers 4096 Oct 21 08:00 report.txt
│└┬┘└┬┘└┬┘   └─┬┘ └───┬────┘
│ │  │  │      │      └ group
│ │  │  │      └ owner (user)
│ │  │  └ others: ---  nothing
│ │  └ group:  r--  read
│ └ owner:  rw-  read + write
└ type: - file, d directory, l link

Every file has one owner, one group and three permission sets: for the owner (u), the group (g) and everyone else (o). Linux checks them in that order and uses the first one that matches.

What r, w, x mean

On a fileOn a directory
r (4)Read the contentList the names inside (ls)
w (2)Change the contentCreate, delete, rename files inside
x (1)Run it as a programEnter it (cd) and access files inside

Note: deleting a file needs w on the directory, not on the file.

Octal numbers

Add up r=4, w=2, x=1 for each of user, group, others:

NumberMeansTypical for
644rw-r—r—Normal files
640rw-r-----Config files with secrets, group may read
600rw-------Private keys, passwords
755rwxr-xr-xDirectories, scripts, programs
750rwxr-x---Directories only the group may enter
700rwx------Private directories (~/.ssh)
777rwxrwxrwxNever. It is not a fix, it’s a hole.

chmod and chown cheat sheet

WhatCommand
Set exact permissionschmod 640 file
Add execute for the ownerchmod u+x script.sh
Remove all rights for otherschmod o-rwx file
Give the group the same as the ownerchmod g=u file
Change ownersudo chown anna file
Change owner and groupsudo chown anna:developers file
Change only the groupsudo chgrp developers file
Recursivelyadd -R, e.g. sudo chown -R anna:developers /srv/app
Dirs 755, files 644, recursivelysudo chmod -R u=rwX,go=rX /srv/app

The capital X sets execute only on directories (and files that are already executable). That’s the clean way to fix a whole tree.

“Permission denied”: checklist

  1. Who am I? id
  2. Who owns it and what are the rights? ls -l file
  3. Check every folder on the path, each needs x: namei -l /srv/app/conf/app.yml
  4. Just added to a group? Log out and in again.
  5. Still denied with fitting rights? Check ACLs (getfacl file, see Special permissions and ACLs) and AppArmor (sudo dmesg | grep -i apparmor).
§ home
~/knubertus/home/
1,1 Top