Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Who may use a file (permissions)

After this lesson: I can set who may use a file.

45 min lesson8 commandsLinux administration lab: users, permissions, cron and systemd in your browser

The staff rota anyone could change

A shop keeps its staff rota in one file on the shared computer behind the till.

One Saturday a new starter swapped their shifts with somebody else's. Nobody could say who did it, or stop it happening again.

The file let every account change it. One small setting would have fixed that.

By the end of this lesson

Note

I can set who may use a file.

Key words

WordWhat it meansExample
Useran account on a machine, with its own name and files
Groupa named list of users who share some files
Ownerthe user a file belongs to
Permissionthe right to read, change or run a file
Modethe letters that show the permissions of a filerw-r--r--
sudorun one command as the administrator, root

The cells students fill in are left blank in this preview.

Before you start

Note

Press Start the lab on the bar at the top and wait for the machine. You are the administrator of this server, so sudo works for you. Press the Runs in order.

Every file has an owner and a group

Every file belongs to one user, its owner, and to one group.

Its mode gives three sets of rights. The first set is for the owner. The second is for members of the group. The third is for everybody else.

Each set is three letters: r to read, w to write and x to run. A dash means no.

Who am I, and which groups?

Command
id

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Look at the passwords file

Command
ls -l /etc/shadow

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Try to read it

Command
cat /etc/shadow

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: reading -rw-r-----

PartLettersWhoMay
First character-The typeA dash is a file, a d is a folder
Ownerrw-rootRead and write
Groupr--Members of shadowRead only
Everybody else---student, and all othersNothing at all

The cells students fill in are left blank in this preview.

chmod sets the mode with three digits

chmod changes the mode. Each set of three letters becomes one digit. Add 4 for r, 2 for w and 1 for x.

So 600 means the owner may read and write, and nobody else may do anything.

To test it, the Runs use sudo -u nobody. It runs one command as nobody, a built-in account that stands for any other user.

Worked example: modes as digits

DigitsOwnerGroupEverybody elseGood for
600rw- (4+2)--- (0)--- (0)A private file
640rw- (4+2)r-- (4)--- (0)A file the team may read
644rw- (4+2)r-- (4)r-- (4)A file anybody may read
755rwx (4+2+1)r-x (4+1)r-x (4+1)A program anybody may run

The cells students fill in are left blank in this preview.

Write a rota into the shared folder

Command
echo "Sat 9-5 Sam" > /tmp/rota.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Make it private

Command
chmod 600 /tmp/rota.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Can another user read it?

Command
sudo -u nobody cat /tmp/rota.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Let everybody read it

Command
chmod 644 /tmp/rota.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Try again as the other user

Command
sudo -u nobody cat /tmp/rota.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people fix "Permission denied" with chmod 777. It works, because it lets every user read, change and run the file. That is exactly what went wrong with the rota. Give the fewest rights that do the job, such as 640.

A team shares through a group

To share a folder with a team, an administrator makes a group with sudo groupadd. They add each member with sudo usermod -aG, then give the folder to the group with sudo chgrp.

The group's digit then decides what the team may do. Everybody else gets the last digit, often 0. The lab asks you to do this for a real team.

Going further

Make a file /tmp/menu.txt. You may read and write it, your group may read it, and everybody else may do nothing.

Work out the three digits on paper first. Then set them with chmod. Check with stat -c "%a %A" /tmp/menu.txt, which prints the digits and the letters.

About this lab and its source: Linux administration lab: users, permissions, cron and systemd in your browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.