Skip to content

Beginner · Permissions, users and sudo · topic 9 of 57 · 35 min

File permissions: rwx, chmod and chown

Explanation

Every file has an owner, a group and three sets of permissions: for the owner (u), the group (g) and others (o). Each set is read (r), write (w) and execute (x). chmod changes them, by letters (u+x) or numbers (r=4, w=2, x=1, so 750); chown changes the owner and group.

Reading permissions

bash
$ ls -l /srv/payroll.csv
-rw-r-----. 1 meena finance 20480 Oct 19 11:00 /srv/payroll.csv
Owner meena can read and write; the finance group can read; everyone else gets nothing.

After 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.

PermissionOn a fileOn a directory
r (read)Read the contentsList the names inside (ls)
w (write)Change the contentsCreate, delete or rename files inside
x (execute)Run it as a program or scriptEnter 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.

LettersSumDigit
rwx4+2+17
rw-4+26
r-x4+15
r--44
---00

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

bash
$ 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

bash
# 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 tree
Only root can give a file to another user. A normal user can change the group only to a group they belong to.

Common 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).
Tip: stat file shows the permissions both ways at once, for example Access: (0640/-rw-r-----). Use it to check your sums.

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

owner · group · others

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.

bash
# 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/ledgers

Practice (0/3 done)

Task 1 · Question

What is rwxr-x--- as a number?

Task 2 · Code · Python

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".

Task 3 · Code · Python

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

Question 1

1.What does chmod u+x run.sh do?

Question 2

2.To delete a file inside a directory, you need…

Question 3

3.What does chown :web /srv/site do?

Question 4

4.A file is owned by asha with mode 604 (rw----r--). asha is also in the file's group. Can asha read it?

Question 5

5.Why is chmod 777 a bad fix?

Read more (free)

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.

From Users, groups and permissions

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.

From Linux basics for backend developers

What does chmod 755 give "others"?Related

Answer: Read and execute

The last digit 5 = 4 (read) + 1 (execute).

From Linux security basics

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.

From Access control lists (ACLs)

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

  1. 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.
  2. Module 2 exam: Permissions, users and sudo · 14 questions · 25 min, without help. 6 topics of this module still to finish first.
  3. Then the next module, Processes, services and logs. 57 topics left in the course before the final exam.

← Previous: Creating and managing users and passwords