Skip to content

Instantly share code, notes, and snippets.

@iam-TJ
Forked from jakesylvestre/gist:b3662db00c9fe3ab05a2
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iam-TJ/893716f37936fc411dbd to your computer and use it in GitHub Desktop.
Save iam-TJ/893716f37936fc411dbd to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script to add a user to Debian/Ubuntu system
if [ $(id -u) -ne 0 ]; then
echo "Only root may add a user to the system" >&2
exit 1
else
if [ $# -lt 2 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
else
username="$1"
password="$2"
fi
if /usr/bin/getent passwd "$username"; then
echo "$username exists" >&2
exit 2
else
if /usr/sbin/adduser --gecos '' --disabled-password "$username"; then
echo "User $username has been added to system"
salt=$(echo $RANDOM | /usr/bin/md5sum | /bin/dd bs=8 count=1 2>/dev/null)
if ! /usr/sbin/usermod --password $(/usr/bin/mkpasswd -m sha-512 "$password" "$salt") "$username"; then
echo "Failed to set password for user $username" >&2
fi
else
echo "Failed to add user $username" >&2
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment