scripting
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.
Search
Powered by
Tags
10.3 11.0 11.1 apache bash beta bootchart codeweavers compiz drivers emerald emulation esx fat ass tomato man filesystems flash fonts gnome grub ibm kde kde4 kernel knoppix linux lotus notes lvm Microsoft nomad nvidia openoffice opensuse performance repositories review scripting security tech tutorial ubuntu user management VMWare windows wine workstationArchives
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- February 2008
- January 2008
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
Ben Kevan's Blog
- Connecting to Linux via RDP using NOMAD on openSUSE 11.1
- Red Hat (RHT) Profits Soar, is Novell (NOVL) Next?
- Dropping KDE 3.5 for openSUSE 11.2 isn’t really a bad thing
- 5 Days on openSUSE 11.1 was an early X-Mas present
- Unexpected Downtime
- Amarok 2.0 Final Released - Shipping with openSUSE 11.1
- PCLinuxOS 2009 Beta 2 - Review, Thoughts and Screenshots
- Opera 10.0 Alpha 1 on openSUSE 11.1 - Review
- Ding Dong the Emeralds Back Compiz
- 11.1 Build Service Repositories being Built