Lesson 5 of 12 · 9 min · Users and access
Users, groups and sudo
What you will learn
- How Linux identifies users and groups by number, and what the files
/etc/passwd,/etc/shadowand/etc/grouphold - Creating, changing and removing users and groups
- The difference between root, system accounts and normal users
- How
sudoworks, how to grant it, and safe habits
In simple words
A Linux machine is like an office building. Everyone who enters has an ID card (a user account), people in the same team share some doors (groups), and one master key opens everything (the root account). This lesson shows how accounts are made and how to borrow the master key safely for a single task.
Words to know
- User: an account on the machine. Programs run as users too, not only people.
- root: the administrator account. It can do anything, so a mistake as root can break everything.
- Group: a set of users who get the same access.
- sudo: a command that runs one instruction with administrator rights, and keeps a record of it.
Users and groups are numbers
The kernel does not know names. It knows UIDs (user ids) and GIDs (group ids), and file ownership stores those numbers. Names are a convenience for people. Check who you are:
id
uid=0(root) gid=0(root) groups=0(root)
By convention, UID 0 is root, the account that can do anything. UIDs below 1000 are system accounts that run services (such as www-data, postgres) and have no password login. Ordinary people start at 1000 on Debian and Ubuntu (RHEL-family systems also use 1000 today).
Every user has one primary group (given to new files) and can be in many supplementary groups. Groups are the main way to share access: put several users in devs, give the directory to the group devs, and permissions follow (see the previous lesson).
Where accounts live
| File | What it holds | Readable by |
|---|---|---|
/etc/passwd |
One line per account: name, UID, GID, comment, home directory, login shell | everyone |
/etc/shadow |
The password hashes and password ageing | root (and the shadow group) |
/etc/group |
One line per group with its members | everyone |
grep '^asha:' /etc/passwd
getent group devs
ls -l /etc/passwd /etc/shadow
asha:x:1001:1001::/home/asha:/bin/bash
devs:x:1002:asha
-rw-r--r-- 1 root root 927 Nov 12 12:28 /etc/passwd
-rw-r----- 1 root shadow 528 Nov 12 12:28 /etc/shadow
The fields of the passwd line are name : x : UID : GID : comment : home : shell. The x means "the password hash is in /etc/shadow" (hashes used to live in passwd, which everyone can read, so they were moved). A shell of /usr/sbin/nologin or /bin/false blocks interactive login: this is what service accounts use.
getent asks the system's account sources, which on a company machine may also include LDAP or other directories, not only these files. Prefer getent over reading the files when you want to be sure.
Managing users and groups
Run these as root (or with sudo):
useradd -m -s /bin/bash asha # -m creates the home directory, -s sets the shell
passwd asha # set a password interactively
groupadd devs
usermod -aG devs asha # -a = append. Without -a it REPLACES the user's groups.
id asha
uid=1001(asha) gid=1001(asha) groups=1001(asha),1002(devs)
A new group membership applies to new sessions: the user must log in again (or run newgrp) before existing shells see it.
More useful ones:
usermod -s /usr/sbin/nologin svc # remove login ability
usermod -L asha # lock the password; -U unlocks
userdel -r asha # delete the user and the home directory
groupdel devs
chage -l asha # password ageing info
Debian and Ubuntu also ship friendlier adduser and addgroup commands; they ask questions and apply defaults. In scripts, use useradd, which exists everywhere.
For service accounts, create a system user with no home and no login: useradd --system --no-create-home --shell /usr/sbin/nologin appsvc. A service that runs as its own user can damage only what that user may touch.
Becoming root, carefully: sudo
Logging in as root is risky: no audit trail of who did what, and every typo has full power. Instead users stay themselves and run single commands with sudo ("superuser do"):
sudo apt-get update # run one command as root
sudo -u postgres psql # run as another user
sudo -l # list what I am allowed to run
sudo -i # open a root shell (use rarely)
sudo asks for your own password (not root's), checks the rules, runs the command and logs it (in /var/log/auth.log on Debian-family systems, or the journal). Without the right to use sudo:
sudo: a password is required
Who may use sudo
On Ubuntu, members of the group sudo can run any command; on RHEL-family systems it is the group wheel:
usermod -aG sudo asha # Ubuntu/Debian
usermod -aG wheel asha # RHEL, Rocky, Amazon Linux
For finer control edit the rules with visudo (never open /etc/sudoers with an ordinary editor: a syntax error can lock everyone out; visudo checks it before saving). Prefer small files under /etc/sudoers.d/:
# /etc/sudoers.d/deploy -- allow only restarting one service, without a password
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp
Read it as: user deploy, on ALL hosts, as root, without a password, may run exactly this command. Give the narrowest rule that works. NOPASSWD: ALL is for automation accounts at most, and only with other protections.
Habits that keep servers safe
- Use
sudoper command rather than a permanent root shell. - One account per person, never a shared login.
- Disable direct root login over SSH (
PermitRootLogin no) and use SSH keys instead of passwords. The SSH course material in the Networking course covers keys. - Remove or lock accounts when people leave. Review who is in
sudoregularly.
Common mistakes
usermod -G devs ashawithout-a. It replaces all other groups, includingsudo. Always-aG.- Expecting a new group to work at once. Log out and in.
sudo echo text > /etc/file. The redirection is done by your shell, not sudo, so it fails with "Permission denied". Useecho text | sudo tee /etc/file.- Editing sudoers directly. Use
visudo.
Try it yourself
- Create a user
lab1with a home directory and Bash shell, and a grouplabs. Add the user to the group and prove it withid lab1. - Read the user's line in
/etc/passwd. Name each field. - Create a system account
appsvcthat cannot log in. Check thatsu - appsvcis refused. - Write a sudoers drop-in that lets
lab1run only/usr/bin/systemctl status ssh, and test it withsudo -l -U lab1.
Check yourself
Why does Linux store ownership as numbers, and what is UID 0?
The kernel works with ids, and names are mapped from /etc/passwd and /etc/group (or a directory service). UID 0 is root, which bypasses permission checks.
What is the difference between usermod -G devs asha and usermod -aG devs asha?
-G sets the supplementary groups to exactly the list given, removing the others. -aG appends to the existing groups. The -a is essential.
Why are password hashes in /etc/shadow and not in /etc/passwd?
/etc/passwd must be readable by all users (programs map ids to names), so hashes there could be copied and cracked. /etc/shadow is readable only by root.
Why is sudo preferred over logging in as root?
It limits root power to individual commands, keeps a log of who ran what, lets you grant narrow rights, and uses each person's own credentials.
Interview questions
How do you give a user permission to restart only one service?
Add a rule with visudo or a file in /etc/sudoers.d/ that names the exact command, for example deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp, and check it with sudo -l -U deploy.
A user was added to a group but still gets "permission denied". Why?
Group membership is read at login, so existing sessions do not see it. The user must start a new session. Also check the file's mode and that the directory path has x for the group.
What is a system account and why does a service run as one?
A low-UID account with no login shell and no password, created for a daemon. Running the service as its own user limits the damage if the service is compromised.
Summary
- The kernel uses UIDs and GIDs; names come from
/etc/passwd,/etc/groupand/etc/shadow. - Use
useradd -m,usermod -aG,groupadd,userdel -r, and give services their own no-login accounts. - Grant administration with
sudo(groupsudoorwheel) and narrow rules throughvisudo. - Log in again after group changes; never use
usermod -Gwithout-a.
Going deeper
- sudo(8) manual page: how sudo works and what it logs.
- useradd(8) and passwd(5): account creation and the file format.
- Ubuntu Server: user management: official Ubuntu guidance for accounts and sudo.
See how much of this you can explain
A free mock interview asks you new questions on Linux for DevOps for DevOps Engineer and scores every answer, with what to fix.