Problem overview
This script addresses the need for clear visibility over Linux privilege assignments by instantly comparing the list of detected admins against an official roster of authorized administrators, ensuring that any unauthorized accounts stand out and can be promptly addressed.
#!/bin/bash
# This resource is provided as a convenience for Level users. We cannot
# guarantee it will work in all environments. Please test before deploying
# to your production environment. We welcome contributions to our community
# library
# Level Library
# https://level.io/library/script-linux-unauthorized-admins
#!/bin/bash
# Define authorized admins
AUTHORIZED_ADMINS="{{cf_authorized_admins}}"
# Define detected admins
# Get all users in the 'sudo' group (local admins)
admins=$(getent group sudo | cut -d: -f4 | tr ',' '\n')
active_admins=()
for admin in $admins; do
if [[ -n "$admin" ]]; then
# Check if the account is expired
exp_date=$(sudo chage -l "$admin" | grep "Account expires" | awk -F': ' '{print $2}')
if [[ "$exp_date" == "never" || "$exp_date" == "" ]]; then
# Check if the account is locked
status=$(sudo passwd -S "$admin" | awk '{print $2}')
if [[ "$status" != "L" ]]; then
active_admins+=("$admin")
fi
fi
fi
done
# Convert array to comma-separated string
DETECTED_ADMINS=$(IFS=','; echo "${active_admins[*]}")
# Convert lists to arrays (lowercase for case-insensitive comparison)
IFS=',' read -r -a detectedArray <<< "$(echo "$DETECTED_ADMINS" | tr '[:upper:]' '[:lower:]' | sed 's/, */,/g')"
IFS=',' read -r -a authorizedArray <<< "$(echo "$AUTHORIZED_ADMINS" | tr '[:upper:]' '[:lower:]' | sed 's/, */,/g')"
# Find unauthorized admins
unauthorizedAdmins=()
for detected in "${detectedArray[@]}"; do
found=false
for authorized in "${authorizedArray[@]}"; do
if [[ "$detected" == "$authorized" ]]; then
found=true
break
fi
done
if [[ "$found" == false ]]; then
unauthorizedAdmins+=("$detected")
fi
done
# Join unauthorized admins into a comma-separated string
unauthorizedString=$(IFS=,; echo "${unauthorizedAdmins[*]}")
# Output unauthorized admins or success message
if [[ ${#unauthorizedAdmins[@]} -gt 0 ]]; then
echo "$unauthorizedString"
exit 1
else
echo "No unauthorized admins detected."
exit 0
fi
It takes two comma-separated lists—one for currently detected admins (DetectedAdmins) and one for the authorized admin list (AuthorizedAdmins)—and converts them to case-insensitive arrays. It then identifies any usernames present in the detected list that do not match the authorized list, flags them as unauthorized, and exits with a non-zero code to trigger alerts or notifications in Level. Because it runs with root-level permissions, there’s no need for manual elevation, enabling effortless integration into your day-to-day security and compliance routines.
Use cases
- Monitoring newly added Linux administrators that lack official authorization
- Performing regular compliance audits of Linux privileged users
- Enforcing least-privilege policies by detecting any unexpected admin accounts
- Consolidating results into larger admin compliance workflows or reporting structures
Recommendations
- Pair this script with a script-based monitor in Level to immediately alert on unauthorized admin additions
- For scheduled checks, build a Level automation with a time-based trigger to run frequent compliance scans
- Always update your custom “AuthorizedAdmins” field in Level to ensure accurate comparisons
- Test the script in a non-production environment to verify it functions correctly with your specific distribution