7

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. checker auto parts . 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 2fiI have to say sorry for the formatting that wordpress butchered. va abc . I do use good practice when scripting, just wordpress didn’t show that.. roof leak repair . haha.


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

About the Author

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

Comments (7)

Trackback URL | Comments RSS Feed

  1. [...] to follow up on my Blog posting of the creating users script, I give you my script for deleting users (disabling etc may come to follow).. So without further [...]

  2. Marcus Meissner says:

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

    Ciao, Marcus

  3. ben.kevan says:

    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

  4. BiilYBonnYU says:

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

  5. Most people think they can write, but you are a true writer. You have put together valuable information in a way that’s easy for the reader to read and digest.

  6. car says:

    This is a wonderfully written article. You must have done a lot of research in this area. You have made this topic easy to read, interesting and easy to understand.

  7. I just have to tell you that you have really outdone yourself with this quality article presentation. I am very fascinated with the points you make here and I am in complete agreement with you.

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.