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

You are asked to audit a server for risky accounts. What does your script check?

IntermediateScenarioScenarios: deployments, user management and fleet operations
Answer

Situation read → Look for privilege, missing passwords, passwordless sudo and stray keys.

What I'd do →

awk -F: '$3 == 0 && $1 != "root" {print "UID 0 account:", $1}' /etc/passwd
awk -F: '$2 == "" {print "empty password:", $1}' /etc/shadow
grep -rhE '^[^#].*NOPASSWD' /etc/sudoers /etc/sudoers.d/ 2>/dev/null
awk -F: '$3 >= 1000 && $7 !~ /(nologin|false)$/ {print $1}' /etc/passwd | while read -r u; do
  f="/home/$u/.ssh/authorized_keys"; [[ -s $f ]] && echo "$u: $(grep -c . "$f") key(s)"
done
lastlog -b 90 | awk 'NR > 1 && $0 !~ /Never logged in/ {print "inactive 90d+:", $1}'

Why → These are the usual paths to unauthorized root: extra UID 0 accounts, blank passwords and unrestricted sudo.

Interview tip → Mention running audits as code (CIS benchmarks via Lynis or OpenSCAP) rather than only ad-hoc scripts.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: deployments, user management and fleet operations