$ sudo apt install devops-knowledge

Linux for DevOps
Instructor Guide

Step-by-step explanations with real terminal examples for every module

01
Introduction to Linux & Why It Matters in DevOps
Setting the stage — what Linux is and where students will encounter it

How to explain to students

Start by asking: "When you visit Netflix, GitHub, or Google — what OS do you think is running those servers?" The answer is almost always Linux. Explain that over 96% of web servers run Linux. DevOps is about automating those servers, so Linux is non-negotiable.

Use the analogy: "Windows is like an automatic car. Linux is a manual — harder to learn, but gives you full control over every gear."

bash — why-linux.sh
# Where will students use Linux in DevOps?
# ✅ Docker containers → run on Linux
# ✅ AWS/GCP/Azure VMs → Linux by default
# ✅ CI/CD pipelines (GitHub Actions, Jenkins)
# ✅ Kubernetes nodes → Linux

$ uname -a
Linux devbox 5.15.0 #1 SMP x86_64 GNU/Linux

# This single line tells you kernel, hostname, architecture
🐧
Free & Open Source
No licensing costs. Companies save millions deploying Linux at scale.
Lightweight
Can run on minimal hardware — perfect for containers and cloud VMs.
🔧
Customizable
Every part of the OS can be tweaked, scripted, or automated.
🔒
Secure by design
Permissions, users, and groups are first-class citizens.
02
Terminal Navigation & File Management
Moving around the filesystem and managing files from the command line

How to explain to students

Compare the terminal to a GPS for your computer. pwd is "where am I?", ls is "what's around me?", and cd is "go there". Give students a real folder structure to navigate.

Live demo: Create a fake project structure on screen and let students replicate it. Nothing teaches mkdir and cp faster than actually using them.

bash — navigation demo
$ pwd  # Print current directory
/home/student

$ ls -la  # List all files with details
drwxr-xr-x 3 student student 4096 Apr 25 10:00 projects
-rw-r--r-- 1 student student 220 Apr 25 09:00 .bashrc

$ mkdir -p projects/webapp/config
$ cd projects/webapp
~/projects/webapp $ touch index.html app.js

$ cp index.html index.html.bak  # Backup before editing!
$ mv index.html.bak archive/
$ rm -rf archive/  # ⚠️ No recycle bin!
pwd ls -la cd .. mkdir -p cp / mv / rm ⚠️ rm -rf is permanent
03
File Permissions, Users, Groups & SSH Keys
Linux security model — who can read, write, and execute what

How to explain permissions

Use the apartment analogy: A file is an apartment. The owner has a key. The group is like the floor — neighbors with shared access. Others are strangers. Permissions (rwx) decide what each can do.

Teach chmod with the numeric system. It clicks instantly once students see 7 = 4+2+1 = read+write+execute.

bash — permissions
# Read a permission string: -rwxr-xr--
# Position: [type][owner][group][others]
# r=4, w=2, x=1 → 7=rwx, 5=r-x, 4=r--

$ ls -l script.sh
-rw-r--r-- 1 student devs 512 Apr 25 script.sh

$ chmod 755 script.sh  # owner=rwx, group=r-x, others=r-x
$ ls -l script.sh
-rwxr-xr-x 1 student devs 512 Apr 25 script.sh

# SSH Key Setup
$ ssh-keygen -t ed25519 -C "student@devops"
Generating public/private ed25519 key pair.
Enter file: ~/.ssh/id_ed25519

$ ssh-copy-id student@server.example.com
Number of key(s) added: 1 ✓

# Now login without password:
$ ssh student@server.example.com
🔑
chmod 600 private keys
SSH private keys must be readable only by owner. SSH will refuse to work otherwise.
👥
useradd / usermod
Create users and add them to groups like sudo or docker.
🛡️
sudo vs su
sudo = one command as root. su = switch user entirely. Prefer sudo.
04
Package Management & System Updates
apt (Ubuntu/Debian) and yum/dnf (RHEL/CentOS) — installing and updating software

