Everyone Can Learn Program
中文 Start learning
Core · LinuxLesson 4 of 6Free to read

Permissions and sudo · Linux and the Command Line

Who is allowed to read, change and run each file?

Course contents

Why you need it

Permissions are Linux's first line of defence. A password file readable by everyone, or a script anyone can change and the server then runs, is a classic way in for attackers. "Permission denied" is also the error beginners meet most, and it always has a clear reason.

The intuition

ls -l shows something like -rwxr-x---. After the first character (- file, d folder) come three groups of three:

ownergroupothers
rwxr-x---

r read, w write, x execute (for a folder: enter it).

Each group is also a digit: r = 4, w = 2, x = 1, added up. So rwx r-x --- is 750, and chmod 750 file sets it. chmod u+x file adds execute for the owner only.

sudo runs one command as root, the administrator, who may do anything.

Definition

Permission bits

Read, write and execute for the owner, the group and everyone else, written as letters (rwxr-xr--) or digits (754).

Least privilege

Give each user and program only the access it needs. Secrets are often 600: the owner reads and writes, nobody else can even read.

Examples

Read the permissions

Run it in the lesson

deploy.sh can't run yet; secret.env belongs to root and only root can read it.

ls -l
chmod u+x deploy.sh
ls -l deploy.sh
./deploy.sh
Step by step
  1. L1deploy.sh is rw-r--r-- (644): no x anywhere.
  2. L2u+x: add execute for the owner (you).
  3. L3Now rwxr--r--, which is 744.
  4. L4./ means: run the file in this folder.deploying

Permission denied, and sudo

Run it in the lesson

You are not root, so the 600 file is closed to you. sudo opens it, which is exactly why sudo should be used rarely.

whoami
sudo cat secret.env
cat secret.env

This example stops with an error on purpose: reading the message is the point.

Step by step
  1. L1You are learner.learner
  2. L2As root, the read works.
  3. L3As learner: Permission denied, and the run stops here.

Common mistakes

chmod 777

777 lets anyone read, change and run the file. It "fixes" permission errors by removing all protection. Find out who actually needs access instead.

sudo by habit

A typo with sudo can damage the whole system, and files you create with sudo belong to root. Use it only for the one command that needs it.

Quick quiz

1. What digits mean `rw-r-----`?

  1. A640
  2. B644
  3. C750
Show the answer

Answer: A

Owner rw- = 4 + 2 = 6, group r-- = 4, others --- = 0.

2. What does this print?

touch key.pem
chmod 600 key.pem
ls -l key.pem
  1. A
    -rw------- 1 learner learner     0 Nov  3 09:00 key.pem
  2. B
    -rw-r--r-- 1 learner learner     0 Nov  3 09:00 key.pem
  3. C
    -rwx------ 1 learner learner     0 Nov  3 09:00 key.pem
Show the answer

Answer: A

6 = rw for the owner, 0 and 0 for everyone else.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictDigits to letters

    After these commands, what permissions does ls -l show for a and b?

  2. ModifyLock down the key

    chmod 777 makes the key readable and writable by everyone. Change it so only you can read and write api.key, and nobody can run it.

  3. WriteMake a script runnable

    Make deploy.sh executable for its owner, readable by everyone, and writable only by its owner (that's 744), then run it.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
Permission权限Whether a user may read, write or execute a file.
rootroot 超级用户The administrator account on Linux; it may do anything.
sudosudoRuns one command as root.