To change who can read, write or run a file, Linux provides three commands:
chmod (permissions), chown (owner) and chgrp (group). This guide shows
how to use them in practice, with both notations of chmod, symbolic and
octal.
What you will learn
- Use
chmodin symbolic notation (u+x,g-w,o=r) - Use
chmodin octal notation (644,755,700) - Change the owner with
chown - Change the group with
chgrp - Apply recursive changes to a directory
How do you change permissions with chmod?
chmod accepts two notations. The symbolic one changes a single
right without touching the others; the octal one sets all nine at once.
Both are worth knowing, and exams use both.
Symbolic notation
Symbolic notation says who you change, which action and which permission:
| Who | Meaning |
|---|---|
u | User (owner) |
g | Group |
o | Others |
a | All (everyone) |
| Action | Meaning |
|---|---|
+ | Add a permission |
- | Remove a permission |
= | Set exactly these permissions |
Examples:
# Make a script executable by its ownerchmod u+x script.sh
# Remove write from the group and the otherschmod go-w report.txt
# Give read-only access to everyonechmod a=r document.txtCheck the result after each change:
ls -l script.shOctal notation
Each permission has a numeric value:
| Permission | Value |
|---|---|
r (read) | 4 |
w (write) | 2 |
x (execute) | 1 |
You add the values for each level (user, group, others):
| Permissions | Calculation | Octal |
|---|---|---|
rwx | 4+2+1 | 7 |
rw- | 4+2+0 | 6 |
r-x | 4+0+1 | 5 |
r-- | 4+0+0 | 4 |
--- | 0+0+0 | 0 |
For chmod, you write three digits: user, group, others.
# rw-r--r-- (read/write for the owner, read-only for the others)chmod 644 report.txt
# rwxr-xr-x (executable by everyone, write for the owner only)chmod 755 script.sh
# rwx------ (full access for the owner, nothing for the others)chmod 700 private-dir/The most common combinations
In practice, four octal values cover almost every case. Rather than recomputing the bits each time, learn these four lines: they read as "who can do what", from the most open (644, readable by everyone) to the most closed (600, reserved for the owner). The rule that emerges is simple: the execute right only goes to scripts and directories, never to a data file.
| Octal | Symbolic | Typical use |
|---|---|---|
644 | rw-r--r-- | Configuration files, documents |
755 | rwxr-xr-x | Scripts, shared directories |
700 | rwx------ | Sensitive personal files or directories |
600 | rw------- | SSH keys, secret files |
How do you change the owner with chown?
# Change the owner of a filesudo chown alice file.txt
# Change owner AND group in a single commandsudo chown alice:devs file.txtHow do you change the group with chgrp?
# Change the group of a filesudo chgrp devs project.txtchgrp is useful when you want to change only the group without touching
the owner. You can also use chown :devs file.txt (note
the : in front of the group).
How do you apply changes recursively?
To apply a change to a directory and all its contents:
# Recursive permissionschmod -R 755 project/
# Recursive ownersudo chown -R alice:devs project/Practical examples
Two situations come back constantly in administration: a script that refuses to run, and a directory several people must write to. Each one shows the right order of the moves: observe, change, check.
Make a script executable and run it
This is the use case that surprises people most often: a freshly created file is not executable, even if it starts with a #!/bin/bash. The shebang says which interpreter to use, it grants no right. The four steps below have you observe the starting state, set the x bit, then run the script.
-
Create a script:
Terminal window echo '#!/bin/bash' > my-script.shecho 'echo "Hello!"' >> my-script.sh -
Check the current permissions:
Terminal window ls -l my-script.shOutput -rw-r--r-- 1 bob bob 26 Apr 6 10:00 my-script.sh -
Add the execute right:
Terminal window chmod u+x my-script.sh -
Run the script:
Terminal window ./my-script.sh
Share a directory with a group
A second common case: several people must write to the same directory. The answer is never chmod 777, which opens it to the whole system, but going through a common group. First change the owning group of the directory, then grant it write access. Only the members of devs gain the right, the others stay read-only.
-
Change the group of the directory:
Terminal window sudo chgrp devs project/ -
Give write access to the group:
Terminal window chmod g+w project/ -
Check:
Terminal window ls -ld project/Output drwxrwxr-x 2 bob devs 4096 Apr 6 10:00 project/
Troubleshooting
Most permission blocks come down to two causes: you are not the owner of the file, or the problem does not come from the target file but from its parent directory. Go through the Symptom column to find the exact message in front of you. The last line deserves particular attention: without the x right on a directory, its contents stay unreachable whatever the permissions of the files inside.
| Symptom | Likely cause | Fix |
|---|---|---|
chmod: changing permissions of 'file': Operation not permitted | You are not the owner of the file | Use sudo chmod or ask the owner |
chown: changing ownership of 'file': Operation not permitted | chown requires root | Add sudo in front of the command |
| A script stays non-executable | chmod applied to the wrong file | Check the exact file with ls -l |
| Every file in a directory is executable | chmod -R 755 applied instead of separating files and directories | Use find with -type f and -type d |
Permission denied despite chmod 777 | The parent directory lacks x | Check the parent directory's permissions with ls -ld |
Put it into practice
Between the theory of the rwx bits and the right chmod at the first attempt, there is a step that only practice gets you over. This lab gives you four concrete cases: a private secret, an executable script, a note readable by the group and a locked home directory, each of which must carry exact permissions in octal or symbolic notation.
Key points
chmodchanges permissions: symbolic notation (u+x) or octal (644).chownchanges the owner (requiressudo).chgrpchanges the group.- The common combinations:
644(files),755(scripts, directories),700(private). - The
-Roption applies changes recursively: use it with care.
Knowledge check
Check that the essentials of this guide stuck. The questions cover only what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Use sudo: run a command as root
A new full quiz with random questions
Work again only on the questions you missed