Scenario
You are training a junior admin on switching users and using sudo correctly. The goal is to demonstrate safe escalation patterns: run a single privileged command, open a root login shell for multi-step work, create a temporary admin user, grant them sudo via the correct group, validate access, then clean everything up.
The difference between I can run sudo and I understand privilege boundaries shows up in the command choices you make under pressure.
Objective
- Run a privileged command with
sudo. - Enter a root login shell using
sudo -i. - Create a temporary user account.
- Grant sudo access via group membership.
- Switch into the new account with a full login environment using
su -. - Prove sudo works with
sudo whoamiand clean up withuserdel -r.
Concepts
-
Use
sudofor single commands when possible. A full root shell increases blast radius if you mistype. -
A root login shell via
sudo -imatters because it loads root’s login environment, not just a root UID. - Group-based sudo is common. The allowed group name varies by distro family, so you verify group membership and then verify behavior.
-
Switching users with
su -loads the target user’s login environment, which affects PATH and profile behavior.
Walkthrough
sudo ls /root
Use sudo for one-off privileged commands. It limits blast radius and keeps a clean audit trail.
[sudo] password for lab:
file1.txt backup.tar.gz
sudo -i
sudo -i simulates a root login shell. Use it when you have several admin tasks to perform and want consistent
root environment behavior.
useradd tempsudo
Creating a temporary account is a safe way to validate group rules and sudo policy without touching production users.
passwd tempsudo
This makes the account usable for su - and for interactive sudo prompts.
The privileged group name differs by distro family. Use the appropriate command for your host, then verify.
usermod -aG sudo tempsudo
usermod -aG wheel tempsudo
Group membership is not the same as effective capability. You still prove behavior with an actual sudo command.
exit
Dropping back to an unprivileged context is part of clean operational discipline.
su - tempsudo
su - loads the target user’s login environment, which matters when you are testing PATH, profiles, and group
membership behavior.
sudo whoami
This is the quick identity proof. If sudo is working, the output should be root.
root
sudo userdel -r tempsudo
Always remove training accounts and test access once verification is complete.
Breakpoints
Group membership may not apply to an existing session. Re-enter a full login session with su -, or log out and
back in, then test again with sudo whoami.
If you used sudo -i and forget you are root, you may accidentally run risky commands. Confirm identity with
whoami before changes, and exit back to a normal shell when you are done.
Cleanup checklist
- Confirm the test account is gone.
- Confirm there are no remaining processes owned by the test account.
getent passwd tempsudo
ps -u tempsudo
Reference
sudo: Run commands with elevated privileges.-
sudo -i: Start a root login shell.-i: Use a login shell environment.
su: Switch user.-
su -: Switch user with a full login environment.-: Load the target user’s login environment.
useradd: Create a user account.passwd: Set or change a user password.-
usermod: Modify a user account.-a: Append to supplementary groups.-G: Set supplementary groups.
userdel: Delete a user account.-
userdel -r: Delete a user and remove their home directory.-r: Remove home directory and mail spool.
whoami: Print effective username.ls: List directory contents./root: Root user’s home directory.getent: Query system databases.ps: List processes.