How to explain to students

Compare apt to the App Store — but for the terminal, and way faster. Ask students: "Imagine installing Photoshop just by typing one command." That's what package managers do.

Emphasize that in DevOps, servers must be kept updated for security. Outdated packages are a top cause of breaches. apt upgrade is like pressing "Update All" on your phone.

bash — package management
# === Ubuntu / Debian (apt) ===
$ sudo apt update  # Refresh package list
$ sudo apt upgrade -y  # Upgrade all packages
$ sudo apt install nginx git curl -y
$ sudo apt remove nginx
$ apt search python3  # Find packages

# === RHEL / CentOS (yum/dnf) ===
$ sudo yum update -y
$ sudo dnf install nginx -y  # dnf = modern yum

# Check if a service is running after install
$ sudo systemctl status nginx
● nginx.service - A high performance web server
Active: active (running) since Fri 2025-04-25
apt update apt upgrade apt install yum / dnf systemctl
05
Writing Bash Scripts for Automation
From one-liners to full automation scripts with variables, loops, and conditionals

How to explain to students

Tell students: "Every time you do something more than twice in the terminal, write a script." Scripts are just saved commands. Start with a simple "Hello, World" script, then build up to variables, if-else, and loops.

The shebang line #!/bin/bash is magic — explain it tells the OS which interpreter to use. Without it, the script might not run.

bash — first_script.sh
#!/bin/bash  # Always start with this!

# Variables
NAME="DevOps Student"
DATE=$(date +%Y-%m-%d)  # Command substitution
echo "Hello $NAME — today is $DATE"

# If-else
DISK=$(df / | awk 'NR==2{print $5}' | tr -d '%')
if [ "$DISK" -gt 80 ]; then
  echo "⚠️ Disk usage is ${DISK}% — WARNING!"
else
  echo "✅ Disk usage is ${DISK}% — All good"
fi

# Loop through files
for FILE in /var/log/*.log; do
  echo "Found log: $FILE"
done
📝
Always use quotes
Use "$VAR" not $VAR to avoid word-splitting bugs with spaces.
🧪
Test with set -x
Add set -x at the top to print each command as it runs — great for debugging.
🚨
Exit on error
Add set -e to stop the script immediately if any command fails.
06
Using AI to Write & Debug Bash Scripts
Leveraging AI tools as a DevOps pair programmer

How to explain to students

Teach students that AI is a force multiplier, not a replacement. The goal is to know enough Linux to verify and modify what AI generates. Bad prompt = bad script. Good prompt = great starting point.

Show a before/after: a vague prompt gives a generic script, a detailed prompt gives a production-ready one. Teach students to always read, test, and understand AI output before running it on a server.

bash — AI prompting examples
# ❌ Weak prompt → weak script
"Write a bash script to monitor disk"

# ✅ Strong prompt → production-ready script
"Write a bash script that:
- Checks disk usage on / every 5 minutes
- Alerts if usage exceeds 85%
- Logs to /var/log/disk-monitor.log with timestamps
- Sends email using sendmail if available
- Handles errors and uses set -e, set -u"

# Debug tip: Paste error into AI with context
./monitor.sh: line 12: [: too many arguments
# Ask: "What does this bash error mean and how do I fix it?"
ChatGPT Claude GitHub Copilot Always review AI output Understand before running
07
Project: Disk Usage Monitor with Alerts
Real-world bash script that monitors disk, logs results, and sends alerts

How to explain to students

This is where everything comes together. Walk through the script line by line before asking students to build it. Emphasize: this is the kind of script that runs on real production servers at companies like Amazon and Netflix.

bash — disk_monitor.sh (complete project)
#!/bin/bash
set -euo pipefail  # Strict mode

# ── Configuration ──────────────────────
THRESHOLD=85
LOG_FILE="/var/log/disk_monitor.log"
ALERT_EMAIL="admin@company.com"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')

# ── Functions ───────────────────────────
log() { echo "[$TIMESTAMP] $*" | tee -a "$LOG_FILE"; }

check_disk() {
  local USAGE
  USAGE=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
  log "Disk usage: ${USAGE}%"

  if [[ "$USAGE" -ge "$THRESHOLD" ]]; then
    log "🚨 ALERT: Disk at ${USAGE}% — threshold is ${THRESHOLD}%"
    echo "Disk alert on $(hostname)" | mail -s "Disk Alert" "$ALERT_EMAIL" 2>/dev/null || true
  else
    log "✅ OK — ${USAGE}% used"
  fi
}

# ── Main ────────────────────────────────
log "=== Disk Monitor Started ==="
check_disk

# Schedule with cron: */5 * * * * /usr/local/bin/disk_monitor.sh
📅
Add to cron
Run crontab -e and schedule this script every 5 minutes automatically.
📂
Log rotation
Mention logrotate — logs need to be rotated or they fill the disk you're monitoring!
🧩
Extend it
Challenge: add monitoring for CPU, memory, or check multiple mount points.
08
Quiz: Linux Commands & Permissions
10 MCQs + 2 fill-in-the-command questions

