Linux permissions

Every file has an owner and a rule: who can read, who can change, and who can execute.

Comandu penguin with a shield and locked folders

Three questions

A Linux permission fits in three verbs. Read: you can see the contents. Change: you can edit or delete. Execute: you can run it, in the case of a program or a script.

A file can let you read and still block execution. They are separate rules. Having one does not give you the others.

Owner, group, and everyone else

The same trio repeats three times. The first applies to the owner of the file. The second applies to the owner's group. The third applies to anyone else on the machine.

That is why a coworker can only look at the file, and you can edit it. It is not one rule. It is three, side by side.

Solve

You can edit the report. A coworker opens the same file and can only read. What changed?

The notice that shows up

When the rule blocks the command, the terminal does not stay quiet. It writes Permission denied. The folder exists. The file exists. The permission for that action is missing.

bash - comandu
devlinux@lab-server:~$ ./scripts/send_metrics.sh
bash: ./scripts/send_metrics.sh: Permission denied

Solve

The script is in the folder and the terminal said Permission denied when you tried to run it. What is missing?

A wrong path is a different notice

Permission denied means the path was found and the rule blocked it. If the name is wrong, the terminal writes No such file or directory. One is permission. The other is the path.

bash - comandu
devlinux@lab-server:~$ ./scripts/send_metric.sh
bash: ./scripts/send_metric.sh: No such file or directory

The real name was send_metrics.sh. The terminal never looked at the permission, because it did not find the file.

Figure it out

The command answered No such file or directory. What is the first fix?

Read the rule before you change it

ls -l shows, next to the name, letters like r, w, and x. r is read, w is change, x is execute. A dash in place of a letter means that action is off.

bash - comandu
aluno@desafio:~$ ls -l
-rw-r--r-- 1 aluno aluno  120 Oct 20 notes.txt
drwxr-xr-x 2 aluno aluno 4096 Oct 20 reports

The first letter says the type. - is a file. d is a folder. Then come nine letters, in three groups: owner, group, and everyone else.

On notes.txt, the owner has rw-: they can read and change, not execute. The group and everyone else have r--: read only.

On the reports folder, the owner has rwx. On a folder, x does not run a program. It lets you enter with cd and reach what is inside. Without that x, the folder can show up in ls and still refuse entry.

Figure it out

In the listing, the script has r and w, and it does not have x. Can you open its text?

The x the script was missing

The script from the start was found. The x was missing. Reading the text still works, if r is there. Running it is another permission.

First you read ls -l. Only then do you change the rule. chmod u+x, followed by the name, turns execution on for the owner. u is the owner. +x adds execute and leaves the rest as it was.

bash - comandu
devlinux@lab-server:~$ ls -l scripts/send_metrics.sh
-rw-r--r-- 1 devlinux devlinux 80 Oct 20 scripts/send_metrics.sh
devlinux@lab-server:~$ chmod u+x scripts/send_metrics.sh
devlinux@lab-server:~$ ls -l scripts/send_metrics.sh
-rwxr--r-- 1 devlinux devlinux 80 Oct 20 scripts/send_metrics.sh

The owner went from rw- to rwx. The group and everyone else still have read only. Then ./scripts/send_metrics.sh can run.

To find your way in the folder before you look at the rule, see how to navigate folders.

Categories

Permissions