Skip to content
Visual explainer 3h to learn 4 steps

Permissions, ownership & umask

Set exactly the right permissions and ownership with chmod, chown and umask.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 10 minutes to read.

Topic 6 of 42

In short

Every file has an owner, a group and three sets of read, write and execute permissions for the user, the group and everyone else. chmod changes them using symbolic (u+x) or octal (750) notation, chown changes ownership and umask sets defaults for new files. Getting these right is the first line of defence on any server.

Explained simply

Every file and directory on Linux has an owner (a user), a group, and three sets of permissions: one for the owner, one for the group and one for everyone else. Each set says whether that person may read (r), write (w) or execute (x).

You see this in ls -l as a string like rw-r-----. Read it in threes. Here the owner can read and write, the group can only read, and everyone else can do nothing. Admins also write it as numbers: read is 4, write is 2, execute is 1, and you add them up, so rw- is 6 and r-- is 4.

chmod changes the permissions, chown changes the owner and group, and chgrp changes only the group. umask decides what permissions brand-new files get, so you do not have to fix every file by hand.

Permissions are your first and simplest security tool. Most Permission denied errors on a server come down to the wrong owner or one missing bit, and you can find it in a minute once you know where to look.

Press Play on a diagram to walk through it one step at a time.

MapReading -rwxr-x--- 1 ana dev script.sh
-rwxr-x---
  • Type: -
    • - regular file, d directory, l symlink
  • User (ana): rwx
    • Octal 7 = 4 + 2 + 1
  • Group (dev): r-x
    • Octal 5 = 4 + 1
  • Other: ---
    • Octal 0, no access
FlowDebugging a Permission denied on /srv/app/config.yml
  1. 1
    Who am I?

    id shows the user and every group

  2. 2
    Walk the path

    namei -l shows each directory's mode

  3. 3
    Missing x on a directory?

    Every parent needs x to pass

  4. 4
    Check the file

    Owner, group and the matching class bits

  5. 5
    Still denied?

    Look for ACLs (+) or SELinux (.)

In depth

rwx means something different on directories

On a file, r lets you read the content, w lets you change it, and x lets you run it as a program. On a directory, think of it as a list of names. r lets you list the names. w lets you add, remove or rename entries in that list. x lets you enter the directory and reach the files inside it.

This leads to surprises. Deleting a file needs w on the directory, not on the file, so you can delete a read-only file that root owns if it sits in your own directory. r without x lets you see names but ls -l shows question marks for their details. x without r lets you open a file only if you already know its exact name. To reach /srv/app/config.yml you need x on /, /srv and /srv/app.

CompareThe same bits on a file and a directory
On a file
  • r: read the content
  • w: change the content
  • x: run it as a program
On a directory
  • r: list the names inside
  • w: create, delete, rename entries
  • x: enter it and reach files inside

Symbolic chmod and the capital X

Symbolic mode is who, operator, permissions. Who is u (owner), g (group), o (others) or a (all). The operator is + to add, - to remove and = to set exactly. You can chain them with commas: chmod u=rwx,g=rx,o= script.sh. If you leave out who, as in chmod +x file, it acts like a but does not set bits that your umask would block.

The capital X adds execute only to directories, and to files that already have execute for someone. That makes it perfect for recursive changes. chmod -R 755 dir would make every text file executable; chmod -R u=rwX,g=rX,o= dir gives directories the x they need and leaves normal files without it.

chmod --reference=a.txt b.txt copies a mode from one file to another.

  • chmod g+w file adds group write and leaves everything else
  • chmod o= file removes all rights for others
  • chmod 640 file sets the whole mode at once

Who may change ownership

Only root can give a file to another user. Otherwise a user could dodge disk quotas or plant files that look like someone else's. A normal user can change a file's group, but only to a group they belong to.

chown alice file sets the owner, chown alice:web file sets owner and group, chown :web file sets only the group, and chown alice: file sets the owner and her login group. -R works recursively; add -h to change a symlink itself rather than its target.

One trap: changing the owner or group of an executable clears its SUID and SGID bits, even when root does it. So set special bits after chown, not before. Use stat file to see the numeric mode, owner, group and all timestamps in one view.

  • chown -R app:app /srv/app gives a whole tree to the app user
  • chgrp web file is the same as chown :web file
  • stat -c '%a %U %G' file prints mode, owner and group

umask is a mask, not a subtraction

Programs ask for a mode when they create something: usually 666 for files and 777 for directories. The kernel then clears every bit that is set in the umask. With umask 022, files get 644 and directories 755. With 027, files get 640 and directories 750. With 077, only the owner gets anything.

It is a bit mask, not arithmetic. umask 033 gives files 644, not 633, because there is no execute bit to remove in the first place. umask also never adds bits: a new file never starts out executable, however you set it.

