Last active
August 2, 2016 11:41
-
-
Save Ham5ter/a76b5eb01d80e4511bff40be8492ce12 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# | |
# This script is used to Create a User Account with SUDO Privileges!!!, | |
# Display the Password and force the User to change that Password upon the first Login! | |
# | |
# Version: 1.0 | |
# Author: [email protected] | |
# Date: 02.08.2016 | |
main(){ | |
# Default values. These Values can be overwritten with Comandline Arguments | |
FORCE_PASSWORD_CHANGE=true | |
ADD_TO_SUDO_GROUP=true | |
# Parse arguments | |
parse_arguments $@ | |
# Add the User | |
if id -u "$1" >/dev/null 2>&1; then | |
echo "Error: The User \"$USERNAME\" already exists!" | |
exit 1 | |
else | |
adduser --disabled-password --gecos "" $USERNAME | |
fi | |
# Add the User to the SUDO Group | |
if [ "$ADD_TO_SUDO_GROUP" = true ] ; then | |
usermod -aG sudo $USERNAME | |
fi | |
# Set the Password | |
if [ "$USE_PASSWORD" = true ] ; then | |
PASSWORD_OF_THE_NEW_USER=$PROVIDED_PASSWORD | |
else | |
PASSWORD_OF_THE_NEW_USER=$(genpasswd) | |
fi | |
# Force Password Change on Login! | |
echo $USERNAME:$PASSWORD_OF_THE_NEW_USER | chpasswd | |
if [ "$FORCE_PASSWORD_CHANGE" = true ] ; then | |
chage -d 0 $1 | |
fi | |
# Display Informations | |
echo "" | |
echo "###" | |
echo "# The Password for the Account \"$USERNAME\" is: \"$PASSWORD_OF_THE_NEW_USER\"" | |
echo "###" | |
} | |
genpasswd() { | |
local l=$1 | |
[ "$l" == "" ] && l=16 | |
tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs | |
} | |
usage() { | |
cat 1>&2 <<EOF | |
Usage $(basename $0) USERNAME [OPTIONS] | |
This script is used to Create a User Account with SUDO Privileges. | |
-v|--verbose Make the Script verbose. | |
-h|--help Print this help message. | |
-p|--password <p> Set the Password of the new user to <p>. | |
-n|--no-reset Dont ask the user to change his Password. | |
-s|--no-sudo Dont add the user to the sudo Group. | |
EOF | |
} | |
parse_arguments(){ | |
if [ $# -eq 0 ]; then | |
echo "Error: Missing Argument" | |
usage | |
exit 1 | |
fi | |
PARAMS="" | |
while (( "$#" )); do | |
case "$1" in | |
-v|--verbose) | |
set -e | |
set -x | |
shift 1 | |
;; | |
-h|--help) | |
usage | |
exit 0 | |
;; | |
-n|--no-reset) | |
FORCE_PASSWORD_CHANGE=false | |
shift 1 | |
;; | |
-s|--no-sudo) | |
ADD_TO_SUDO_GROUP=false | |
shift 1 | |
;; | |
-p|--password) | |
PROVIDED_PASSWORD=$2 | |
USE_PASSWORD=true | |
shift 2 | |
;; | |
--) # end Argument parsing | |
shift | |
break | |
;; | |
-*|--*=) # unsupported Argument | |
echo "Error: Unsupported Argument '$1'" >&2 | |
usage | |
exit 1 | |
;; | |
*) # preserve positional Arguments | |
USERNAME="$PARAMS$1" | |
shift | |
;; | |
esac | |
done | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment