Beginner · Permissions, users and sudo · topic 9 of 57 · 35 min
File permissions: rwx, chmod and chown
Explanation
Reading permissions
$ ls -l /srv/payroll.csv
-rw-r-----. 1 meena finance 20480 Oct 19 11:00 /srv/payroll.csvAfter the type letter, the nine characters are three groups of three: owner, group, others. Each slot shows the letter if allowed, or a dash if not. Linux checks them in order: if you are the owner, only the owner set counts; else if you are in the group, only the group set counts; else the others set. root skips these checks for reading and writing.
| Permission | On a file | On a directory |
|---|---|---|
| r (read) | Read the contents | List the names inside (ls) |
| w (write) | Change the contents | Create, delete or rename files inside |
| x (execute) | Run it as a program or script | Enter it (cd) and reach files inside |
Note the directory row. Deleting a file needs w on the DIRECTORY, not on the file. And a directory without x is almost useless: you cannot cd into it or open anything inside, even if the files themselves are readable.
Numbers (octal mode)
Each permission has a value: r = 4, w = 2, x = 1. Add the values in each set to get one digit, so three digits describe all nine slots. This form is called octal because each digit is 0 to 7.
| Letters | Sum | Digit |
|---|---|---|
| rwx | 4+2+1 | 7 |
| rw- | 4+2 | 6 |
| r-x | 4+1 | 5 |
| r-- | 4 | 4 |
| --- | 0 | 0 |
So rwxr-x--- is 750, rw-r--r-- is 644, and rw------- is 600. Common choices: 644 for normal files, 755 for programs and public directories, 600 for secrets such as private keys, 750 or 770 for team directories.
chmod: changing permissions
$ chmod 640 payroll.csv # numbers: rw-r-----
$ chmod u+x deploy.sh # letters: add execute for the owner
$ chmod g-w,o-r report.txt # remove group write and others read
$ chmod o= secret.txt # others: nothing
$ chmod a+r notice.txt # a = all (u, g and o)
$ chmod -R g+rX /srv/team # recursive; capital X = execute only on directories (and files already executable)In the letter form, who is u, g, o or a; the operator is + (add), - (remove) or = (set exactly); and then the permissions. Letters are best for small changes; numbers are best for setting everything at once.
chown and chgrp: changing owner and group
# chown meena payroll.csv # new owner
# chown meena:finance payroll.csv # owner and group together
# chown :finance payroll.csv # group only (same as chgrp finance payroll.csv)
# chown -R apache:apache /var/www/app # whole treeCommon mistakes
- chmod 777 to "fix" a permission error. It lets everyone change or replace the file, which is a real security hole. Find out which user needs what, and give only that.
- chmod -R 755 on a tree with private keys or config files makes them readable by all. Use -R g+rX or set files and directories separately with find -type f / -type d.
- Forgetting execute on the directories along a path. To read /srv/team/plan.txt you need x on /srv and /srv/team as well as r on the file.
- Running a script with ./deploy.sh and getting "Permission denied": add execute with chmod u+x deploy.sh (or run bash deploy.sh).
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
File permissions: rwx, chmod and chown · 1/7
Every file has an owner, a group and three sets of permissions.
Prefer a video by a teacher? Find free videos on “linux file permissions chmod chown octal explained” on YouTube ↗
In real life: A shared folder for the accounts team
A Kolkata trading company keeps its monthly ledgers on a RHEL file server. The accounts team must read and write them, the auditors group only reads (handled later with an ACL), and nobody else may even list them. The admin creates the group, sets the owner and gives 770 on the directory and 660 on the files.
# groupadd accounts
# mkdir /srv/ledgers
# chown root:accounts /srv/ledgers
# chmod 770 /srv/ledgers
# ls -ld /srv/ledgers
drwxrwx---. 2 root accounts 6 Oct 19 12:00 /srv/ledgersPractice (0/3 done)
What is rwxr-x--- as a number?
Write to_octal(perms) that turns a 9-letter permission string like "rwxr-x---" into a 3-digit string like "750". r = 4, w = 2, x = 1, - = 0. Return a string, so "---------" gives "000".
Write from_octal(mode) that does the opposite: "750" gives "rwxr-x---". Each digit becomes three letters: 4 adds r, 2 adds w, 1 adds x, and a missing one is "-".
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- umask and the special bits: SUID, SGID and stickyPermissions, users and sudo · same module
- Users, groups and the files behind themPermissions, users and sudo · same module
- Users, groups and permissionsDevOps: Git, Docker, Kubernetes and CI/CD · Linux for DevOps
- Linux basics for backend developersBackend Development · Deployment and scaling
- Linux security basicsCyber Security: from zero to SOC analyst · Systems and cryptography
- Bits: AND, OR, XOR and shiftsData Structures and Algorithms (DSA) for interviews · Tries, union-find and bit manipulation
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What does chmod u+x run.sh do?
Answer: Adds execute for the owner
u = owner, + = add, x = execute.
To delete a file inside a directory, you need…
Answer: w (and x) on the directory
Deleting changes the directory, so you need write and execute on the directory.
What does chown :web /srv/site do?
Answer: Changes only the group to web
With a leading colon only the group changes.
A file is owned by asha with mode 604 (rw----r--). asha is also in the file's group. Can asha read it?
Answer: Yes, as owner
The owner set rw- applies to asha, so she can read and write.
Why is chmod 777 a bad fix?
Answer: Anyone on the system can change or replace the file
It removes all protection; give only the needed access.
On a directory, what does the x permission allow?Related
Answer: Entering the directory with cd
For directories, x means you may enter (traverse) it.
What does chmod 600 .env do?Related
Answer: Only the owner can read and write the file
6 = rw for the owner, 0 for group and others. Good for secret files.
What does chmod 755 give "others"?Related
What does setfacl -m d:g:dev:rwx /srv/code do?Related
Answer: Sets a default ACL so new files give group dev rwx
d: makes a default entry that new files inherit.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “Permissions, users and sudo” before its exam: umask and the special bits: SUID, SGID and sticky, Access control lists (ACLs), sudo and the sudoers file.
- Module 2 exam: Permissions, users and sudo · 14 questions · 25 min, without help. 6 topics of this module still to finish first.
- Then the next module, Processes, services and logs. 57 topics left in the course before the final exam.