Sample quiz questions (interactive)

Q1. Which command shows the current working directory?
A
ls
B
cd
C
pwd
D
echo
Q2. What does chmod 644 file.txt mean?
A
Everyone can read and write
B
Owner: rw-, Group: r--, Others: r--
C
Owner: rwx, Group: r-x, Others: r--
D
Read-only for everyone
Q3. Which command installs a package on Ubuntu?
A
yum install nginx
B
brew install nginx
C
sudo apt install nginx
D
sudo get nginx
Q4. What does set -e do in a bash script?
A
Exit immediately if any command fails
B
Enable debugging output
C
Set an environment variable
D
Suppress all errors
Q5. Which SSH key type is recommended for new setups?
A
RSA 1024-bit
B
DSA
C
ed25519
D
ECDSA 256-bit

Fill-in-the-command

Fill 1: What command makes script.sh executable by the owner only?
Fill 2: Write the cron expression to run a script every day at 2:30 AM.
09
Assignment: Backup Script with Logging
Write a bash script that backs up a folder and logs all results

How to explain to students

Frame this as a real job task: "Your manager asks you to set up an automated nightly backup for the /var/www/html folder. It must log success or failure. You have 24 hours." This mirrors what junior DevOps engineers actually do in their first week.

📋 Assignment Requirements

  • Accept a source folder as a script argument (e.g., ./backup.sh /var/www/html)
  • Create a timestamped .tar.gz backup in /backups/ directory
  • Log success or failure with timestamps to /var/log/backup.log
  • Handle edge cases: source not found, backup dir doesn't exist, disk full
  • Use functions, variables, and proper error handling (set -e)
  • Bonus: Delete backups older than 7 days automatically
  • Bonus: Send an email alert on failure
bash — expected output when grading
$ ./backup.sh /var/www/html
[2025-04-25 02:30:01] Backup started for: /var/www/html
[2025-04-25 02:30:03] ✅ Backup created: /backups/html_2025-04-25_02-30-01.tar.gz
[2025-04-25 02:30:03] Size: 24M
[2025-04-25 02:30:03] Cleaning up backups older than 7 days...
[2025-04-25 02:30:03] ✅ Backup completed successfully

$ cat /var/log/backup.log
[2025-04-25 02:30:01] Backup started for: /var/www/html
[2025-04-25 02:30:03] ✅ Backup created: html_2025-04-25_02-30-01.tar.gz
📊
Grading rubric
Script runs: 30pts. Logging works: 25pts. Error handling: 25pts. Code quality: 20pts.
🎯
Common mistakes
Hardcoded paths, no quotes around variables, missing shebang, no error checking.
💡
Extend challenge
Add the cron job that runs this script automatically at 2 AM every night.