Beginner · Linux for DevOps · topic 6 of 53 · 30 min
Users, groups and permissions
Explanation
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.
asha:x:1001:1001:Asha Rao:/home/asha:/bin/bash
name:password placeholder:UID:GID:comment:home folder:login shellid # 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 groupsReading permissions
-rwxr-xr-x 1 deploy deploy 2048 Oct 19 10:00 deploy.sh
drwxr-x--- 2 deploy www 4096 Oct 19 10:00 releasesAfter 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.
| Octal | Letters | Typical use |
|---|---|---|
| 755 | rwxr-xr-x | Scripts, programs and normal folders |
| 644 | rw-r--r-- | Normal files such as configs and web pages |
| 700 | rwx------ | Private folders, such as ~/.ssh |
| 600 | rw------- | Secrets: private SSH keys, .env files |
chmod and chown
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/shopumask 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.
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519 ~/.ssh/authorized_keysNeed 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
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)
What octal number matches rw-r--r--?
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.
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Processes, signals and systemd servicesLinux for DevOps · same module
- Bash scripting, text tools and cronLinux for DevOps · same module
- File permissions: rwx, chmod and chownLinux and Red Hat (RHCSA to RHCE) · Permissions, users and sudo
- Linux security basicsCyber Security: from zero to SOC analyst · Systems and cryptography
- Linux basics for backend developersBackend Development · Deployment and scaling
- Users, groups and the files behind themLinux and Red Hat (RHCSA to RHCE) · Permissions, users and sudo
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.
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.
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.
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.
What is the UID of root?Related
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 “Linux for DevOps” before its exam: Processes, signals and systemd services, Bash scripting, text tools and cron, Linux networking for DevOps.
- Module 2 exam: Linux for DevOps · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Git in depth. 53 topics left in the course before the final exam.