Linux Fundamentals · 3.3

🔑 sudo, Root & User Context

The key to administrative power — and the responsibility that comes with it⏱ ~2 min

In Linux, administrative tasks require root-level permissions. Unlike Windows where you log in as Administrator, Linux keeps the root account separate and uses sudo to grant temporary elevated privileges.

Three Ways to Run Commands as Root

MethodCommandWhen to Use
sudosudo adduser aliceSingle command — elevates just that one command
sudo susudo suSwitch to root shell — all following commands run as root (exit to return)
su rootsu root (requires root password)Rarely used in Ubuntu — use sudo su instead
bash
# Run a single command as root
sudo apt update
# Switch to root shell (stays root until you type exit)
sudo su
whoami # confirms: root
exit # return to normal user
# Check who you currently are
whoami
id # shows UID, GID, and all groups
# Who's currently logged in?
who
w # more details including what they're doing
🔒 SecurityRunning everything as root is dangerous. If you're root and accidentally type 'rm -rf /' you destroy the entire system with no prompt. Best practice: run as a normal user and only use sudo when needed. This is called 'principle of least privilege'.

The sudo Configuration File: /etc/sudoers

  • Controls who is allowed to use sudo and what they can run
  • Never edit this with a regular text editor — use sudo visudo which validates syntax before saving
  • Corrupted /etc/sudoers can lock everyone out of sudo permanently
  • Competition: check /etc/sudoers for unauthorized users with ALL=(ALL) NOPASSWD: ALL — that gives passwordless sudo to anyone
🧠Quick Checkfirst try = +5 XP

What does sudo do?

0 XP🔥 0 days