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

Free lesson

File permissions: who may read, write or run

After this lesson: I can say who may read, write or run a file, and change it.

55 min lesson15 commandsLinux administration lab: users, permissions, cron and systemd in your browser

The payroll file everyone could open

A small company keeps its payroll file on a shared server. A new starter opens the folder and finds it. Anyone with a login can read every salary.

Nobody hacked anything. The file simply had the wrong permissions. This lesson is about getting them right.

By the end of this lesson

Note

I can say who may read, write or run a file, and change it.

Key words

WordWhat it meansExample
Ownerthe one user a file belongs to
Groupa named list of users who share files
Otherseverybody who is not the owner or in the group
Rootthe administrator account, which may do anything
sudoruns one command as rootsudo whoami
Setgida folder setting: new files join the folder's group2750

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

Three kinds of person, three rights

Every file has an owner and a group. Everybody else is others.

For each of the three, the file says who may read (r), write (w) and run (x) it.

On a folder, r means list it, w means add or remove files, and x means go into it.

Who am I?

Command
id

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

Read a folder's mode, owner and group

Command
stat -c "%a %A %U %G" /srv/projects

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

Worked example: reading a mode

ModeOwner mayGroup mayOthers may
rwxr-x---read, write, runread, runnothing
rw-rw-r--read, writeread, writeread
rw-------read, writenothingnothing
rwxr-xr-xread, write, runread, runread, run

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

chmod changes a mode

chmod changes the mode. You can give it three digits, such as chmod 640 payroll.txt.

Or you can say what to add or take away. g+w adds write for the group. o-r takes read away from others.

Run these in order. Each ls -l shows the new mode.

Make an empty file

Command
touch payroll.txt

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

Owner read and write, group read

Command
chmod 640 payroll.txt

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

Look at the mode

Command
ls -l payroll.txt

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

Let the group write too

Command
chmod g+w payroll.txt

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

Look again

Command
ls -l payroll.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 think chmod 777 is a quick fix when something says permission denied. It lets every user on the machine change the file. Find out who needs the right, and give it to that owner or group only.

Users, root and sudo

Every person and every service has a user account. An ordinary user can open another user's file only when its mode allows it, and cannot change the system's files.

Root can do anything. Nobody should work as root all day. sudo runs just one command as root, and then you are an ordinary user again.

An ordinary user is refused

Command
ls /root

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

The same user, with sudo

Command
sudo whoami

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

Give the file to root

Command
sudo chown root payroll.txt

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

Look at the owner

Command
ls -l payroll.txt

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

Give it back

Command
sudo chown student payroll.txt

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

A shared folder needs a group and setgid

A team shares a folder. Make a group, for example sales, and put each member in it with usermod -aG sales name.

Set the folder's group with chgrp. Then choose what the group may do.

Add the setgid right, the 2 in 2750. Now a file made in the folder joins the folder's group, whoever made it.

Make a practice folder

Command
mkdir -p teamdemo

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

Setgid, group may read and run, others nothing

Command
chmod 2750 teamdemo

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

Look for the s

Command
ls -ld teamdemo

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

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.