Exercises — Module 2: Linux
Environment: an Ubuntu VM you can destroy — AWS free-tier EC2 (best: real SSH experience), or Multipass/VirtualBox locally, or WSL2 as fallback. Everything assumes you have sudo.
Part A — Warm-Up Drills (do daily for a week, 10 min each)
Drill 1 — Navigation speed run. Without arrow-key history: jump to /var/log, list files by size (ls -lhS), jump to your home, back to previous dir (cd -), print where you are.
✓ Under 30 seconds, no mistakes.
Drill 2 — File juggling. In /tmp/drill: create nested dirs a/b/c in one command; create 5 files f1..f5 in one command (touch f{1..5}); copy them all to a/b/; rename f1 to first; delete only f2 and f4.
✓ find /tmp/drill -type f shows exactly the files you expect.
Drill 3 — Reading files. Using /var/log/syslog (or any big log): show first 15 lines; last 15; lines 50–60 (sed -n '50,60p' or head+tail combo); count lines; follow it live while you open a second terminal and run logger "hello from drill".
✓ You saw your own message arrive in tail -f.
Drill 4 — Permission reps. Create script.sh. Set exactly: owner rwx, group r-x, others none — once with numeric mode, once with symbolic (u=rwx,g=rx,o=). Read back with ls -l and decode the string out loud.
✓ You can convert rw-r-x--x ↔ 651 instantly.
Drill 5 — Pipe gymnastics. From /etc/passwd: count users; list only usernames (cut -d: -f1); sort them; show only those containing "s"; count those.
✓ One pipeline per task, no temp files.
Part B — Guided Labs
Lab 1 — Server First Hour (60 min) — the ritual of every new server
- Launch Ubuntu EC2 (t3.micro), SSH in with the key.
apt update && apt upgrade -y.- Create user
deploywith sudo; copy your SSH key to it (ssh-copy-idor manualauthorized_keyswith correct 600/700 perms). - New terminal: confirm
ssh deploy@IPworks BEFORE step 5 (golden rule: never lock yourself out). - Harden:
PasswordAuthentication no,PermitRootLogin noin sshd_config; restart sshd; verify password login now fails. - Add
~/.ssh/configentry on your laptop sossh labworks. ✓ You can be on a fresh server, hardened, in under 15 minutes — time yourself on a second fresh instance.
Lab 2 — Service Lifecycle with nginx (45 min)
- Install nginx. Verify three ways:
systemctl status,curl localhost,ss -tulpn | grep 80. - Change the homepage in
/var/www/html/index.htmlusing vim only (no nano). - Stop nginx; observe what each of the three checks shows now. Start it.
enableit and explain what that changes. - Watch logs live:
tail -f /var/log/nginx/access.logwhile curling from your laptop; find your laptop's IP in the log. ✓ You can state the difference between "installed", "running", "enabled", and "listening".
Lab 3 — Users, Groups & a Shared Directory (45 min)
- Create users
aliceandbob, groupwebteam; add both. - Create
/srv/sharedowned byroot:webteam, mode2775(look up what the 2 does — setgid). - As alice (
sudo -u alice bash): create a file in /srv/shared. As bob: append to alice's file. As a third user: try and fail. ✓ You can explain every permission bit that made each outcome happen. - Give alice passwordless sudo for exactly one command (
systemctl restart nginx) via/etc/sudoers.d/; prove she can restart nginx but NOTapt installanything.
Lab 4 — Process Detective (45 min)
- Start a CPU hog:
yes > /dev/null &. Find it intop; note load average climbing over 2–3 minutes; kill it by PID. - Start 3 background
sleep 3000 &jobs. List withjobsandps; kill one by PID, one withpkill -f, bring one to foreground and Ctrl+C it. - Run
sleep 500, suspend with Ctrl+Z, resume in background withbg, thendisownit. Log out, log in — is it alive? Now compare with a plain background job after logout. ✓ You can explain what happened to each and wherenohup/systemd fit.
Lab 5 — The Disk-Full Incident (Rehearse This One Twice) (30 min)
- Fill the disk:
fallocate -l <most-of-free-space> /tmp/bigfile(checkdf -hfirst; leave a little room). - Now "discover" it cold: pretend you just got paged. Run your diagnosis chain:
df -h→du -xh --max-depth=1 / 2>/dev/null | sort -rh | head→ drill down → find the culprit → delete → verify. - Write the 4-command sequence on a sticky note. This is the most common real incident in ops. ✓ From "paged" to "fixed" in under 3 minutes on the second rehearsal.
Lab 6 — Cron + Log Analysis (30 min)
- Cron job: every minute, append
date >> /tmp/heartbeat.log. Verify after 3 minutes. Then fix its real-world flaw: redirect stderr too, use absolute paths. - Change it to every 5 minutes, weekdays only, 9:00–17:00 (verify your expression at crontab.guru — then explain each field out loud).
- Download a sample nginx access log (or use your Lab 2 traffic). Produce: top 5 IPs, top 5 URLs, count of 404s, requests per hour — one pipeline each. ✓ Your four pipelines saved in a notes file; you'll reuse them for real.
Part C — Challenges
C1. Find, in one find command each: all .conf files under /etc modified in the last 7 days; the 5 largest files under /var; all files in /tmp older than 3 days (and delete them).
C2. Create a file named -rf and another named my file (final).txt in a dir. Now delete both safely. (This teaches quoting/-- forever.)
C3. Using only ss, ps, and /proc, answer: what process listens on port 22, what's its PID, when did it start, what user runs it?
C4. Archive /etc (as root) to a timestamped .tar.gz, copy it to your laptop with scp, extract it locally into a sandbox dir, and verify one known file survived the round trip (diff).
C5. Set up key-based SSH from your VM back to... itself (ssh localhost without password). Sounds silly — it's exactly what CI runners and Ansible need, and it exercises the whole keys/permissions chain.
Part D — Break & Fix Scenarios
D1 — Broken nginx config. Add a syntax error to nginx.conf, run systemctl restart nginx (fails). Diagnose using systemctl status, journalctl -u nginx, and nginx -t; fix; restart.
✓ You used the error message, not guesswork.
D2 — Permission denied mystery. As root, create /opt/app/config.yml mode 600. As deploy, try to read it — reproduce the exact error an app would log. Fix it TWO ways (ownership change; group+mode change) and argue which is better.
D3 — Port already in use. Start python3 -m http.server 8080; in another terminal start a second one — read the error. Find and kill the first via ss -tulpn, restart the second.
D4 — The zombie hunt. Run this to create a zombie: bash -c 'sleep 2 & exec sleep 300' & then after 2s check ps aux | grep defunct. Explain why you can't kill it, and what actually clears it.
D5 — Full lockout drill (use a disposable VM!). Set PasswordAuthentication no while ALSO removing your authorized_keys. You're locked out — recover via the cloud console (EC2 serial console / user-data / detach-volume trick). Painful once = careful forever.
Part E — Interview Rehearsal (out loud)
- A server is "slow." Walk me through your first five commands and what each rules out.
- Explain Linux file permissions to a junior, including the difference between file and directory execute bits.
killvskill -9; why is -9 a last resort?- Disk is at 100%. Go.
- How does SSH key authentication work, and why is it safer than passwords?
- What happens between power-on and your app running? (boot → systemd story)
Done when
Lab 5 under 3 minutes, all D scenarios fixed from error messages alone, Part E answered without notes.
