Permission & Ownership — Terminal

ls -l shows the file type, then three permission trios (groups of three letters) for the owner, the group (a set of users) and others (every other user), such…

Permission denied. You'll see this message a lot, and it doesn't mean the file is broken. Every file on Linux and macOS has an owner and permissions: rules for who may read it, change it, and run it. It's like a boarding house. There's the person who rents the room, the neighbours on the same floor, and visitors from outside. Each of them has their own rights: to look inside the room, to change things in it, to use it. On Linux, the room is a file. The person who rents it is the owner, the neighbours are the group (a set of users who share access), and the visitors are others (every other user). The three rights are reading, changing and running the file.

Reading ls -l

ls -l shows the details of a file, including its permissions and its owner:

$ ls -l deploy.sh
-rw-r--r-- 1 budi dev 37 Oct  8 09:15 deploy.sh
Part Example Meaning
type - - a regular file, d a folder, l a symlink
owner rw- the owner may read and write
group r-- members of the dev group may only read
others r-- everyone else may only read
owner name budi the user who owns the file
group name dev the file's group
size 37 37 bytes

A symlink is a pointer to another file. The three groups of three letters after the file type are called trios. Each trio is always in the order r, w, x, and - means that permission isn't given:

The system uses only one trio per user, and it picks it in this order:

  1. If you own the file, the owner trio is used.
  2. If you aren't the owner but you're in its group, the group trio is used.
  3. Any other user gets the others trio.

Numbers: r = 4, w = 2, x = 1

Each trio can be written as one digit: add up the values of the letters that are in that trio.

Trio Sum Digit
rwx 4 + 2 + 1 7
rw- 4 + 2 6
r-x 4 + 1 5
r-- 4 4
--- 0

Three trios make three digits, in the order owner, group, others. The file type letter isn't counted. So -rwxr-xr-- is 754, and -rw-r--r-- is 644.

Now back to deploy.sh, a script that belongs to Budi, with the permissions 644. Typing ./deploy.sh means running the file deploy.sh in this folder. Guess first: when Budi runs it, does it work or is it refused?

$ ./deploy.sh
bash: ./deploy.sh: Permission denied
$ chmod 755 deploy.sh
$ ls -l deploy.sh
-rwxr-xr-x 1 budi dev 37 Oct  8 09:15 deploy.sh
$ ./deploy.sh
Deploy started...

These four permissions are the ones you'll use most:

Number String Usually for
644 rw-r--r-- regular files
755 rwxr-xr-x scripts and folders
600 rw------- secret files: .env, a private key
700 rwx------ private folders, such as ~/.ssh

Symbolic mode: add or remove one permission

Sometimes you only want to change one permission without calculating all the digits again. Write who, how and the permission, for example u+x:

chmod u+x deploy.sh   # the owner gets x: rw-r--r-- becomes rwxr--r--
chmod go-r .env       # group and others lose r
chmod a+r README.md   # everyone (all) may read
chmod u=rw,go= .env   # set exactly: owner rw-, the rest ---, the same as 600

Owner and group: chown

chown (change owner) changes a file's owner and group:

sudo chown sari deploy.sh                  # the owner becomes sari
sudo chown sari:dev deploy.sh              # owner sari, group dev
sudo chown -R www-data:www-data storage/   # -R: the folder and everything in it

Changing a file's owner takes root rights (the system's admin account, not the top folder). That's why the command starts with sudo (run with admin rights). After that, ls -l shows the new owner, and the owner trio applies to them.

From a string to a number in JavaScript

In the exercise, you write two functions. parsePermission turns a 9-letter string (three trios) into a number, and describePermission turns the number into text. One way to write them:

function parsePermission(permStr) {
  const VALUES = { r: 4, w: 2, x: 1 };
  const digits = [];
  for (let i = 0; i < 9; i += 3) {
    const trio = permStr.slice(i, i + 3); // "rwx", then "r-x", then "r--"
    let total = 0;
    for (const char of trio) total += VALUES[char] || 0; // "-" isn't in VALUES, so it counts 0
    digits.push(total);
  }
  return Number(digits.join("")); // [7, 5, 4] becomes "754" becomes 754
}

function describePermission(octal) {
  const NAMES = ["none", "x", "w", "wx", "r", "rx", "rw", "rwx"];
  const [owner, group, others] = String(octal).split("").map(Number);
  return "owner:" + NAMES[owner] + ", group:" + NAMES[group] + ", others:" + NAMES[others];
}

console.log(parsePermission("rwxr-xr--")); // 754
console.log(describePermission(755)); // owner:rwx, group:rx, others:rx
console.log(describePermission(640)); // owner:rw, group:r, others:none

⚠️ Before you try chmod 777

TL;DR: ls -l shows the file type, then three permission trios (groups of three letters) for the owner, the group (a set of users) and others (every other user), such as -rwxr-xr--. r means read, w write, x run (or enter a folder). Each user gets only one trio: the owner trio if they own the file, the group trio if they're in its group, otherwise the others trio. In numbers r = 4, w = 2, x = 1, added up per trio: rwxr-xr-- = 754. 644 is for regular files, 755 for scripts and folders, 600 for secret files, 700 for private folders. chmod changes permissions: chmod u+x adds x for the owner only, chmod +x for everyone. sudo chown user:group file changes the owner. Don't use 777.

⚡ Three permission trios

Meaning: ls -l shows a file's permissions, such as -rw-r--r--. The first letter is the file type (- is a regular file). After it come three trios (groups of three letters) for the owner, the group (a set of users) and others (every other user). Each trio goes r (read), w (write), x (execute), and - means that permission isn't given.

⚡ Numbers: r = 4, w = 2, x = 1

Guess first: each trio can be written as a number, with r = 4, w = 2, x = 1 added up. The script deploy.sh has permission 644, and Budi owns it. If Budi types ./deploy.sh, does the script run?

Refused, Permission denied. The owner digit is 6 = 4 + 2, so the owner trio is rw-: no x.

Meaning: Add up the letters in each trio: rwx = 7, r-x = 5, r-- = 4. So 644 is rw-r--r--, the permission of a regular file.

In the exercise, the function parsePermission turns a permission string into a number: parsePermission("rwxr-xr--") must return the number 754, not a string.

⚡ Allow it to run

Write:

chmod 755 deploy.sh

Meaning: chmod changes a file's permissions. 755 gives the owner rwx, and the group and others r-x. Every user may run deploy.sh, but only the owner may change it. Don't use 777 (every user may change the file).

⚡ For the owner only

Write:

chmod u+x deploy.sh

Meaning: Who-letters: u is the owner, g group, o others, a everyone. + adds a permission, - removes it. So u+x adds x for the owner only. chmod +x with no who-letter gives x to everyone.

What you will learn