tech katta

Users and permissions: rwx, chmod, chown, sudo and umask

Lesson 07 of 23 · 6 min readFree

In this lesson

Read the permission string from ls -l, change it with chmod in octal and symbolic form, fix a deploy folder that your user cannot write to, and use sudo without running everything as root.

On this page
  1. Why this matters
  2. Users and groups
  3. Reading the permission string
  4. chmod: change permissions
  5. chown: change the owner and group
  6. sudo: borrow root for one command
  7. umask: default permissions for new files
  8. Try it

Linux is a multi-user system. Every file has an owner, a group and a set of permissions that decide who can read it, change it or run it. Most "it works on my laptop" deploy errors turn out to be a permissions problem, so this is one of the most practical lessons in the module.

Why this matters#

A typical failure looks like this: you copy a new release to the server, start the app and get Permission denied. Or the website returns a 403 because the web server cannot read your files. Or a secrets file is readable by every user on the machine. All three are solved by reading and setting permissions correctly, and the last one is a security issue, not just an annoyance.

All commands here were run on a real Linux machine as a normal user named deploy, which is the example user for this course.

Users and groups#

Every process runs as a user, and every user belongs to one or more groups. id shows who you are:

Shell
id
Text
uid=30033(deploy) gid=30033(deploy) groups=30033(deploy),33(www-data)

The user deploy has a numeric id, a main group also called deploy, and is also a member of www-data, the group the nginx web server runs under on Ubuntu. The names are labels; the system itself works with the numbers. User accounts live in /etc/passwd, which every user can read, and password hashes live in /etc/shadow, which only root can:

Shell
ls -l /etc/passwd /etc/shadow
Text
-rw-r--r-- 1 root root   2890 Dec  2 20:46 /etc/passwd
-rw-r----- 1 root shadow 1421 Dec  2 20:46 /etc/shadow

The special user root (id 0) can do anything and ignores permissions. That is why you avoid working as root: a typing mistake as root can destroy the system.

Reading the permission string#

ls -l starts each line with ten characters:

Text
-rw-r----- 1 deploy www-data 26 Dec  2 20:46 .env

