Linux Administration Automation¶
Overview¶
Linux administration is repetitive — perfect for scripts that are idempotent, logged, and safe under sudo.
This is Tutorial 12 in Module 12: Linux Administration of the REBASH Academy Shell Scripting for DevOps Engineers series — written for Linux administrators, DevOps engineers, SREs, and platform engineers who automate production hosts with Bash.
Prerequisites¶
- Process Automation — Signals and Traps
- Bash 4.2+ on Linux (WSL2/VM/cloud)
Learning Objectives¶
By the end of this tutorial, you will be able to:
- Apply the core ideas of “Linux Administration Automation” in a real ops script
- Use
set -euo pipefailas the production default - Use quoted expansions and clear stderr diagnostics
- Produce meaningful exit codes for automation consumers
- Debug behaviour with
bash -xwhen something fails - Relate this topic to day-to-day Linux admin and DevOps work
Architecture¶
Ops scripts sit between humans/automation and system tools. This topic’s control points are shown below.
Theory¶
What it is¶
Linux admin automation uses shell scripts to perform repeatable host operations: managing users, installing packages, controlling services, rotating logs, watching disk usage, and running backups. Instead of typing privileged commands by hand on each machine, you encode the desired checks and mutations so the same steps apply in the lab, on a bastion, or across a small fleet. The shell remains the practical interface to useradd, package managers, systemctl, and filesystem tools.
Why it matters¶
Human-driven administration does not scale and drifts between hosts. An idempotent script that creates a user only when missing, installs a pinned package non-interactively, and verifies a service is active becomes a building block for DevOps and platform workflows. Disk and backup automation protect availability: full volumes and untested backups are still among the most common outage causes. Encoding these tasks with clear exit codes lets Continuous Integration (CI), cron, or configuration management call them safely.
How it works¶
For users, check with id before useradd / usermod. Never embed passwords in scripts — prefer SSH keys or a secrets store. For packages, detect the family and call apt-get, dnf, or zypper non-interactively (DEBIAN_FRONTEND=noninteractive on Debian/Ubuntu). Pin versions when reproducibility matters.
Drive services through systemctl enable --now, systemctl is-active, and systemctl show rather than scraping unstable English status text. Configure logrotate for system logs; for application logs, compress and prune by age or size in a dedicated script that supports dry-run. Monitor space with df -h, df -i, and du -sh on critical paths — watch inodes as well as bytes. Back up with tar or rsync, keep retention and checksums, log start and end times, and exit non-zero on failure. Practise a restore dry-run path so backups are not theatre.
Key concepts¶
| Area | Practice |
|---|---|
| Users | Idempotent create/update; no passwords in git |
| Packages | Non-interactive installs; pin when needed |
| Services | systemctl status APIs over text scraping |
| Logs | logrotate or scripted compress/prune + dry-run |
| Disk | Threshold alerts on bytes and inodes |
| Backups | Retention, checksums, tested restore path |
Common pitfalls¶
- Running
useraddblindly every night and failing on “already exists” - Interactive package prompts that hang under cron
- Parsing
systemctl statusEnglish output that changes between versions - Alerting only on
df -hwhile inodes on a small/varare exhausted - Taking backups that have never been restored in a drill
Hands-on Lab¶
Create a workspace for this tutorial.
Focus: idempotent user check; disk report; mini backup with retention
Step 1 – Admin toolkit slice¶
cat > admin.sh << 'EOF'
#!/usr/bin/env bash
set -euo pipefail
echo "== disk =="; df -h . | tee disk.txt
echo "== inodes =="; df -i . | tee -a disk.txt
mkdir -p backup-src backup-out
echo data > backup-src/note.txt
ts=$(date +%Y%m%d%H%M%S)
tar -czf "backup-out/backup-$ts.tgz" -C backup-src .
ls -l backup-out
# idempotent user presence check (no create):
id -u "$(whoami)" >/dev/null
command -v systemctl >/dev/null && systemctl is-system-running --quiet || true
EOF
chmod +x admin.sh
./admin.sh
Final step – Cleanup note¶
Validation¶
- Lab commands run under
~/rebash-shell/lab12/ - You can explain each Theory heading in your own words
- Failure path exits non-zero and prints diagnostics to stderr (where applicable)
- You can relate this topic to a real DevOps or Linux admin task
Code Walkthrough¶
Production Bash for Linux Administration Automation always combines:
- A clear shebang (
#!/usr/bin/env bash) - Strict mode near the top (
set -euo pipefail) from Module 2 onward - Quoted expansions and explicit tests
- Functions with
localfor reusable behaviour - Documented exit codes and stderr logging
Keep scripts short enough to review in a single merge request. When logic grows (complex JSON APIs, heavy state), hand off to Python and keep Bash as the launcher.
Security Considerations¶
- Treat all external input (args, files, env) as untrusted until validated
- Never log secrets; prefer masked CI variables and secret stores
- Prefer least privilege — do not require root for file-local tasks
- Avoid
evaland unquoted expansions in destructive commands - Validate paths stay under an allow-listed root before
rmor overwrite
Common Mistakes¶
Skipping strict mode
Cron and CI hide failures that an interactive terminal would show. Fix: start with set -euo pipefail from Module 2 onward.
Unquoted path expansions
Spaces and globs rewrite your command line. Fix: always "$path" / "$@".
Assuming interactive PATH
Aliases and fancy PATH entries disappear under schedulers. Fix: set PATH or use absolute paths.
Best Practices¶
- One purpose per script; compose with functions or small binaries
- Log to stderr; reserve stdout for data or RESULT lines
- Idempotent behaviour where scheduling may overlap
- Pair every new script with a failing-path test you actually run
- Run ShellCheck in CI before merging automation
Troubleshooting¶
| Symptom | Likely cause | Fix |
|---|---|---|
| Works in terminal, fails in cron | PATH / cwd / env | Fingerprint env; set PATH |
unbound variable | set -u | Provide defaults or export vars |
| Pipeline “succeeds” incorrectly | Missing pipefail | set -o pipefail |
[[ unexpected operator | Running under sh/dash | Fix shebang to Bash |
Summary¶
Linux Administration Automation is a core skill for Linux admins and DevOps engineers automating real hosts and pipelines. Practise the lab until the failure path is as familiar as the happy path, then continue the track.
Interview Questions¶
- How does this topic show up in production Linux administration or CI?
- What failure mode appears if you ignore quoting or strict mode here?
- How would you test this behaviour under a minimal cron-like environment?
- When would you move this logic out of Bash into Python or another tool?
- What exit code contract would you document for teammates?
Sample answer — question 2
Unquoted expansions and missing pipefail create silent or partial failures — especially under cron — that look healthy in monitoring until data is wrong.
Related Tutorials¶
- Shell Scripting for DevOps Engineers – Category Overview
- Process Automation — Signals and Traps (previous)
- Networking Automation with Shell (next)
- Learning Paths