T6 Linux and Bash · Lesson 5
Permissions
Who may do what to each file: users, groups and root, reading the ten characters of a permission string, what read, write and run mean for files and for folders, changing them with chmod in letters and in numbers, running your own scripts, and the defaults that new files get.
- say who you are, which group you are in, and who owns a file
- read a permission string such as -rw-r--r--
- say what read, write and execute allow on a file and on a folder
- change permissions with chmod, using letters
- write permissions as numbers such as 644 and 755
- make a script runnable, and explain the defaults that umask gives
The back cover
In a block of flats, each flat has an owner, some rooms are shared by one floor, and the caretaker holds a key to everything. Linux guards its files the same way. When a command answers Permission denied, these rules are the reason.
These pages show users, groups and root, and how to read the ten characters of a permission string. They show what read, write and run mean for files and for folders. They show how to change them with chmod, in letters and in numbers, and how scripts run and new files get their rights.
Before you start
The story: Three locks
Every door in the Underway has three locks: one for its owner, one for its guild, and one for everybody else. The sluice orders are kept behind a door with all three undone, because somebody found the locks a nuisance. This morning a passing carter changed the order, thinking it was a delivery note.
Half of the lower tunnels are now ankle-deep in water.
Warden Rao stands in the water with her lamp held high. "A lock left open is an order to anyone who passes," she says. "Read every door. Then set each one to exactly what it needs, and no more."
A Linux machine is shared. Many people may log in to one server, and many programs run under their own names. So every file says who owns it, and who else may read it, change it or run it. Most of the Permission denied messages you will ever meet come from these rules, and this lesson teaches you to read and set them.
It builds on these ideas. Each one has a tiny example, so you can check that you remember it.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| Who you are. | whoami | Permissions depend on it. |
| The long listing. | ls -l | Its first column is the permissions. |
| The owner column. | asha asha | The owner and the group. |
| The root user. | the # prompt | The one user the rules do not stop. |
| Permission denied. | the end of an error | Now you learn where it comes from. |
| The exit status. | echo $? | 126 and 127 have meanings here. |
| ls -F. | a * after a program | That * marks the execute permission. |
| Folders as lists. | a folder's 4096 bytes | Writing to a folder means changing that list. |
| Patterns. | *.sh | To set many files at once. |
| Running a file. | bash pump.sh | Now with ./pump.sh. |
By the end of this lesson you will be able to:
- say who you are, which group you are in, and who owns a file;
- read a permission string such as -rw-r--r--;
- say what read, write and execute allow on a file and on a folder;
- change permissions with chmod, using letters;
- write permissions as numbers such as 644 and 755;
- make a script runnable, and explain the defaults that umask gives.
How to follow this lesson
Many blocks start at the sluice door from the scene: a file of orders that anyone may change, a pump script that nobody can run, and a records folder. You can lock yourself out of things here as often as you like; each block starts afresh.
In one line
Every file records who owns it and what each kind of user may do with it; this lesson teaches you to read those marks and to set them.
Users, groups and root
At a glance
Think of a block of flats. Each flat has an owner, the families on one floor share a store room, and the caretaker has a key to everything.
Linux works the same way. Every file has an owner, a user, and a group, and the commands whoami and id say who you are. The user root, number 0, may do anything, and only root may give a file to another owner. The command sudo runs one command as root, for users who are allowed.
Take a guess: Does the file /etc/passwd hold anyone's password?
Think of a block of flats. Each flat has an owner. The flats on one floor may share a store room that only that floor's families can open. And the building's caretaker has a key to everything. Linux works the same way, with users, groups and one all-powerful user.
An account on the machine, with a name and a number. Every program runs as some user, and every file is owned by one.
You already know whoami. The command id tells you more: your user's number with -u, and the name of your main group with -gn:
whoami
id -u
id -gn
asha
1000
asha
A named set of users. A file belongs to one group, and that group's members can be given rights that others do not get. On Ubuntu, each new user gets a group of the same name.
A user can be in several groups; the command groups lists them. Every file's long listing shows its owner and its group, in the third and fourth columns:
touch notes.txt
ls -l notes.txt
-rw-rw-r-- 1 asha asha 0 Jan 1 10:00 notes.txt
Here the owner is asha, and the group is asha too, the group of the same name. A new file belongs to the user who made it.
root
One user is special. root has the number 0, and the permission rules do not stop it: root may read, change or remove any file on the machine. That is why a prompt that ends in # is a warning, and why daily work is done as a normal user:
id root
uid=0(root) gid=0(root) groups=0(root)
Only root may give a file to another user, with chown, short for change owner, or move it to another group, with chgrp. A normal user is refused. This session was run on a real machine:
Typed by a normal user on a real Linux machine
asha@tower:~/shop$ chown root notes.txt
chown: changing ownership of 'notes.txt': Operation not permitted
asha@tower:~/shop$ echo $?
1
asha@tower:~/shop$
To do something as root without logging in as root, people use sudo. It runs one command as another user, usually root, if the system's settings allow you to, and it usually asks for your password first. So sudo chown root notes.txt would work for a user who is allowed to use sudo. The small shell has no sudo, as it has no root to become.
| Who | Number | May |
|---|---|---|
| root | 0 | do anything to any file |
| a normal user, such as asha | 1000 and up, on Ubuntu | what the permissions allow |
| a group, such as asha | its own number | what the group's permissions allow |
Worked example 1
Warden Rao asks three things about the sluice door's orders.txt: who owns it, which group it belongs to, and whether you could hand it over to root yourself.
What we have: the file orders.txt, and the commands ls -l and id
What we need: the owner, the group, and a yes or no
The plan: read the third and fourth columns of ls -l; compare the owner with whoami; only root may use chown to give a file away
At the sluice door, with orders.txt
ls -l orders.txt
whoami
-rw-rw-rw- 1 asha asha 20 Jan 1 10:00 orders.txt
asha
- The owner is asha and the group is asha. You are asha, so you own it. But giving it to root needs root's power, and the chown session above shows the refusal.
The owner is asha, the group is asha, and no: only root, or sudo, can give it to root.
Check: whoami prints the same name as the owner column.
A path that fails: thinking that owning a file lets you give it away. On Linux the owner can change its permissions, but only root can change its owner.
When you are stuck: you cannot tell who owns what
ls -l shows the owner in the third column, and the group in the fourth.
whoami says who you are; id -gn says your main group; groups lists all of yours.
Operation not permitted, from chown or chgrp: only root may do that.
A prompt ending in # means you are root: be extra careful.
Your turn
touch a.txt
ls -l a.txt | cut -d' ' -f3
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
asha
- The file was made by asha.
- The third space-separated field of ls -l is the owner.
It prints asha.
Which user's number is 0, and what is special about it?Tap to reveal
- The permission rules do not stop this user.
root, who may do anything to any file.
You try chown root pump.sh as a normal user. What happens?Tap to reveal
- Only root may give a file away.
Operation not permitted, with status 1.
Did you know?
sudo began around 1980
sudo is older than Linux. The sudo project's history says it was first written by Bob Coggeshall and Cliff Spencer around 1980, in the Department of Computer Science at SUNY Buffalo, on a VAX-11/750 running 4.1BSD. An updated version was posted to the Usenet group net.sources in December 1985. Its manual still sums up its job: it lets a permitted user run a command as the superuser or as another user.
Why it fits here: It shows how old this topic's sudo is: one command at a time as root, an idea from about 1980.
Sources: sudo project: A Brief History of Sudo; sudo manual: sudo(8)
A file named passwd with no passwords
The file /etc/passwd lists every user, but on many systems today it holds no passwords. Its manual explains that with the shadow password suite, /etc/passwd has an x in the password field, and the encrypted passwords sit in /etc/shadow, which only the superuser can read. The list itself stays readable by all, because tools such as ls use it to turn user numbers into names.
Why it fits here: It answers this topic's question: the user list is open to all, while the passwords moved to a file only root can read.
Source: Linux man-pages: passwd(5)
In one line
Every file has an owner, a user, and a group. The commands whoami and id say who you are. The user root, number 0, may do anything, and only root may change a file's owner or group. The command sudo runs one command as root for users who are allowed.
Reading a permission string
At a glance
Look again at the first column of a long listing. Its ten characters are like a door with three locks: one for the owner, one for the group, and one for everyone else.
The first character is the kind of thing, a dash for a file and d for a folder. Then come three sets of three: r for read, w for write and x for execute, with a dash for no. Each person gets exactly one set: the owner's if it is theirs, the group's if they are in it, and otherwise the others' set.
Take a guess: Did the first Unix have three sets of permissions, or fewer?
Look again at the first column of a long listing. It has ten characters, and they are a door with three locks: one for the owner, one for the group, and one for everyone else.
At the sluice door: orders.txt, which anyone may change, pump.sh, which nobody can run, and a records folder
ls -l
total 12
-rw-rw-rw- 1 asha asha 20 Jan 1 10:00 orders.txt
-rw-rw-r-- 1 asha asha 13 Jan 1 10:00 pump.sh
drwxrwxr-x 2 asha asha 4096 Jan 1 10:00 records
Take -rw-rw-r--, the line of pump.sh, and split it as the table does. The first character is the kind, which you met in the file system lesson. Then come three groups of three:
| Characters | Here | For whom |
|---|---|---|
| 1 | - | the kind: - a file, d a folder, l a link |
| 2 to 4 | rw- | the owner, asha |
| 5 to 7 | rw- | the group, asha |
| 8 to 10 | r-- | everyone else, called others |
Each group of three always has the same order: r, then w, then x. A letter means yes, and a dash in its place means no.
| Letter | Word | On a file it allows |
|---|---|---|
r | read | reading the contents |
w | write | changing the contents |
x | execute | running it as a program |
So pump.sh may be read and changed by its owner and its group, and only read by others. Nobody may run it: there is no x anywhere. And orders.txt, with rw-rw-rw-, may be changed by everyone, which is how the carter in the scene changed the orders.
Which set applies to you
The three sets do not overlap. The owner gets the owner's set, other members of the file's group get the group's set, and everyone else gets the others' set. So the owner is held to the owner's set even when the group's set allows more. Here the owner has no rights and the group has all of them, and asha, who owns the file and is in its group, is still refused:
printf 'hi\n' > a.txt
chmod 070 a.txt
cat a.txt
cat: a.txt: Permission denied
The chmod line set the rights with a number, which a later section explains: 0 gave the owner nothing, and 7 gave the group everything.
| You are | The set used | For pump.sh |
|---|---|---|
| the owner, asha | characters 2 to 4 | rw- |
| another member of the group | characters 5 to 7 | rw- |
| anyone else | characters 8 to 10 | r-- |
| root | none: root is not stopped | anything |
Worked example 2
Read the records folder's string, drwxrwxr-x, aloud, and say what a visitor who is neither asha nor in her group may do with it.
What we have: the string drwxrwxr-x
What we need: its kind, its three sets, and the visitor's rights
The plan: split it into 1, 3, 3 and 3 characters, and read each set as read, write, execute
- d: a folder.
- rwx: the owner may read, write and execute.
- rwx: the group may do the same.
- r-x: others may read and execute, but not write.
At the sluice door: orders.txt, which anyone may change, pump.sh, which nobody can run, and a records folder
ls -ld records
drwxrwxr-x 2 asha asha 4096 Jan 1 10:00 records
A folder, and the visitor gets r-x: read and execute, no write.
Check: the visitor is neither the owner nor in the group, so the last three characters apply.
A path that fails: reading the string as nine letters for one person. Each person gets one set of three, never the whole nine.
When you are stuck: a permission string will not make sense
Count off the first character, then split the rest into threes.
Read each three as r, w, x in that order; a dash is a no.
Decide which set is yours: owner first, then group, then others.
Use ls -ld on a folder, to see the folder's own string and not its contents.
Your turn
What may others do with a file whose string is -rw-r-----?Tap to reveal
- The last three characters are ---.
- So others get nothing.
Nothing: no read, no write, no execute.
Which characters of -rwxr-xr-- belong to the group, and what do they allow?Tap to reveal
- Characters 5 to 7 are r-x.
r-x: read and execute.
touch a.txt
ls -l a.txt | cut -c1-10
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
-rw-rw-r--
- cut -c1-10 keeps the first ten characters of the line.
- A new file here is rw for owner and group, and r for others.
It prints -rw-rw-r--.
Did you know?
Once there were two sets
Unix permissions once came in two sets, not three. The 1974 paper on Unix says each new file was given seven protection bits. Six of them gave read, write and execute permission separately for the owner of the file and for all other users. The seventh made a program run with the user identity of the file's creator. The group's set of three came later.
Why it fits here: It answers this topic's question: the Unix of 1974 gave rights to the owner and to everyone else, with no group set.
Source: Dennis M. Ritchie and Ken Thompson: The UNIX Time-Sharing System
An eleventh character
A long listing can show an eleventh character after the ten. The GNU ls manual says it tells whether an alternate access method, such as an access control list, applies to the file. A space means none, and a dot means a security context alone. A plus means anything else, such as an access control list, a list of extra rules for named users and groups. The command getfacl shows such a list.
Why it fits here: It completes this topic's ten characters: a plus after them means extra rules, such as an access control list, also apply.
Sources: GNU Coreutils manual: What information is listed; Linux man-pages: acl(5); Linux man-pages: getfacl(1)
In one line
The ten characters are the kind, then three sets of rwx: for the owner, the group, and others. A letter is yes and a dash is no. Each person gets exactly one set: owner first, then group, then others, and root is not stopped at all.
What read, write and run mean
At a glance
The three letters mean slightly different things for files and for folders. For a file they are about its contents; for a folder, about its list of names.
On a file, r reads the contents, w changes them, and x runs the file as a program. On a folder, r lists the names inside, w adds, removes or renames names, and x lets you go in and reach the files inside. So removing a file needs w on its folder, not on the file.
Take a guess: If everyone may write in /tmp, why can you not delete other people's files there?
The three letters mean slightly different things for files and for folders. For a file they are about its contents. For a folder they are about its list of names, because a folder is a list of the names inside it.
| Letter | On a file | On a folder |
|---|---|---|
r | read the contents | list the names inside it |
w | change the contents | add, remove or rename names inside it |
x | run it as a program | go into it, and reach the files inside it |
Files
Without w, a file cannot be changed, not even by its owner, until the owner gives the right back. The error comes from bash, which tried to open the file for the >>:
printf 'pump one\n' > notes.txt
chmod 444 notes.txt
echo extra >> notes.txt
echo $?
cat notes.txt
chmod u+w notes.txt
echo extra >> notes.txt
cat notes.txt
bash: line 3: notes.txt: Permission denied
1
pump one
pump one
extra
Without r, the contents cannot be read at all, though the file's name and its line in ls -l still show:
printf 'secret\n' > orders.txt
chmod 000 orders.txt
cat orders.txt
ls -l orders.txt
cat: orders.txt: Permission denied
---------- 1 asha asha 7 Jan 1 10:00 orders.txt
Folders
A folder's w controls its list of names. Without it, nothing can be added or removed inside, even files that you own and could otherwise change:
mkdir box
touch box/a.txt
chmod -w box
touch box/b.txt
rm box/a.txt
ls box
touch: cannot touch 'box/b.txt': Permission denied
rm: cannot remove 'box/a.txt': Permission denied
a.txt
That surprises many people. Removing a file is a change to the folder's list, not to the file, so it is the folder's w that counts.
A folder's x lets you go into it and reach what it holds. Without x, the names can still be listed with r, but nothing inside can be opened, and cd refuses:
mkdir box
touch box/a.txt
chmod -x box
ls box
cd box
cat box/a.txt
a.txt
bash: line 5: cd: box: Permission denied
cat: box/a.txt: Permission denied
And a folder's r lets you list it. Without r but with x, you can go in, but not see what is there:
mkdir box
chmod -r box
ls box
cd box
pwd
ls: cannot open directory 'box': Permission denied
/home/asha/shop/box
Worked example 3
A helper cannot save a new log in the records folder, though the folder's logs belong to him. Find out why from the listing, and fix it.
What we have: a records folder whose string is dr-xr-xr-x
What we need: the missing right, and the fix
The plan: saving a new file adds a name to the folder's list, which needs the folder's w; give the owner w with chmod
mkdir records
chmod 555 records
ls -ld records
touch records/new.log
chmod u+w records
touch records/new.log
ls records
dr-xr-xr-x 2 asha asha 4096 Jan 1 10:00 records
touch: cannot touch 'records/new.log': Permission denied
new.log
- The folder had r and x for everyone, but no w, so its list could not change. After u+w, the owner may add names.
The folder lacked w; chmod u+w records fixes it.
Check: the second touch worked, and ls shows new.log.
A path that fails: giving the log file w instead. The file does not exist yet, and even if it did, a new name needs the folder's w.
When you are stuck: Permission denied, and you are not sure why
Ask what the command is doing: reading, changing, or running? Then look for r, w or x.
Adding or removing a file needs the folder's w, not the file's.
Going into a folder, or reaching anything inside it, needs the folder's x.
Check every folder on the path with ls -ld: each one needs x.
Your turn
You own a file, but the folder it sits in has no w for you. Can you remove the file?Tap to reveal
- Removing changes the folder's list of names.
- That needs the folder's w.
No: removing needs the folder's w.
What does x mean on a folder?Tap to reveal
- It is not running: folders are not programs.
The right to go into it and reach what is inside.
touch a.txt
chmod 444 a.txt
echo hi > a.txt
echo $?
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
bash: line 3: a.txt: Permission denied
1
- 444 gives everyone read, and nobody write.
- So bash cannot open a.txt for writing, and the status is 1.
It prints the Permission denied error, then 1.
Did you know?
The t at the end of /tmp
/tmp is open for everyone to write in, yet people cannot delete each other's files there. The reason is the restricted deletion flag, or sticky bit. The GNU manual says it stops users from removing or renaming a file in a folder unless they own the file or the folder, and that it is common on folders like /tmp. A long listing shows it as a t in the very last place.
Why it fits here: It answers this topic's question: on /tmp the sticky bit adds a rule, so only a file's owner may remove it.
Sources: GNU Coreutils manual: The Structure of File Mode Bits; GNU Coreutils manual: What information is listed
rm asks, but the folder decides
A file without w can still be removed, because removal needs w on the folder. The GNU rm manual says that when a file is unwritable and you are typing at a terminal, rm asks before removing it, unless -f is given. That question is a courtesy, not a lock: answer no and the file is skipped, answer yes and it goes. The folder's rights decide, not the file's.
Why it fits here: It sharpens this topic's rule: removing needs the folder's w, and rm only asks first when the file itself lacks w.
In one line
On a file, r reads the contents, w changes them and x runs it. On a folder, r lists the names, w adds or removes names, and x lets you go in and reach what is inside. Removing a file needs the folder's w.
Changing permissions: chmod
At a glance
The owner of a file can change its permissions with chmod, short for change mode. The change is written as a short formula.
First comes who: u for the user who owns it, g for the group, o for others, or a for all. Then what to do: + adds, - takes away, and = sets exactly. Then which rights: r, w and x. So u+x reads as: for the owner, add execute. Formulas join with commas, and -R changes a whole folder.
Take a guess: How can one chmod -R open every folder without making every file a program?
The owner of a file can change its permissions with chmod, short for change mode. The mode is written as a short formula: who, then what to do, then which rights.
| Part | Letters | Meaning |
|---|---|---|
| who | u g o a | the user who owns it, the group, others, all three |
| what | + - = | add, take away, set exactly |
| which | r w x | read, write, execute |
So u+x reads as: for the user who owns it, add execute. Watch each change in the listing:
touch a.txt
chmod u+x a.txt
ls -l a.txt
chmod g-w a.txt
ls -l a.txt
chmod o-r a.txt
ls -l a.txt
chmod a+r a.txt
ls -l a.txt
-rwxrw-r-- 1 asha asha 0 Jan 1 10:00 a.txt
-rwxr--r-- 1 asha asha 0 Jan 1 10:00 a.txt
-rwxr----- 1 asha asha 0 Jan 1 10:00 a.txt
-rwxr--r-- 1 asha asha 0 Jan 1 10:00 a.txt
The sign = sets the rights exactly, throwing away what was there. Several formulas can be joined with commas, and o= with nothing after it means no rights at all:
touch a.txt
chmod u=rw,g=r,o= a.txt
ls -l a.txt
-rw-r----- 1 asha asha 0 Jan 1 10:00 a.txt
With no letter before the sign, chmod acts as if a were given, but it leaves alone the rights that the umask setting holds back, which the last section explains. With -R it changes a folder and everything inside it, all the way down.
| You write | It means |
|---|---|
chmod u+x file | the owner may now run it |
chmod go-w file | the group and others may no longer change it |
chmod a+r file | everyone may read it |
chmod o= file | others get nothing |
chmod u=rw,go=r file | owner reads and writes; group and others read |
chmod -R go-rwx folder | nobody but the owner gets into anything below |
Worked example 4
The sluice orders in the scene could be changed by anyone. Set them as the warden asked: the owner may read and write, the group may read, and others get nothing.
What we have: orders.txt, which is -rw-rw-rw-
What we need: -rw-r----- on orders.txt
The plan: use = for each class, so that old rights cannot linger: u=rw, g=r, o= with nothing
At the sluice door: orders.txt, which anyone may change, pump.sh, which nobody can run, and a records folder
ls -l orders.txt
chmod u=rw,g=r,o= orders.txt
ls -l orders.txt
-rw-rw-rw- 1 asha asha 20 Jan 1 10:00 orders.txt
-rw-r----- 1 asha asha 20 Jan 1 10:00 orders.txt
- Each = replaced a whole set. Others lost both their r and their w.
The orders are now -rw-r-----.
Check: the last three characters are dashes, so a passing carter gets nothing.
A path that fails: chmod o-w orders.txt alone. That stops others writing, but leaves them reading the orders, which the warden did not want.
When you are stuck: chmod does not give the result you expected
invalid mode: check the order: who, then + - or =, then r w x.
A right is still there after +: you may have changed the wrong class. u is the owner, o is others.
A bare +x or -w left some class alone: name the class, as in a+x.
Operation not permitted: only the owner, or root, may change a file's mode.
Your turn
touch a.txt
chmod u=rwx,go= a.txt
ls -l a.txt | cut -c1-10
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
-rwx------
- u=rwx gives the owner everything.
- go= gives the group and others nothing.
It prints -rwx------.
Write the chmod that takes write away from the group and others, and changes nothing else.Tap to reveal
- g and o together are go.
- - takes away.
chmod go-w file.
What is the difference between chmod g+r and chmod g=r?Tap to reveal
- + adds to what is there.
- = replaces the group's whole set.
g+r adds read; g=r makes the group's rights exactly read.
Did you know?
A capital X for folders
chmod has a capital X as well as a small x. The GNU manual says X changes execute or search permission only for folders, and for files that already had execute permission. So chmod -R a+X shared lets everyone enter every folder under shared, while plain text files stay as they were. A small x there would make every file executable.
Why it fits here: It answers this topic's question: chmod -R with a capital X opens the folders and leaves ordinary files alone.
Links ignore chmod
chmod never changes a symbolic link, a name that points to another file. The GNU manual says that for each link named on the command line, chmod changes the file it points to instead. The Linux manual for links adds that a link's own permissions are not used at all: they are always 0777 and cannot be changed. That is why every link in a long listing shows lrwxrwxrwx.
Why it fits here: It shows a limit of this topic's chmod: on a link it changes the file pointed to, since a link's own rights are fixed.
Sources: GNU Coreutils manual: chmod invocation; Linux man-pages: symlink(7)
In one line
chmod changes a file's rights with a formula: who (u, g, o or a), then + to add, - to take away or = to set exactly, then r, w and x. Join formulas with commas, and add -R for a whole folder.
Permissions as numbers
At a glance
Modes are often written as three digits, such as chmod 644. They say the same thing as the letters, in a shorter form.
Each right has a value: read is 4, write is 2 and execute is 1. Add the values of the rights you want, and you get one digit for one set. The three digits are for the owner, the group and others, in that order. So 644 suits a normal file, 600 a private one, 755 a program or folder, and 700 a private folder.
Take a guess: What does a fourth digit in front, as in chmod 4755, mean?
You will often see modes written as three digits, such as chmod 644. They say exactly the same thing as the letters, in a shorter form. Each right has a value: read is 4, write is 2 and execute is 1. Add up the values of the rights you want, and you get one digit for one set.
| Rights | Sum | Digit |
|---|---|---|
--- | 0 | 0 |
--x | 1 | 1 |
-w- | 2 | 2 |
r-- | 4 | 4 |
r-x | 4 + 1 | 5 |
rw- | 4 + 2 | 6 |
rwx | 4 + 2 + 1 | 7 |
Each digit stands for one set, in the same order as the string: owner, then group, then others. So 644 means the owner gets 6, rw-, and the group and others get 4, r--. Every sum from 0 to 7 can be made in only one way, which is why the digits are never ambiguous.
touch a.txt
chmod 600 a.txt
ls -l a.txt
chmod 644 a.txt
ls -l a.txt
chmod 755 a.txt
ls -l a.txt
chmod 700 a.txt
ls -l a.txt
-rw------- 1 asha asha 0 Jan 1 10:00 a.txt
-rw-r--r-- 1 asha asha 0 Jan 1 10:00 a.txt
-rwxr-xr-x 1 asha asha 0 Jan 1 10:00 a.txt
-rwx------ 1 asha asha 0 Jan 1 10:00 a.txt
A handful of modes cover most needs, and are worth knowing by sight:
| Mode | String | Used for |
|---|---|---|
644 | rw-r--r-- | ordinary files that others may read, such as a web page |
600 | rw------- | private files, such as a key or a password file |
755 | rwxr-xr-x | programs and scripts that anyone may run, and folders others may enter |
700 | rwx------ | a private folder, or a script only you may run |
640 | rw-r----- | a file the group may read, and others not at all |
777 | rwxrwxrwx | everything for everyone: almost never right |
To turn a string into digits, take each set of three and add its values. For rwxr-x---: rwx is 4 + 2 + 1 = 7, r-x is 4 + 1 = 5, and --- is 0, so 750.
777 is not a fix
When something says Permission denied, it is tempting to run chmod 777 and move on. That lets anyone on the machine change or replace the file, as anyone could change the sluice orders. Find the one right that is missing, and give only that.
Worked example 5
Set both of the warden's private files, orders.txt and notes.txt, to the mode she wants: the owner reads and writes, the group reads, and others get nothing. Use one command with digits.
What we have: two files, and the rights rw-, r-- and ---
What we need: one chmod with three digits
The plan: add up each set: rw- is 4 + 2 = 6, r-- is 4, --- is 0; so the mode is 640, and chmod takes several file names
At the sluice door, with notes.txt as well
chmod 640 orders.txt notes.txt
ls -l orders.txt notes.txt
-rw-r----- 1 asha asha 0 Jan 1 10:00 notes.txt
-rw-r----- 1 asha asha 20 Jan 1 10:00 orders.txt
- One command set both files. ls lists the two in name order, so notes.txt comes first.
chmod 640 orders.txt notes.txt gives both -rw-r-----.
Check: each listing ends in three dashes, and the group has only r.
A path that fails: chmod 460, with the digits in the wrong order. The owner would get only read, and the group read and write.
When you are stuck: a numeric mode gives an unexpected string
Check the order: owner, then group, then others.
Check each sum: read 4, write 2, execute 1.
A digit above 7, or more than four digits, gives invalid mode.
Going from a string to digits: split it into threes and add.
Your turn
What string does chmod 750 give?Tap to reveal
- 7 is 4 + 2 + 1, rwx.
- 5 is 4 + 1, r-x.
- 0 is ---.
rwxr-x---.
Which mode is rw-rw-r--?Tap to reveal
- rw- is 6, rw- is 6, r-- is 4.
664.
touch a.txt
chmod 400 a.txt
ls -l a.txt | cut -c1-10
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
-r--------
- 4 for the owner is read only.
- 0 and 0 give nothing to the group and others.
It prints -r--------.
Did you know?
A fourth digit in front
A mode can have a fourth digit, written in front. The GNU manual explains that three special bits share one more octal digit: 4000 is set user ID, 2000 is set group ID and 1000 is the sticky bit. Set user ID makes a program run with the user ID of its file's owner, whoever starts it. So chmod 4755 does that, and ls shows an s where the owner's x would be.
Why it fits here: It answers this topic's question: a digit in front of the three holds the special bits, such as 4 for set user ID.
Sources: GNU Coreutils manual: Numeric Modes; GNU Coreutils manual: The Structure of File Mode Bits; GNU Coreutils manual: What information is listed
Octal in program code
Octal also hides in program code. In C, a number that starts with a zero, such as 0644, is octal. Python followed that rule before version 3.0, but its manual now forbids leading zeros in a decimal number: 0123 is not valid, to avoid mixing it up with C's octal. Python writes octal with the prefix 0o instead, so a mode in Python reads os.chmod(path, 0o644).
Why it fits here: It carries this topic's three digits into code: Python writes the mode 644 as 0o644, so it is read as octal.
Sources: cppreference: Integer constant (C); Python Language Reference: Lexical analysis; Python Library Reference: os
In one line
In a numeric mode, read is 4, write 2 and execute 1; each set's rights add up to one digit, for owner, group and others in that order. 644 is a normal file, 600 a private one, 755 a program or folder, 700 a private one.
Running scripts, and the defaults
At a glance
A script is a file of commands, such as pump.sh, which holds one echo. To run it like a program, you name it with its path and it needs the x right.
Typed as ./pump.sh it runs; without x, bash refuses with status 126, and without the ./ it is not found, status 127. Running bash pump.sh needs only r, since bash reads the file. New files get their rights from the umask, which takes rights away: with 002, a new file is 664 and a new folder 775.
Take a guess: Why might your umask be 002 when the usual default is 022?
A script is a file of commands, such as pump.sh at the sluice door, which holds one echo. To run it like a program, you name it with its path, and it needs the x right. Without x, bash refuses, with a status of 126:
At the sluice door: orders.txt, which anyone may change, pump.sh, which nobody can run, and a records folder
./pump.sh
echo $?
chmod +x pump.sh
./pump.sh
echo $?
bash: line 1: ./pump.sh: Permission denied
126
pumping
0
The ./ at the front matters. A plain name, such as pump.sh, is looked for only in the folders where programs are kept, and not in the current folder. So even a runnable script needs its path:
At the sluice door: orders.txt, which anyone may change, pump.sh, which nobody can run, and a records folder
chmod +x pump.sh
pump.sh
echo $?
bash: line 2: pump.sh: command not found
127
The environment lesson of this track explains that list of folders, called PATH. Remember the two numbers: 127 means not found, and 126 means found but not allowed to run.
You can also hand a script to bash to read. Then bash only needs to read the file, so no x is needed. The lesson on scripts uses both ways:
At the sluice door: orders.txt, which anyone may change, pump.sh, which nobody can run, and a records folder
bash pump.sh
pumping
ls -F, from the file system lesson, marks every runnable file with a *:
At the sluice door: orders.txt, which anyone may change, pump.sh, which nobody can run, and a records folder
chmod +x pump.sh
ls -F
orders.txt pump.sh* records/
The defaults: umask
Where do a new file's rights come from? A program asks for rw for everyone on a new file, and rwx for everyone on a new folder, and then the umask takes some rights away. The command umask prints the current setting:
umask
touch new.txt
mkdir new
ls -ld new.txt new
0002
drwxrwxr-x 2 asha asha 4096 Jan 1 10:00 new
-rw-rw-r-- 1 asha asha 0 Jan 1 10:00 new.txt
A setting of your shell that lists the rights to take away from every new file and folder. It is written like a mode: 002 takes w from others, 022 takes w from the group and others, and 077 takes every right from them.
The rights named in the umask are taken away from 666 for files and 777 for folders. With 0002, only the others' w goes, so files get 664, rw-rw-r--, and folders 775, rwxrwxr-x, which matches the listing. 0002 is also the umask of a normal user on the Ubuntu laptop this lesson was written on. A stricter umask makes new things private:
umask 077
touch private.txt
mkdir private
ls -l
total 4
drwx------ 2 asha asha 4096 Jan 1 10:00 private
-rw------- 1 asha asha 0 Jan 1 10:00 private.txt
A umask set at the prompt lasts until that shell ends. The environment lesson shows where to put it, so that every new shell starts with it.
Worked example 6
Corin's new pump script, check.sh, will not run, and he wants a script that he can run, his group can run, and others cannot touch. Fix it with one numeric chmod, run it, and check the mark in ls -F.
What we have: check.sh, a script with no x for anyone
What we need: rwxr-x---, and the script running
The plan: owner rwx is 7, group r-x is 5, others nothing is 0: chmod 750; then run it by its path
In a folder with check.sh, which holds one echo
./check.sh
chmod 750 check.sh
./check.sh
ls -F
bash: line 1: ./check.sh: Permission denied
all pumps checked
check.sh*
- The first try was refused, with 126. After chmod 750, the owner's x lets it run, and ls -F marks it with a *.
chmod 750 check.sh, and the script prints all pumps checked.
Check: the * after check.sh in ls -F shows that it can be run.
A path that fails: chmod 777 check.sh. It runs, but anyone on the machine could then change what it does, and the next person to run it would run their changes.
When you are stuck: a script will not run
Permission denied, status 126: add x, as in chmod u+x script.sh.
command not found, status 127: run it by its path, ./script.sh.
Still nothing? Try bash script.sh, which needs only r.
New files keep coming out with the wrong rights: check umask.
Your turn
printf 'echo hi\n' > hi.sh
bash hi.sh
./hi.sh
echo $?
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
hi
bash: line 3: ./hi.sh: Permission denied
126
- bash reads the file, so r is enough: hi.
- ./hi.sh needs x, and there is none: refused, 126.
It prints hi, then the Permission denied error, then 126.
What do the statuses 126 and 127 mean?Tap to reveal
- One is found but refused; the other is not found.
126: found, but not allowed to run. 127: not found.
With umask 022, what rights does a new file get?Tap to reveal
- Start from 666 for a file.
- Take away 022: w from the group and others.
644, rw-r--r--.
Did you know?
Why a mask can be 002
A mask of 002 depends on a private group. The login.defs manual says the mask starts at 022 when nothing else is set, and that useradd can give each new user a group with the user's own name. The pam_umask manual adds an option for such users: when the user name matches the main group's name, the group bits copy the owner bits, so 022 becomes 002.
Why it fits here: It answers this topic's question: when your group is yours alone, the mask can give the group the same rights as you, turning 022 into 002.
Sources: Linux man-pages: login.defs(5); Linux man-pages: pam_umask(8)
When x is not enough
The x right is not always the last word. A whole file system can be mounted with the option noexec, which the mount manual says does not permit direct execution of any binaries on it. The execve manual lists the error for that case: execution is denied because the file system is mounted noexec. A script with x on such a disk is refused, while bash script.sh still works, as bash only reads the file.
Why it fits here: It adds a limit to this topic's x right: on a disk mounted noexec, ./script.sh is refused even with x.
Sources: Linux man-pages: mount(8); Linux man-pages: execve(2)
In one line
A script runs by its path, as ./pump.sh, and needs x: without it, status 126; without the ./, status 127. bash script.sh needs only r. The umask takes rights away from new files and folders: 002 gives 664 and 775.
Where it is used
Permissions matter most on servers, where many people and programs share one machine. Here is where each idea of this lesson does its work.
| What you learned | Where it is used | A real example |
|---|---|---|
| users and groups | programs that run under their own names | a web server running as its own user |
| reading ls -l | finding why something is refused | a page the web server cannot read |
chmod 600 | secrets | a file holding a key or a password |
chmod 644 | files others may read | the pages of a website |
chmod 755, +x | scripts and programs | a script that sets up a project |
| a folder's x | reaching files inside | a web server reaching a site's folder |
| umask | the rights of every new file | a team folder where new files must be shared |
sudo | the few jobs that need root | installing a program |
Mistakes and debugging
These are the slips that people meet most often with permissions. Every output below is the real one, from the code shown.
1. Running a script without x
At the sluice door: orders.txt, which anyone may change, pump.sh, which nobody can run, and a records folder
./pump.sh
bash: line 1: ./pump.sh: Permission denied
The script has no x. The fix: chmod u+x pump.sh, or run it with bash pump.sh.
2. Running a script without ./
At the sluice door: orders.txt, which anyone may change, pump.sh, which nobody can run, and a records folder
chmod u+x pump.sh
pump.sh
bash: line 2: pump.sh: command not found
A plain name is not looked for in the current folder. The fix: ./pump.sh.
3. Digits in the wrong order
touch a.txt
chmod 466 a.txt
ls -l a.txt
echo more >> a.txt
-r--rw-rw- 1 asha asha 0 Jan 1 10:00 a.txt
bash: line 4: a.txt: Permission denied
The digits go owner, group, others, so 466 left the owner with read only. The fix: chmod 644 a.txt.
4. A numeric mode given to a whole tree
At the sluice door: orders.txt, which anyone may change, pump.sh, which nobody can run, and a records folder
chmod -R 644 records
ls records
cd records
chmod: cannot access 'records/2026.log': Permission denied
2026.log
bash: line 3: cd: records: Permission denied
644 has no x, so chmod took the folder's x away first, and then could not reach the file inside. Now nobody can go in. The fix: chmod 775 records. For a whole tree, use letters that leave x alone, such as chmod -R go-w records.
5. Letters in the wrong order
At the sluice door: orders.txt, which anyone may change, pump.sh, which nobody can run, and a records folder
chmod w-o orders.txt
chmod: invalid mode: ‘w-o’
Try 'chmod --help' for more information.
A mode reads who, then the sign, then the rights. The fix: chmod o-w orders.txt.
6. 777 as a cure
At the sluice door: orders.txt, which anyone may change, pump.sh, which nobody can run, and a records folder
chmod 777 orders.txt
ls -l orders.txt
-rwxrwxrwx 1 asha asha 20 Jan 1 10:00 orders.txt
No error, which is the danger: everyone may now change the orders, and even run them. The fix: find the one right that is missing, and give only that, as in chmod 640 orders.txt.
The debugging corner: ls -l, then the question
When something says Permission denied, run ls -l on the file and ls -ld on its folder. Then ask three things. Who am I to this file: its owner, in its group, or other? What am I doing: reading, writing, or running? Which letter does that need, in which set? The answer names the missing letter.
Quick card
The whole lesson on one card. Come back to it whenever you need a reminder.
The lesson in code
whoami # who am I?
ls -l orders.txt # owner, group, and the ten characters
chmod u=rw,g=r,o= orders.txt # letters: set each class exactly
ls -l orders.txt
chmod 755 pump.sh # numbers: rwx r-x r-x
./pump.sh # run it by its path
umask # the rights new files lose
asha
-rw-rw-rw- 1 asha asha 20 Jan 1 10:00 orders.txt
-rw-r----- 1 asha asha 20 Jan 1 10:00 orders.txt
pumping
0002
| When you see | It means | Look at |
|---|---|---|
Permission denied reading a file | no r in your set | ls -l, then chmod u+r |
Permission denied writing a file | no w on the file | chmod u+w |
cannot touch or cannot remove, denied | no w on the folder | ls -ld on the folder |
cd: ...: Permission denied | no x on the folder | chmod u+x folder |
./script: Permission denied, status 126 | no x on the script | chmod u+x script |
command not found, status 127, for your script | no ./ in front | ./script |
Operation not permitted | not yours to change | the owner, or root with sudo |
invalid mode | the mode is written wrongly | who, then + - =, then r w x |
The story: Exactly what it needs
You go down the tunnels reading each door's marks: who owns it, which guild shares it, and what each may read, write or run. The sluice orders get their owner's write and the guild's read, and nothing for anyone else. The pump script, which nobody could run, gets the one mark it was missing.
The water drains by nightfall, and the next carter who tries the door is politely refused.
Corin asks whether it would be easier to give everyone everything. Warden Rao looks down at his wet boots until he stops asking.
Reagents gathered: permission, owner, group, chmod, execute bit, umask.
Sources
- GNU Bash Reference Manual (GNU Free Documentation License 1.3 or later (the manual's copying notice, read 2026-11-06); facts and ideas only)
- GNU Coreutils manual (GNU Free Documentation License 1.3 or later (the manual's copying notice, read 2026-11-06); facts and ideas only)