The first character is the type: - for a file, d for a directory, l for a link. The next nine are three groups of three, for three audiences in this order:

  1. user (the owner),
  2. group (members of the file's group),
  3. other (everyone else).

Each group of three is r (read), w (write) and x (execute), with - where a permission is missing. So rw-r----- means: the owner can read and write, the group can read, everyone else gets nothing. The next two columns are the owner (deploy) and the group (www-data).

The system checks in order. If you are the owner, only the owner bits apply. If not, but you are in the group, the group bits apply. Otherwise the "other" bits apply. This is why a file can be rw------- for you and still be unreadable by a service running as another user.

What r, w and x mean for files and for folders#

For a file, r lets you read it, w lets you change it and x lets you run it as a program. For a directory they mean something different:

  • r lets you list the names inside.
  • x lets you enter the directory and reach the things inside it. Without x, nothing in the folder can be opened, even by an exact path.
  • w lets you create, rename and delete entries inside it. (Deleting a file depends on the folder's w, not the file's.)

We tested a folder containing a file, with different modes, as the owner:

Shell
chmod 600 d     # then try to list, read the file, and create a file
Text
mode 600:
secret.txt
cat: /srv/p7/d/secret.txt: Permission denied
touch: cannot touch '/srv/p7/d/new.txt': Permission denied

With rw- and no x, ls could still list the names, but the file could not be opened and nothing could be created. With mode 700 all three worked. Mode 500 (read and execute, no write) let us read the file but not create a new one.

Rule of thumb: folders almost always need x for whoever must use them, and a user who needs to reach a file needs x on every folder on the way to it.

chmod: change permissions#

chmod changes the bits. There are two ways to write them.

Symbolic form says who, what and which: u (user), g (group), o (other), a (all), then + (add), - (remove) or = (set exactly), then r, w, x.

Shell
chmod g+r app/shared/.env
chmod o-rwx,g=r app/shared/.env

Octal form uses numbers. Add up r=4, w=2 and x=1 for each audience, giving one digit each for user, group and other:

  • 7 is rwx (4+2+1), 6 is rw- (4+2), 5 is r-x (4+1), 4 is r--, 0 is nothing.
  • 755 means rwxr-xr-x: the owner can do everything, others can read and enter. This is normal for folders and programs.
  • 644 means rw-r--r--: the owner can edit, others can read. Normal for ordinary files.
  • 600 means rw-------: only the owner. Right for secrets such as .env and SSH private keys.
  • 640 means rw-r-----: the owner edits, the group reads. Right for a secret that a service in a group must read.
Shell
chmod 600 app/shared/.env
ls -l app/shared/.env
chmod 640 app/shared/.env
stat -c '%a %A' app/shared/.env
Text
-rw------- 1 deploy deploy 26 Dec  2 20:46 app/shared/.env
640 -rw-r-----

stat -c '%a %A' prints the octal and symbolic form together, which is a handy way to check yourself.

Making a script executable#

A script without x cannot be run directly:

Shell
/srv/p7/app/deploy.sh
chmod u+x /srv/p7/app/deploy.sh
/srv/p7/app/deploy.sh
Text
bash: line 1: /srv/p7/app/deploy.sh: Permission denied
deploying

Recursive changes and the capital X#

chmod -R applies to everything under a folder. Be careful: chmod -R 640 t gave the subfolder t/sub the mode rw-r----- with no x, so its contents could no longer be opened. Use the capital X in symbolic form, which sets x only on directories and files that already have it:

Shell
chmod -R u=rwX,g=rX,o= t
ls -ld t t/sub
Text
drwxr-x--- 3 root root 4096 Dec  2 20:46 t
drwxr-x--- 2 root root 4096 Dec  2 20:46 t/sub

chown: change the owner and group#

Only root can give a file away, so chown normally needs sudo. The form is chown user:group path; either part can be left out.

Shell
sudo chown -R deploy:deploy /srv/app
sudo chown :www-data /srv/app/shared/.env

Here is the deploy-folder problem from the start of the lesson, reproduced. The release folder was owned by root, and deploy tried to write into it:

Shell
echo x > /srv/p7/app/releases/20261202/new.txt
Text
bash: /srv/p7/app/releases/20261202/new.txt: Permission denied

ls -ld on the folder showed drwxr-xr-x ... root root, so only root had w. The fix was to hand the folder to the deploy user with chown -R deploy:deploy /srv/p7/app. After that the same write succeeded. Notice that the fix changed the owner. Making the folder world-writable with chmod 777 would also have worked and would be wrong, because then every user on the machine could modify your release.

For the "web server cannot read my file" case, the check is the same. A file with mode 640, owner deploy, group www-data was readable when we ran cat as www-data, and with mode 600 the same command failed with Permission denied. The folders above the file also need x for that group.

sudo: borrow root for one command#

sudo command runs a single command as root, after checking that your user is allowed. The rules are in /etc/sudoers, which you should edit only with sudo visudo, because it checks the syntax before saving. A broken sudoers file can lock you out.

Shell
sudo -n true
Text
sudo: a password is required

We ran that as deploy, which has no sudo rights in this test machine; -n makes sudo fail instead of asking. On your own server your user will normally be in the sudo group.

Habits that keep you safe: use sudo for the one command that needs it, not sudo -i for a root shell; never run sudo on a command you do not understand; and remember that sudo echo x > file redirects as your user, not as root (lesson 5 shows tee).

umask: default permissions for new files#

New files do not start from 777 or 666. The shell has a umask that removes bits from the defaults: files start from 666 and directories from 777, and the umask is subtracted.

Shell
umask 022
touch u1; mkdir ud1
ls -ld u1 ud1
umask 077
touch u2
ls -l u2
Text
-rw-r--r-- 1 deploy deploy    0 Dec  2 20:46 u1
drwxr-xr-x 2 deploy deploy 4096 Dec  2 20:46 ud1
-rw------- 1 deploy deploy 0 Dec  2 20:46 u2

With 022, group and other lose write, giving 644 for files and 755 for directories. With 077, group and other lose everything, giving 600 and 700, a good choice for a user who creates secrets. The value only affects files created afterwards, and only in that shell session unless you put it in ~/.bashrc.

Try it#

  1. Run id and ls -l /etc/passwd /etc/shadow. Explain, using the permission string, why you can read the first but not the second.
  2. Create a file with touch secret.txt. Set it to 600, then to 640 and 644 in turn, checking each result with stat -c '%a %A'.
  3. Make a folder d, put a file inside, and remove x from the folder with chmod u-x d. Try ls d and cat d/file and note which commands fail. Restore it afterwards.
  4. Write a one-line script hello.sh that prints a message. Try to run it as ./hello.sh, read the error, add x with chmod, and run it again.
  5. Run umask, then create a file and a folder and compare their modes. Run umask 077 and repeat.