Script for adding new users

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 paths
PATH=$PATH:/usr/local/bin:/usr/bin:/usr/sbin:/sbin
PGM=`basename $0`

if [ $(id -u) -eq 0 ]; then

# Logging information
DATESTAMP=`date +%Y%m%d`
TIMESTAMP=`date +%H%M%S`
LOGDIR=/var/log/$PGM

# Find out who I am
ME=`whoami`

# Gather argument information
while [ $# -ge 1 ] ; do
case $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 ;;
esac
shift
done

# If no account name on command line, get one
if [ "$ACCT" = "" ] ; then
while [ "$ACCT" = "" ] ; do
echo -n “What is the username? ”
read ACCT
done
fi

# If no uid on command line, get one
if [ "$UUID" = "" ] ; then
while [ "$UUID" = "" ] ; do
echo -n “You need to provide a UID? ”
read UUID
done
fi

# If no comment on command line, get one
if [ "$COMNT" = "" ] ; then
while [ "$COMNT" = "" ] ; do
echo -n “You need to provide comments (ie Full Name)? ”
read COMNT
done
fi

# If no group on command line, assume “users”
if [ "$GROUP" = "" ] ; then
GROUP=users
fi
GID=`grep ^$GROUP: /etc/group | awk -F: ‘{print $3}’`
test “$GID” = “” && die “No group named $GROUP”

# If no home directory on command line, assume /home/$ACCT
if [ "$HDIR" = "" ] ; then
HDIR=/home/$ACCT
fi

# If no shell on command line, assume /bin/bash
if [ "$USHELL" = "" ] ; then
USHELL=/bin/bash
fi

echo “This is what is to be added – ok? (^C if not)”
echo “$ACCT::$UUID:$GID:$COMNT:$HDIR:$USHELL”
read ans

# insure log directory exists
test -d $LOGDIR || mkdir -p $LOGDIR
LOGFILE=$LOGDIR/$DATESTAMP

egrep -w “^$ACCT” /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo “$ACCT exists!”
exit 1
else
password=$ACCT
pass=$(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 do
echo “$TIMESTAMP-$ME-$ACCT::$UUID:$GID:$COMNT:$HDIR:$USHELL” >>$LOGFILE
fi
else
echo “Only root can run $PGM”
exit 2
fi

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.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

About ben.kevan

I am ben kevan.. Well yeah. .that's about it.

3 Comments

  • Marcus Meissner
    October 30, 2008 | Permalink |

    Please read about useradd, it has all the options you need already.

    Ciao, Marcus

  • ben.kevan
    October 31, 2008 | Permalink |

    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

  • November 11, 2008 | Permalink |

    Excellent blog! Interesting article and very informative! I will necessarily subscribe for this blog. http://onlineoneru.ru/map.html

One Trackback

Leave a comment

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Your email is never shared. Required fields are marked *