Skip to content

Instantly share code, notes, and snippets.

@thimbl
Created March 19, 2011 00:43
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save thimbl/877090 to your computer and use it in GitHub Desktop.
Save thimbl/877090 to your computer and use it in GitHub Desktop.
shell script to create user accounts
#!/bin/bash
ROOT_UID=0
SUCCESS=0
E_USEREXISTS=70
# Run as root, of course. (this might not be necessary, because we have to run the script somehow with root anyway)
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script."
exit $E_NOTROOT
fi
#test, if both argument are there
if [ $# -eq 2 ]; then
username=$1
pass=$2
# Check if user already exists.
grep -q "$username" /etc/passwd
if [ $? -eq $SUCCESS ]
then
echo "User $username does already exist."
echo "please chose another username."
exit $E_USEREXISTS
fi
useradd -p `mkpasswd "$pass"` -d /home/"$username" -m -g users -s /bin/bash "$username"
echo "the account is setup"
else
echo " this programm needs 2 arguments you have given $# "
echo " you have to call the script $0 username and the pass "
fi
exit 0
@andevani
Copy link

This will work also.

sudo adduser myuser --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password
echo "myuser:password" | sudo chpasswd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment