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

A contractor needs sudo on one server for two hours. How do you grant it so it expires automatically?

AdvancedScenarioScenarios: deployments, user management and fleet operations
Answer

Situation read → Manual revocation gets forgotten. Make expiry part of the grant.

What I'd do →

  1. Write a validated sudoers drop-in:
    f=/etc/sudoers.d/90-tmp-contractor
    tmp=$(mktemp)
    echo 'contractor ALL=(ALL) /usr/bin/systemctl restart app, /usr/bin/journalctl' > "$tmp"
    visudo -cf "$tmp" && install -m 0440 "$tmp" "$f"; rm -f "$tmp"
    
  2. Schedule removal with a transient systemd timer: systemd-run --on-active=2h --unit=revoke-contractor rm -f "$f" (or at now + 2 hours).
  3. Scope commands narrowly; avoid ALL and anything that can spawn a shell (vi, less, find -exec).
  4. Log the grant and the revocation with logger.

Why → visudo -c prevents a syntax error from breaking sudo for everyone; timed removal prevents forgotten access.

Interview tip → Mention just-in-time access tools (Teleport, AWS SSM with IAM conditions) as the scalable version.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: deployments, user management and fleet operations