Skip to content

Instantly share code, notes, and snippets.

@chelnak
Created February 18, 2015 10:20
Show Gist options
  • Select an option

  • Save chelnak/2ec17eaf1a5c6b5f705c to your computer and use it in GitHub Desktop.

Select an option

Save chelnak/2ec17eaf1a5c6b5f705c to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment