chelnak
2/18/2015 - 10:20 AM

newuser.sh

newuser.sh


#!/bin/bash
#Create a new user based on script arguments
#Require password to be changed at first login
#Add user to sudo group
#usage ./newuser.sh user password

if [ $(id -u) -eq 0 ]; then
        if [ -n "$(id "${1}" 2>/dev/null)"  ];then
                echo "${1} exists"
                exit 1
        else
            	pass=$(perl -e 'print crypt($ARGV[0], "password")' "${2}")
                useradd -m -p $pass "${1}" -G "sudo" -s "/bin/bash" &>/dev/null

                if [ $? -eq 0 ]; then
                        echo "User has been added to the system"

                        echo "Set change password on next logon"

                        chage -d 0 "${1}"

                        exit 0

                else
                    	echo "Failed to add user to the system"
                        exit 1
                fi
        fi

else
    	echo "Must be root"
        exit 1
fi