Back to Resources
Level
Script
Security
This script addresses the need for a straightforward way to identify which Linux user accounts have elevated privileges through the sudo group while confirming that those accounts are neither locked nor expired. This helps ensure only valid and active users maintain admin-level access, enhancing security oversight.
This script checks each user in the sudo group to confirm whether the account is still valid and active, filtering out any locked or expired profiles. By returning a concise list of legitimate local admins, it removes the guesswork in managing privileged access on Linux systems. Administrators can use this data to maintain a secure environment, responding quickly to any unauthorized privilege assignments or stale accounts that should be removed.
#!/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-get-local-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
detectedAdmins=$(IFS=','; echo "${active_admins[*]}")
# Output active admins for verification
echo "$detectedAdmins"
Linux - Get Local Admins
This Bash script identifies active administrators on a Linux system by examining the sudo group membership and performing comprehensive account verification. It processes each potential administrator account by checking for expiration dates and locked status, filtering out any disabled or expired accounts, and ultimately outputs a comma-separated list of active administrator usernames.
Bash
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.