Skip to content

Instantly share code, notes, and snippets.

@liuyu121
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuyu121/9c49cfdd7b1e9696f583 to your computer and use it in GitHub Desktop.
Save liuyu121/9c49cfdd7b1e9696f583 to your computer and use it in GitHub Desktop.
linux create user
#!/bin/bash
# @see http://www.cyberciti.biz/tips/howto-write-shell-script-to-add-user.html
# Script to add a user to Linux system
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment