Every process on a Linux server runs as some user, and that user decides what the process may touch. When the Orbit API runs as its own orbit account instead of root, a bug in the API can damage Orbit’s data but not the operating system. When Maya and Leo each have their own login instead of sharing ubuntu, the logs tell you who restarted the API at 3 a.m. User management is where security, auditing and day-to-day operations meet.
In this lesson you create Orbit’s service account and the team’s human accounts, organise them into groups, control passwords and expiry, retire an account without leaving a mess, and write sudo rules that give people exactly the power they need. Use the orbit-lab VM: the sudo examples need systemd and real logins.
Identity is a number
The kernel does not know names. Every user has a UID and every group a GID; files store those numbers, and tools translate them to names using the account files. id shows both sides:
$ id
uid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu),4(adm),24(cdrom),27(sudo),30(dip),105(lxd)
Each user has one primary group (gid=), used for new files, and any number of supplementary groups. On Ubuntu, membership of sudo is what lets ubuntu run administrative commands. Your exact list may differ.
| UID range | Who | Examples |
|---|---|---|
| 0 | root, the superuser; permission checks do not apply | root |
| 1 to 999 | System accounts for services, no human logs in | www-data, postgres, orbit |
| 1000 and up | Human accounts | ubuntu, maya, leo |
| 65534 | nobody, an owner meaning “no one in particular” | NFS, unprivileged helpers |
Numbers matter beyond this machine. Files copied with tar, mounted over NFS or shared with a container through a volume keep their numeric UID, and the name shown depends on the machine reading them. That is why a file created by a container can appear as owned by “1001” or by an unrelated user on the host.
The account files
/etc/passwd
Despite the name, it holds no passwords. One line per account, seven fields separated by colons:
$ grep -E '^(root|www-data|orbit|maya):' /etc/passwd
root:x:0:0:root:/root:/bin/bash
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
orbit:x:996:996:Orbit API:/srv/orbit:/usr/sbin/nologin
maya:x:1001:1001:Maya (developer):/home/maya:/bin/bash
| Field | Example | Meaning |
|---|---|---|
| 1 | maya | Login name |
| 2 | x | Password lives in /etc/shadow |
| 3, 4 | 1001, 1001 | UID and primary GID |
| 5 | Maya (developer) | Comment, called GECOS |
| 6 | /home/maya | Home directory |
| 7 | /bin/bash | Login shell; /usr/sbin/nologin refuses interactive logins |
/etc/shadow
The password hashes and ageing rules, readable only by root (mode 640 with group shadow on Ubuntu, 000 on the Red Hat family):
$ sudo grep maya /etc/shadow
maya:$y$j9T$Fq3...$Xk1...:20760:0:99999:7:::
The fields are name, hash, last change (in days since 1 January 1970), minimum and maximum days between changes, warning days, inactivity days and expiry date. $y$ marks a yescrypt hash, the default on current Debian, Ubuntu and RHEL. A hash starting with ! means the password is locked; * or ! alone means the account never had one.
/etc/group
One line per group: name, password placeholder, GID and a comma-separated member list, such as orbit-devs:x:1003:maya,leo. Only supplementary members appear; users whose primary group it is are not listed.
Ask getent, not the files
On many servers, accounts also come from LDAP, Active Directory or a cloud directory through SSSD. getent passwd orbit and getent group orbit-devs ask the same name service the system uses, so they see local and directory accounts alike, in the same format as the files.
Warning: Never edit /etc/passwd, /etc/shadow or /etc/group with a normal editor while the system is in use. Use the commands below, or vipw and vigr, which lock the files and check them.
Creating accounts
useradd, adduser and Alpine
| Tool | Where | Behaviour |
|---|---|---|
useradd | Every distribution | Low-level; does only what the flags say. Best for scripts |
adduser | Debian, Ubuntu | Friendly wrapper: asks questions, creates home, sets a password |
adduser | Red Hat family | Just another name for useradd |
adduser, addgroup | Alpine (BusyBox) | Different flags: -S system, -D no password, -H no home |
A bare useradd maya on Ubuntu creates no home directory and may pick /bin/sh as the shell, so always pass the flags you want. That is also why scripts, Ansible roles and Dockerfiles use useradd with explicit options: the result is the same everywhere.
The orbit service account
A service account needs no password, no interactive shell and no home full of dotfiles:
$ sudo useradd --system --user-group --home-dir /srv/orbit --no-create-home --shell /usr/sbin/nologin --comment "Orbit API" orbit
$ getent passwd orbit
orbit:x:996:996:Orbit API:/srv/orbit:/usr/sbin/nologin
$ sudo su - orbit
This account is currently not available.
--system picks a UID below 1000 and skips password ageing, --user-group creates a matching orbit group, --home-dir /srv/orbit with --no-create-home points home at the app directory (which you create and own separately), and --shell /usr/sbin/nologin makes every login attempt print the message above and exit.
Packages often declare service users with systemd-sysusers instead: a one-line file such as u orbit - "Orbit API" /srv/orbit /usr/sbin/nologin in /etc/sysusers.d/orbit.conf, applied with sudo systemd-sysusers. It is declarative and safe to run twice.
Maya and Leo
$ sudo useradd --create-home --shell /bin/bash --comment "Maya (developer)" maya
$ sudo useradd --create-home --shell /bin/bash --comment "Leo (operations)" leo
$ sudo passwd maya
--create-home copies the starter files from /etc/skel into /home/maya. On Ubuntu you could run sudo adduser maya instead and answer its questions. On servers where people log in with SSH keys only, you can skip the password, but they will need one (or a passwordless rule) to use sudo.
Groups
Create the team group and add both people to it:
$ sudo groupadd orbit-devs
$ sudo usermod -aG orbit-devs maya
$ sudo gpasswd -a leo orbit-devs
$ id maya
uid=1001(maya) gid=1001(maya) groups=1001(maya),1003(orbit-devs)
Why -a matters
usermod -G sets the complete list of supplementary groups. Without -a (append), sudo usermod -G orbit-devs leo quietly removes Leo from every other group, sudo included. Always type -aG together, or use gpasswd -a user group, which only ever adds. gpasswd -d leo orbit-devs removes one membership.
Membership needs a new login
Groups are attached to a process when the user logs in, and children inherit them. If Maya is already logged in when you add her, her running shell still lacks orbit-devs, and she keeps getting “Permission denied”. She can log out and back in (a fresh SSH connection), or run newgrp orbit-devs, which starts a new shell with that group as primary (exit returns to the old one). Long-running services need a restart for the same reason.
Warning: Some groups are root in disguise. Members of docker can start a container that mounts / and edit anything, and lxd, disk and adm grant powerful access too. Treat adding someone to them as granting sudo.
Passwords, locking and expiry
passwd changes your own password; sudo passwd maya sets someone else’s. chage controls ageing and expiry:
$ sudo chage -E 2026-12-31 maya
$ sudo chage -d 0 leo
$ sudo chage -l maya
Last password change : Nov 03, 2026
Password expires : never
Password inactive : never
Account expires : Dec 31, 2026
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
-E sets an account expiry date (useful for contractors), -d 0 forces a password change at the next login and -M 90 would require a new password every 90 days. Many security standards now prefer long passwords plus MFA over forced rotation.
Locking is not what you think
sudo passwd -l maya and sudo usermod -L maya put a ! in front of the password hash. That blocks password logins only. Maya can still log in with an SSH key. To stop every kind of login, also expire the account:
$ sudo usermod -L --expiredate 1 maya
$ sudo usermod -U --expiredate '' maya
The first line locks and expires (day 1 is 2 January 1970, always in the past), and PAM refuses expired accounts whatever the authentication method. The second line undoes both.
Removing users safely
sudo userdel -r maya deletes the account, her home directory and her mail spool. It does not touch files she owns elsewhere, such as in /srv/orbit/shared or /var/backups, and those keep her old UID. If you later create a user who receives UID 1001, that person silently inherits them. A safer sequence for someone leaving the team:
- Lock and expire the account:
sudo usermod -L --expiredate 1 maya. - End running sessions and processes:
sudo loginctl terminate-user mayaand checkpgrep -u maya. - Check scheduled jobs (
sudo crontab -l -u maya) and archive the home:sudo tar -czf /var/backups/maya-home.tar.gz /home/maya. - Find files elsewhere:
sudo find / -xdev -user maya, and hand them over withchown. - Only then run
sudo userdel -r maya(or Debian’ssudo deluser --remove-home --backup maya), and remove her sudoers drop-ins and keys.
Later, sudo find / -xdev -nouser finds files whose owner no longer exists.
sudo: borrowing root, with a paper trail
sudo runs one command as another user (root by default) after asking for your password, then remembers it for a few minutes. Every use is logged with who, where and the full command. On Ubuntu, members of the sudo group may run anything; on the Red Hat family that group is wheel.
Ubuntu 26.04 ships sudo-rs
Since Ubuntu 25.10, sudo is sudo-rs, a Rust reimplementation designed as a drop-in replacement: same commands, same /etc/sudoers syntax, visudo included. A few rarely used features of the original are missing, and Canonical keeps the classic sudo available for anyone who depends on them. sudo --version tells you which one you have.
Reading sudoers rules
A rule reads “who, on which hosts, as whom, may run what”:
%sudo ALL=(ALL:ALL) ALL
leo ALL=(root) /usr/bin/systemctl restart orbit-api
deploy ALL=(orbit) NOPASSWD: /srv/orbit/releases/activate
The first line is Ubuntu’s default (% means a group). The second lets Leo run exactly one command as root. The third lets the CI deploy user run one script as orbit without a password, which is what an unattended pipeline needs.
Least privilege for Leo
Never edit /etc/sudoers directly: a syntax error can lock everyone out of sudo. Put each rule in its own file under /etc/sudoers.d/ and edit it with visudo, which checks the syntax before saving:
$ sudo visudo -f /etc/sudoers.d/leo-orbit
$ sudo visudo -c
/etc/sudoers: parsed OK
/etc/sudoers.d/leo-orbit: parsed OK
$ sudo -l -U leo
User leo may run the following commands on orbit-lab:
(root) /usr/bin/systemctl restart orbit-api
The file contains the single leo line shown above. Drop-in file names must not contain a dot or end in ~, or sudo silently skips them, so leo-orbit works and leo.conf does not. Leo now runs sudo systemctl restart orbit-api with his own password; sudo systemctl stop orbit-api or sudo bash is refused and logged. Arguments must match exactly, so restart orbit-api.service would be refused too. (The unit itself arrives in lesson 9.)
Tip: To let someone edit one root-owned file, use sudoedit: a rule like leo ALL=(root) sudoedit /etc/orbit/orbit.env lets him edit a temporary copy with his own editor, without ever running that editor as root.
su versus sudo -i
| Command | Password asked | Result |
|---|---|---|
su - | root’s | Root login shell; fails on Ubuntu, where root has no password |
sudo -i | yours | Root login shell, allowed and logged by sudoers |
sudo -u orbit bash | yours | Shell as the service user, even though it has nologin |
sudo -u orbit cat /etc/orbit/orbit.env | yours | One command as the service user: the best permission test |
The - in su - and the -i in sudo -i mean a full login: the target’s environment, home directory and profile. Prefer single sudo commands over root shells; each command is then logged individually and you cannot forget you are root.
Who is on the box?
$ who
ubuntu pts/0 2026-11-03 09:12 (10.89.13.1)
leo pts/1 2026-11-03 09:40 (203.0.113.80)
$ last -n 3
leo pts/1 203.0.113.80 Tue Nov 3 09:40 still logged in
ubuntu pts/0 10.89.13.1 Tue Nov 3 09:12 still logged in
reboot system boot 7.0.0-15-generic Tue Nov 3 09:11 still running
who lists current sessions, w adds what each one is doing and how long it has been idle, and last shows login history, including reboots. sudo lastb lists failed logins. On recent releases these records live in a small database (wtmpdb) rather than the old binary file, to survive the year 2038, but the commands look the same. For sudo activity, search the journal: sudo journalctl --since today --grep 'COMMAND=' (lesson 18).
Red Hat and Alpine: Rocky and Alma use the wheel group for full sudo and useradd behind adduser; useradd there creates the home directory by default. Alpine has no sudo out of the box: apk add sudo or the smaller doas. Create a service user with addgroup -S orbit && adduser -S -D -H -h /srv/orbit -s /sbin/nologin -G orbit orbit, and add someone to a group with addgroup maya orbit-devs.
Practice lab
Orbit’s team grows, and you set up the accounts on orbit-lab:
- Create the
orbitsystem account exactly as described, and prove that nobody can log in as it. - Create
mayaandleowith home directories and Bash, and the grouporbit-devswith both as members. - Allow Leo to restart
orbit-apiand nothing else through sudo, and check the rule. - Maya’s contract ends on 31 December 2026. Make the account expire then, and practise the full “lock everything now” command on a test user.
Show a solution
Run sudo useradd --system --user-group --home-dir /srv/orbit --no-create-home --shell /usr/sbin/nologin orbit; sudo su - orbit answers “This account is currently not available”. Create people with sudo useradd --create-home --shell /bin/bash maya (and leo), then sudo groupadd orbit-devs and sudo usermod -aG orbit-devs maya (usermod takes one user at a time, so repeat it for leo), and confirm with getent group orbit-devs. Write leo ALL=(root) /usr/bin/systemctl restart orbit-api with sudo visudo -f /etc/sudoers.d/leo-orbit, validate with sudo visudo -c and inspect with sudo -l -U leo. For the contract, sudo chage -E 2026-12-31 maya and check with sudo chage -l maya; on a throwaway account created with sudo useradd -m test1, sudo usermod -L --expiredate 1 test1 blocks both password and key logins, and sudo userdel -r test1 cleans up.
Common mistakes
- Running usermod -G without -a. It replaces every supplementary group, often removing sudo access.
- Expecting group changes in an open session. Log in again or use
newgrp. - Treating passwd -l as “account disabled”. SSH keys still work until the account is expired.
- Granting wildcards in sudoers. A rule like
systemctl *also allowssystemctl edit, which opens an editor as root and gives a full shell. Allowingless,vim,teeorbashthrough sudo is the same as allowing everything.
Exercise
Leo asks to join orbit-devs. You run sudo usermod -G orbit-devs leo, and an hour later Leo reports that sudo now says he is not allowed to run anything. What happened?
Show answer
Answer: B. -G sets the full list. Add him back with sudo usermod -aG sudo leo (from another admin account) and use -aG or gpasswd -a in the future.
Exercise
You added Maya to orbit-devs five minutes ago. In her open SSH session, id still does not show the group and she cannot write to /srv/orbit/shared. What should she do?
Show answer
Answer: C. A process gets its group list at login and children inherit it. A new session picks up the change; newgrp starts a subshell with the group. A reboot works but is never needed.
Exercise
A contractor’s last day is today. He logs in with an SSH key. Which action actually stops all of his logins while keeping his files for review?
Show answer
Answer: C. Locking the password does not affect key authentication; an expired account is refused by PAM for every method. userdel -r would destroy the home directory you still want to review.
Key takeaways
- The kernel tracks numeric UIDs and GIDs;
/etc/passwd,/etc/shadowand/etc/groupmap them to names, andgetentandidquery them. - Create service accounts with
useradd --system, a nologin shell and a fixed home; create people with explicituseraddflags or Debian’sadduser. - Use
usermod -aG(never-Galone) and remember that new group memberships need a new login. passwd -lonly blocks passwords; add--expiredate 1to stop SSH keys too, and retire users in steps beforeuserdel -r.- Grant sudo with small drop-ins in
/etc/sudoers.d/, edited withvisudo, listing exact commands. Ubuntu 26.04 runs sudo-rs with the same syntax.
FAQ
What is the difference between useradd and adduser?
On Debian and Ubuntu, useradd is the low-level tool that does only what its flags say, while adduser is an interactive wrapper that creates the home directory, copies /etc/skel and asks for a password. On the Red Hat family adduser is simply a link to useradd, and on Alpine it is BusyBox’s own tool with different flags. Use useradd with explicit options in scripts.
How do I add an existing user to a group?
Run sudo usermod -aG groupname username or sudo gpasswd -a username groupname. The -a matters: without it, usermod -G replaces all supplementary groups. The user must start a new login session, or run newgrp groupname, before the membership takes effect.
How do I give a user sudo access?
For full access, add them to the admin group: sudo usermod -aG sudo maya on Ubuntu or wheel on the Red Hat family. For limited access, create a drop-in with sudo visudo -f /etc/sudoers.d/maya containing only the commands she needs, with full paths. Check the result with sudo -l -U maya.
Why can’t I log in as root on Ubuntu?
Ubuntu ships with the root password locked, so su - and password logins as root fail by design. Administrators use sudo with their own account, which keeps an audit trail of who did what. Use sudo -i when you really need a root shell.
Where are Linux passwords stored?
As salted hashes in /etc/shadow, readable only by root. Modern distributions use yescrypt (the hash starts with $y$); older systems used SHA-512 ($6$). The x in /etc/passwd just points to the shadow file, and plain-text passwords are never stored.
How do I see who is logged in and who logged in recently?
who lists current sessions, w also shows what each user is running and how long they have been idle, and last shows the login history including reboots. sudo lastb shows failed attempts, and the journal records every sudo command with the user who ran it.