Groups, sudo & Session Environment
Module 3 · Users & Access Management

Groups, sudo & Session Environment

Managing groups, safely elevating privileges with sudo and visudo, and the login-file chain that configures every shell session.

Groups: Associating Accounts by Security Requirement

Groups exist to associate accounts that share the same access needs, so you can grant a permission once instead of per-user. Group data lives in /etc/group.

TaskCommand
Create a new groupgroupadd groupname
Rename an existing groupgroupmod -n newname oldname
Add an existing user to a groupusermod -aG groupname username
Safely edit /etc/group by handvigr
⚠️ Common Pitfall: Never Forget -a With usermod -G

usermod -G groupname username replaces every supplementary group the user currently belongs to with just that one group. Forgetting -a (append) silently kicks the user out of every other group they were in — a common source of “why did this user suddenly lose access to X” tickets. Always use usermod -aG.


Privilege Escalation: sudo

sudo lets an authorized user run a single command with elevated (typically root) privileges, without sharing the root password directly — a much smaller blast radius than logging in as root outright.

ConceptDetail
/etc/sudoersContains the rules that define who can run what as whom
visudoThe only safe way to edit /etc/sudoers — validates syntax before saving, preventing a broken file from locking out sudo entirely
wheel group (or sudo group on Debian-family systems)The conventional group whose members are granted root-level privileges via sudoers
su -Switch user entirely, loading that user’s full login environment (commonly su - to become root outright)
⚠️ Common Pitfall: Editing /etc/sudoers Directly Can Lock Out Every Admin

Just like /etc/passwd, a syntax error in a hand-edited /etc/sudoers can disable sudo for the entire system — including you. visudo opens a temporary copy, checks the syntax on save, and only replaces the real file if it’s valid.


Who Am I, and Who Else Is Here?

TaskCommand
Print the current usernamewhoami
Print current user, plus what they’re logged in as and doingwho am i
List everyone currently logged inwho
List everyone logged in, what they’re running, and how long they’ve been idlew
Print the current user’s UID, GID, and group membershipsid
Show the login/logout history for a user (or everyone)last
Show the last time every user logged inlastlog

id is the command to reach for whenever a question hinges on effective permissions — it shows exactly which groups (and therefore which permission sets) are actually active for that user’s session, not just what /etc/group claims on paper.


Customizing the User Environment

Login shells read a chain of files to set up environment variables, aliases, and prompt behavior — in roughly this order:

FileScopeWhen it runs
/etc/profileSystem-wideEvery login shell
/etc/profile.d/*.shSystem-wide, modularSourced by /etc/profile — the preferred place to drop new system-wide scripts without editing /etc/profile itself
~/.bash_profile (or ~/.bash_login, ~/.profile)Per-userLogin shells only (e.g., a fresh terminal, SSH session)
~/.bashrcPer-userEvery new interactive shell, including non-login shells (e.g., opening a new terminal tab)
⚠️ Common Pitfall: .bashrc vs .bash_profile: The Classic Mix-Up

If an alias or environment variable works in a fresh SSH login but disappears when you open a new terminal tab in an already-running session (or vice versa), you’ve put it in the wrong file. .bash_profile runs once per login. .bashrc runs for every new interactive shell. Many setups just have .bash_profile source .bashrc to avoid duplicating settings.


🧠 Knowledge Check

1. You run `usermod -G developers alice` intending to add her to the developers group. What actually happens?

2. What's the only safe way to edit /etc/sudoers?

3. A user's custom PATH variable works after SSH login but disappears when they open a new terminal tab within that same session. Where did they most likely define it?


What’s Next

Module 4 moves from accounts to the disks those accounts’ data actually lives on — partitions, filesystems, and the full storage stack.