Skip to content

Beginner · Linux for DevOps · topic 6 of 53 · 30 min

Users, groups and permissions

Explanation

Every file in Linux has an owner, a group, and read, write and execute permissions for the owner, the group and everyone else. Permissions are written as letters (rwxr-xr-x) or as octal numbers (755). DevOps engineers use chmod, chown, sudo and groups daily, and follow least privilege: give each user and service only the access it needs.

Users and groups

Linux is a multi-user system. Each person and each service runs as a user. A user has a name and a number called the UID. root (UID 0) is the all-powerful administrator. Services such as nginx or postgres usually run as their own users, so a bug in one cannot easily damage the others.

A group is a named set of users. Groups make sharing easy: put everyone who deploys into a deploy group, and give that group access to the release folder. Users are listed in /etc/passwd and groups in /etc/group. Password hashes are kept separately in /etc/shadow, which only root can read.

output
asha:x:1001:1001:Asha Rao:/home/asha:/bin/bash
name:password placeholder:UID:GID:comment:home folder:login shell
A line of /etc/passwd and what each field means. The x means the password hash is in /etc/shadow.
bash
id                              # my UID, main group and all groups
id asha                         # the same for another user
sudo useradd -m -s /bin/bash asha   # create user asha with a home folder and bash
sudo passwd asha                # set her password
sudo usermod -aG docker asha    # ADD asha to the docker group
groups asha                     # list her groups
Creating a user and adding her to a group. On Ubuntu, adduser is a friendlier wrapper around useradd.
Careful: usermod -G without -a REPLACES all of the user’s extra groups with the one you name, which can remove sudo access by accident. Always write usermod -aG (a for append). Also, a new group only applies after the user logs out and in again (or starts a new shell with newgrp).

Reading permissions

output
-rwxr-xr-x 1 deploy deploy 2048 Oct 19 10:00 deploy.sh
drwxr-x--- 2 deploy www    4096 Oct 19 10:00 releases
ls -l output. The first character is - for a file or d for a directory, then three sets of rwx.

After the first character come three sets of three letters: for the owner (user, u), the group (g) and others (o, everyone else). r means read, w means write and x means execute. On a directory, r lets you list it, w lets you create and delete files in it, and x lets you enter it with cd. A dash means the permission is missing.

Octal numbers

Each letter has a value: r = 4, w = 2, x = 1. Add them for each set to get one digit. rwx = 4 + 2 + 1 = 7. r-x = 4 + 0 + 1 = 5. rw- = 6. r-- = 4. So rwxr-xr-x is 755, and rw-r--r-- is 644.

OctalLettersTypical use
755rwxr-xr-xScripts, programs and normal folders
644rw-r--r--Normal files such as configs and web pages
700rwx------Private folders, such as ~/.ssh
600rw-------Secrets: private SSH keys, .env files

chmod and chown

bash
chmod 755 deploy.sh            # set exact permissions with octal
chmod u+x deploy.sh            # add execute for the owner only
chmod g-w,o-r report.txt       # remove group write and others read
chmod -R 750 /opt/shop         # apply to a folder and everything inside
sudo chown deploy:www releases # change owner to deploy and group to www
sudo chown -R deploy:deploy /opt/shop
chmod changes permissions; chown changes the owner and group.

umask sets which permissions new files do NOT get. The common default 022 removes write for group and others, so new files are 644 and new folders 755.

root, sudo and least privilege

sudo runs one command as root, after checking that your user is allowed (usually by membership of the sudo group on Ubuntu or the wheel group on RHEL). Working as root all the time is dangerous: one typo can delete the system, and any program you run has full power.

Least privilege means every user and service gets only the access it needs, and nothing more. The web server can read the website files but not change them. The deploy user can write to /opt/shop but cannot read the database password file. If one part is hacked, the damage stays small.

SSH key permissions

SSH refuses to use keys that others can read. Your ~/.ssh folder should be 700, your private key (for example ~/.ssh/id_ed25519) 600, and ~/.ssh/authorized_keys on the server 600. The public key (.pub) can be 644.

bash
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519 ~/.ssh/authorized_keys
Fixes the "UNPROTECTED PRIVATE KEY FILE" and "Permission denied (publickey)" errors in many cases.
Careful: Common mistake: "fixing" a permission error with chmod 777 (everyone can do everything). It makes the problem disappear and opens a security hole. Find out which user needs which access, then grant only that, often by changing the owner with chown or adding a user to a group.

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

Users, groups and permissions · 1/7

Users, groups root = UID 0

Every person and service on Linux runs as a user, and users can belong to groups. Root is the all-powerful administrator.

Prefer a video by a teacher? Find free videos on “linux file permissions chmod chown users groups explained” on YouTube ↗

In real life: A deploy user for a hospital appointment app

A hospital chain runs its appointment booking API on Linux. The DevOps team creates a deploy user that owns /opt/appointments (chown -R deploy:deploy), while the app itself runs as a separate appbooking user that can only read the code. The database password file is 600 and owned by appbooking. Developers get SSH access as deploy but are not in the sudo group. When an auditor asks "who can change production code?", the answer is short and clear.

Practice (0/3 done)

Task 1 · Question

What octal number matches rw-r--r--?

Task 2 · Code · Python

Write two functions. to_octal(perms) takes 9 permission letters such as "rwxr-xr-x" and returns the octal string "755" (r = 4, w = 2, x = 1, - = 0). to_symbolic(octal) does the reverse: "640" gives "rw-r-----". Return strings, so "000" keeps its zeros.

Task 3 · Practice

On a practice Linux machine or VM (not a shared server), create a user called deployer with a home folder, create a group called webteam, add deployer to it, make a folder /opt/demo owned by deployer with group webteam and permissions 750, and put a secret file inside readable only by deployer. Write down the commands and the ls -l output.

Quick quiz

Question 1

1.On a directory, what does the x permission allow?

Question 2

2.Which command adds user ravi to the docker group without removing his other groups?

Question 3

3.Which permissions should a private SSH key file have?

Question 4

4.A web app cannot read its config file. What is the best fix?

Question 5

5.Where are user accounts listed on Linux?

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.

On a directory, what does the x permission allow?

Answer: Entering the directory with cd

For directories, x means you may enter (traverse) it.

Which command adds user ravi to the docker group without removing his other groups?

Answer: sudo usermod -aG docker ravi

-a appends. Without it, -G replaces all extra groups.

Which permissions should a private SSH key file have?

Answer: 600

Only the owner should read or write a private key. SSH rejects keys that others can read.

A web app cannot read its config file. What is the best fix?

Answer: Give the app’s user or group read access, for example with chown or chmod 640

Grant exactly the access needed (least privilege), not full access for everyone.

Where are user accounts listed on Linux?

Answer: /etc/passwd

/etc/passwd lists accounts; password hashes are in /etc/shadow.

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

Answer: Yes, as owner

The owner set rw- applies to asha, so she can read and write.

From File permissions: rwx, chmod and chown

Why use sudo instead of logging in as root?Related

Answer: Each admin action is tied to a named user and logged

Named users plus logging gives accountability and fewer mistakes.

From Linux security basics

With SSH keys, which key stays only on your laptop?Related

Answer: The private key

The public key goes to the server. The private key never leaves your machine.

From Linux basics for backend developers

What is the UID of root?Related

Answer: 0

root is always UID 0.

From Users, groups and the files behind them

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 “Linux for DevOps” before its exam: Processes, signals and systemd services, Bash scripting, text tools and cron, Linux networking for DevOps.
  2. Module 2 exam: Linux for DevOps · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Git in depth. 53 topics left in the course before the final exam.

← Previous: The Linux filesystem and everyday commands