Your default umask comes from the login setup (/etc/login.defs, /etc/profile or /etc/bashrc, depending on the distribution). Change it for yourself in ~/.bashrc, and for a service with UMask= in its systemd unit.

FlowHow umask 027 shapes a new file
  1. Step 1
    Program requests 666

    rw-rw-rw-

  2. Step 2
    umask 027 clears bits

    Group loses w; others lose rwx

  3. Step 3
    Result 640

    rw-r----- for the new file

Learn it in 4 short steps

About 45m each. Track them with a timer in your plan.

  1. 1
    Read permissions in ls -l output~45m
  2. 2
    Use symbolic and octal chmod~45m
  3. 3
    Change owners with chown and chgrp~45m
  4. 4
    Set default permissions with umask~45m

Key points

  • r = 4, w = 2, x = 1; add them per class to get octal modes like 640.
  • Only the first matching class applies: owner, else group, else other.
  • chown user:group file changes both at once; -R recurses.
  • The default umask 022 gives new files 644 and new directories 755.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
ls -l script.sh
chmod 750 script.sh             # rwxr-x---
chmod u+x,g-w,o= notes.txt
sudo chown ana:dev /srv/project -R
umask                           # usually 0022
umask 027; touch new; ls -l new  # rw-r-----

Octal and symbolic chmod, recursive chown and umask in action.

Fix permissions on a web content tree

Someone ran chmod -R 777 on /var/www/site to make an error go away. You want files readable by the web server but writable only by the deploy user. Step 1: give the tree to deploy with the web server's group (apache on RHEL, www-data on Ubuntu). Step 2: set directories to 750 and files to 640 in one pass using X. Step 3: confirm a sample. Step 4: check that the web server group can still read by testing as that user.

bash
sudo chown -R deploy:apache /var/www/site
sudo chmod -R u=rwX,g=rX,o= /var/www/site
ls -l /var/www/site | head
# drwxr-x--- ... css
# -rw-r----- ... index.html
sudo -u apache cat /var/www/site/index.html > /dev/null && echo readable

Find the one directory that blocks access

The app user gets Permission denied reading /srv/app/conf/app.yml, yet the file itself is 644. Step 1: namei -l lists every part of the path with its owner and mode. Step 2: look for a directory without x for the class app falls into. Here /srv/app/conf is 700 and owned by root, so app cannot pass through it. Step 3: fix that one directory rather than opening up the whole tree.

bash
sudo -u app cat /srv/app/conf/app.yml
# Permission denied
namei -l /srv/app/conf/app.yml
# dr-xr-xr-x root root /
# drwxr-xr-x root root srv
# drwxr-xr-x app  app  app
# drwx------ root root conf      <- no x for anyone else
# -rw-r--r-- root root app.yml
sudo chown root:app /srv/app/conf && sudo chmod 750 /srv/app/conf

Quick reference

  • r=4 w=2 x=1: rwxr-x--- is 750, rw-r--r-- is 644, rw------- is 600
  • chmod -R u=rwX,g=rX,o= dir sets dirs to 750 and files to 640 in one go
  • namei -l /full/path shows the owner and mode of every directory on the way
  • Deleting a file needs w and x on its directory, not w on the file
  • chown user: file sets the owner and that user's login group
  • chown clears SUID and SGID on executables, so set those bits afterwards
  • umask 077 in a script makes every file it creates private to the owner

Common mistakes

  • Using chmod -R 777 to make a problem go away.
  • Forgetting that directories need x to be entered.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A directory has mode drwxr-xr-x and is owned by root. Can user bob delete a file inside it that bob owns?

Question 2

2With umask 033, what mode does a new regular file get?

Question 3

3What does chmod -R a+X /data do to a plain text file that has mode 644?

Question 4

4A normal user runs chgrp web file.txt and gets Operation not permitted. Why?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Create notes.txt and set it so the owner can read and write, the group can read, and others have no access. Do it once with octal and once with symbolic notation.

  2. Exercise 2medium

    Make a directory /srv/reports where the group finance can add and read files, others can see nothing, and you can show that a user outside finance is refused.

  3. Exercise 3hard

    Write a one-liner that lists every file under /etc that is writable by others, then fix them so that others lose write without changing any other bit.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What does chmod 755 mean?
The owner can read, write and execute (7), and the group and others can read and execute (5). It is the usual mode for programs and directories.
Why do I need execute permission on a directory?
Execute on a directory lets you enter it and reach the files inside. Without it, even a world-readable file in that directory is out of reach.
How do I make a script runnable?
Run chmod u+x script.sh (or chmod +x) and start it with ./script.sh. It also needs a first line such as #!/bin/bash that names its interpreter.
Where is the default umask set?
It comes from the login setup, such as /etc/login.defs, /etc/profile or /etc/bashrc depending on the distribution. Override it for yourself in ~/.bashrc or for a service with UMask= in its unit file.

Go deeper

ShareXLinkedInWhatsApp