Interview Prep/Shell Scripting/Scenarios: deployments, user management and fleet operations

Onboard 30 engineers: create accounts and install their SSH keys from their GitHub usernames, idempotently.

IntermediateScenarioScenarios: deployments, user management and fleet operations
Answer

Situation read → GitHub publishes public keys at https://github.com/<user>.keys. The script must be safe to rerun.

What I'd do →

while IFS=, read -r user gh_user; do
  [[ $user =~ ^[a-z_][a-z0-9_-]{0,31}$ ]] || { echo "skip invalid $user" >&2; continue; }
  id -u "$user" &>/dev/null || useradd --create-home --shell /bin/bash --groups developers "$user"
  install -d -m 0700 -o "$user" -g "$user" "/home/$user/.ssh"
  keys=$(curl -fsS --max-time 10 "https://github.com/${gh_user}.keys") || { echo "no keys for $gh_user" >&2; continue; }
  [[ -n $keys ]] || continue
  tmp=$(mktemp); printf '%s\n' "$keys" > "$tmp"
  install -m 0600 -o "$user" -g "$user" "$tmp" "/home/$user/.ssh/authorized_keys"; rm -f "$tmp"
done < <(tail -n +2 engineers.csv)
  1. id -u check makes useradd idempotent; install sets owner and mode atomically.
  2. Validate usernames before using them in paths.

Why → Rerunnable scripts let you fix one bad row and rerun without side effects.

Interview tip → Mention that at scale this belongs in config management (Ansible user and authorized_key modules) or SSO with short-lived certificates.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: deployments, user management and fleet operations