Skip to content

Instantly share code, notes, and snippets.

@drekka
Last active December 21, 2023 03:34
Show Gist options
  • Save drekka/52521707b130efae92995996befc5aa1 to your computer and use it in GitHub Desktop.
Save drekka/52521707b130efae92995996befc5aa1 to your computer and use it in GitHub Desktop.
Creates an hidden admin user
#!/bin/bash
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
echo "Warning: you are about to create a hidden admin user!"
read -p "New admin user ID: " user_id
read -p "New admin user Name: " user_name
read -s -p "New admin user password: " password_1
read -s -p "Confirm admin user password: " password_2
if [ "$password_1" != "$password_2" ]; then
echo "Error: Passwords do not match"
exit 1
fi
# Find the next available unique ID we can give the user.
echo "Looking for a free system ID for $user_id ..."
highest_id=$( dscl . -list /Users UniqueID | /usr/bin/awk '$2>m {m=$2} END { print m }' )
next_id=$(( highest_id+1 ))
echo "Next available system ID: $next_id"
# Create the account
dscl . create "/Users/$user_id"
dscl . create "/Users/$user_id" UserShell /bin/zsh
dscl . create "/Users/$user_id" RealName "$user_name"
dscl . create "/Users/$user_id" UniqueID "$next_id"
dscl . create "/Users/$user_id" PrimaryGroupID 20 # Staff group
dscl . passwd "/Users/$user_id" "$password_1"
# Set admin
dscl . append /Groups/admin GroupMembership "$user_id"
# hide the account
dscl . create "/Users/$user_id" IsHidden 1
# Redirect the home directory to a null.
dscl . create "/Users/$user_id" NFSHomeDirectory "/var/empty"
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment