Skip to content

Instantly share code, notes, and snippets.

@jkristoffer
Last active October 4, 2017 04:10
Show Gist options
  • Save jkristoffer/444e2d1d928647034ef328e345c0e274 to your computer and use it in GitHub Desktop.
Save jkristoffer/444e2d1d928647034ef328e345c0e274 to your computer and use it in GitHub Desktop.
Linux Create User Script
#!/bin/bash
# Script to add a user to Linux system
# -------------------------------------------------------------------------
# Copyright (c) 2007 nixCraft project <http://bash.cyberciti.biz/>
# This script is licensed under GNU GPL version 2.0 or above
# Comment/suggestion: <vivek at nixCraft DOT com>
# -------------------------------------------------------------------------
# See url for more info:
# http://www.cyberciti.biz/tips/howto-write-shell-script-to-add-user.html
# -------------------------------------------------------------------------
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