#Why this matters
File ownership and rwx permissions only protect a Linux system if people and background services run under separate, least-privileged identities. If every engineer logs in as root or every service shares one account, a single mistake exposes the entire host. Understanding users, groups, and sudo lets you read account records, grant group access cleanly, and audit who can elevate privileges.
#Users, UIDs, and account files
Inside the kernel, permissions are checked against numeric User IDs (UIDs) and Group IDs (GIDs), not names. Every Linux host reserves UID 0 for root, the superuser who bypasses standard file permission checks. System and service accounts (such as www-data or sshd) usually hold low UIDs (typically 1-999), while normal human logins start at UID 1000.
Two files in /etc hold user account data:
| File | Typical permissions | What it stores |
|---|---|---|
/etc/passwd |
644 (root:root) |
Username, placeholder x, UID, GID, comment, home path, login shell |
/etc/shadow |
640 (root:shadow) or 000 |
Salted password hashes and password expiration settings |
Notice that /etc/passwd does not store actual passwords; the x in field two tells the system to look up the hash in /etc/shadow. Service accounts often end with /usr/sbin/nologin or /bin/false as their shell so nobody can open an interactive login session as that service.
#Groups and managing accounts
Every user belongs to one primary group (recorded in /etc/passwd) and zero or more supplementary groups (listed in /etc/group). Groups are the cleanest way to share directories or logs without resorting to chmod 777.
Common commands for inspecting and managing accounts:
whoamiandid: show your current username, UID, primary GID, and all active group memberships.useradd -m -s /bin/bash analyst: creates useranalyst, builds/home/analyst(-m), and sets Bash as the shell (-s).usermod -aG auditors analyst: appends (-a) theauditorsgroup (-G) toanalyst. Always include-a; omitting it replaces all existing supplementary groups!
$ id analyst
uid=1001(analyst) gid=1001(analyst) groups=1001(analyst),1005(auditors)
$ grep -E "^(analyst|www-data):" /etc/passwd
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
analyst:x:1001:1001::/home/analyst:/bin/bash
$ sudo -l
Matching Defaults entries for analyst on lab:
env_reset, mail_badpass
User analyst may run the following commands on lab:
(ALL : ALL) ALLNote that after adding an account to a new group with usermod -aG, the user's running terminal session still holds the old group list until they start a new login session (or open a fresh shell via su - analyst).
#Switching users and sudo
Two tools let you act as another user:
su - targetuserswitches totargetuser(orrootif omitted) and asks for the target user's password. The-flag loads a clean login environment (HOME,PATH) for that account.sudo commandruns a single command asroot(or another specified user withsudo -u), asks for your own password, checks/etc/sudoers(and/etc/sudoers.d/), and logs the invocation to/var/log/auth.logorjournalctl.
- Run sudo commandVerify user in /etc/sudoers
- Prompt passwordUser enters own password
- Log and executeRecord event in auth log and run
Because sudo enforces per-command rules and ties every privileged action to an individual account, defenders strongly prefer sudo over sharing the root password with su. Always edit /etc/sudoers with visudo, which checks syntax before writing so a typo cannot lock out administration.
#Common mistakes
- Forgetting
-ainusermod -aG: Using-Galone overwrites a user's supplementary groups, often strippingsudoorwheelaccess. - Working permanently as
root: Opening arootshell withsudo sufor routine browsing removes per-command accountability and increases the blast radius of a typo. - Overly broad
sudoersrules: GrantingNOPASSWD: ALLor allowingsudoon interactive tools likevim,less, orpython3effectively grants a full root shell. - Editing
/etc/sudoerswithoutvisudo: A single syntax mistake in/etc/sudoersbreakssudofor every administrator on the host.
#Summary
Linux separates users and services by numeric UID and GID, storing public metadata in /etc/passwd, password hashes in /etc/shadow, and group memberships in /etc/group. Tools like id, useradd, and usermod -aG keep accounts clean and least-privileged, while sudo provides auditable, command-level elevation instead of shared root logins. Auditing sudo -l, group memberships, and service login shells keeps local privilege boundaries intact.