A few people have asked about scripting new user installation for your needs so I thought I would share the script I have made and use to create users in my environment. In my environment I require the assigning of the UID (as I would like UID’s to be the same across all machines), comment (GELOC (I use it for putting in the full name of the user)) and obviously the username.
I have also written it to assign a default password that is set to expire and change the next time they log in.So without further ado:
#!/bin/bash# Script to add a user to Linux systems# Make sure we have the required pathsPATH=$PATH:/usr/local/bin:/usr/bin:/usr/sbin:/sbinPGM=`basename $0`if [ $(id -u) -eq 0 ]; then# Logging informationDATESTAMP=`date +%Y%m%d`TIMESTAMP=`date +%H%M%S`LOGDIR=/var/log/$PGM# Find out who I amME=`whoami`# Gather argument informationwhile [ $# -ge 1 ] ; docase $1 in-c*) COMNT=`echo $1 | sed -e 's/^-c//'` ;;-d*) HDIR=`echo $1 | sed -e 's/^-d//'` ;;-g*) GROUP=`echo $1 | sed -e 's/^-g//'` ;;-s*) USHELL=`echo $1 | sed -e 's/^-s//'` ;;-u*) UUID=`echo $1 | sed -e 's/^-u//'` ;;-h*)echo "Use: $PGM -uUID -gGROUP -cCOMMENTS [-d/path/to/homedir] [-sSHELL] account"exit;;-*) die "$PGM: unknown option "$1"" ;;*) ACCT=$1 ;;esacshiftdone# If no account name on command line, get oneif [ "$ACCT" = "" ] ; thenwhile [ "$ACCT" = "" ] ; doecho -n "What is the username? "read ACCTdonefi# If no uid on command line, get oneif [ "$UUID" = "" ] ; thenwhile [ "$UUID" = "" ] ; doecho -n "You need to provide a UID? "read UUIDdonefi# If no comment on command line, get oneif [ "$COMNT" = "" ] ; thenwhile [ "$COMNT" = "" ] ; doecho -n "You need to provide comments (ie Full Name)? "read COMNTdonefi# If no group on command line, assume "users"if [ "$GROUP" = "" ] ; thenGROUP=usersfiGID=`grep ^$GROUP: /etc/group | awk -F: '{print $3}'`test "$GID" = "" && die "No group named $GROUP"# If no home directory on command line, assume /home/$ACCTif [ "$HDIR" = "" ] ; thenHDIR=/home/$ACCTfi# If no shell on command line, assume /bin/bashif [ "$USHELL" = "" ] ; thenUSHELL=/bin/bashfiecho "This is what is to be added - ok? (^C if not)"echo "$ACCT::$UUID:$GID:$COMNT:$HDIR:$USHELL"read ans# insure log directory existstest -d $LOGDIR || mkdir -p $LOGDIRLOGFILE=$LOGDIR/$DATESTAMPegrep -w "^$ACCT" /etc/passwd >/dev/nullif [ $? -eq 0 ]; thenecho "$ACCT exists!"exit 1elsepassword=$ACCTpass=$(perl -e 'print crypt($ARGV[0], "password")' $password)useradd -u $UUID -g $GID -c "$COMNT" -d $HDIR -s $USHELL $ACCT -p $pass && chage -d 0 $ACCT[ $? -eq 0 ] && echo "$ACCT has been added to system! They will be required to change password on first login" || echo "Failed to add $ACCT!"# log what we doecho "$TIMESTAMP-$ME-$ACCT::$UUID:$GID:$COMNT:$HDIR:$USHELL" >>$LOGFILEfielseecho "Only root can run $PGM"exit 2fi
I have to say sorry for the formatting that wordpress butchered. I do use good practice when scripting, just wordpress didn’t show that haha.
Pingback: Script for deleting users (follow up for creating user) | Ben Kevan's Blog
Please read about useradd, it has all the options you need already.
Ciao, Marcus
useradd by default will not “require” a UID. Since this is in an enterprise setting I want to require the manual addition of a UID. Also, i’ll be adding some cases to port this over to AIX / HPUX etc.
This also assigns a default password for me.
Also since this is in an enterprise setting it’s used for SOX (a US Control) to log
Excellent blog! Interesting article and very informative! I will necessarily subscribe for this blog. http://onlineoneru.ru/map.html