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:
- r (read): may read its contents. For a folder: may list what's in it.
- w (write): may change its contents. For a folder: may create and delete files in it.
- x (execute): may run it as a program. For a folder: may enter it (
cd).
The system uses only one trio per user, and it picks it in this order:
- If you own the file, the owner trio is used.
- If you aren't the owner but you're in its group, the group trio is used.
- 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...
- With
644, the owner trio isrw-: nox. So./deploy.shis refused, even though Budi owns it. chmod(change mode) changes a file's permissions.chmod 755gives the ownerrwx(7), and the group and othersr-x(5). Now every user may run it, but only the owner may change it.- A shell script also needs
r, because bash has to read it. Withoutr, the script is still refused even withx.
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
- Who:
u(user, meaning the owner),g(group),o(others),a(all of them). - How:
+adds,-removes,=sets exactly. chmod +x deploy.shwith no who-letter givesxto everyone (withumask 022, the usual default setting). If you mean the owner only, writeu+x.
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
slice(i, i + 3)takes the trio at position 0, then 3, then 6.VALUES[char] || 0: the letter-isn't inVALUES, so it givesundefined, and|| 0replaces that with 0.String(octal).split("").map(Number)splits755into7,5,5.NAMESis laid out so its index (the position, starting at 0) equals the digit:NAMES[6]is"rw",NAMES[0]is"none". The letters are written without-.
⚠️ Before you try chmod 777
chmod 777to "fix" an error. With777(rwxrwxrwx), every user on the server may change that file. Usually the owner is what's wrong: fix it withchown, then use755or644.- Thinking
+xis for the owner only.chmod +xgivesxto everyone. For the owner only, usechmod u+x. - chmod in
/mnt/cthrough WSL. Files on a Windows drive opened from WSL (/mnt/c/...) use the permissions from Windows. By default,chmodthere doesn't change the trios. The only change it keeps: if you remove everyw, the file becomes read-only in Windows. The three trios of such a file are also always the same. Keep your projects in your Linux home folder (~), sochmodworks as in this lesson. - Thinking the others trio also applies to the owner.
chmod 077 secret.txtblocks its own owner (cat: secret.txt: Permission denied). For the owner, the owner trio is used, and that trio is---. - The format in the exercise.
parsePermissionreturns the number754, not a string of letters.describePermissionwrites letters without-(rx, notr-x), the digit 0 becomesnone, and the format is exactlyowner:rwx, group:rx, others:rx.
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.