/gist:893716f37936fc411dbd Secret
Last active
August 29, 2015 14:04
-
-
Save iam-TJ/893716f37936fc411dbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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