Problem
How do I add new users automatically within a script?
Solution
Use the following script as an example:
:
#
# ident "@(#) newuser"
# comment "@(#) Add a user"
#
# Debugging.
# set -xv# Initialize Variables.
COMMENT=$1
GROUP=$2
USER=$3
#
# Check Variables
#
if [ $# -lt 3 ]
then
echo "\nUsage : $0 COMMENT GROUP USERNAME"
echo "Example $0 'User Name[,Location][,Dept]' unix_group username"
echo "\n^GPress RETURN to continue ... "
read CONT
exit 1
fiecho "This will create user ${USER} in the group ${GROUP} with ${COMMENT} commen
ts"while true
do
echo "\nDo you wish to continue (Y/N/C to Change Comments) [Default=Y]?"
read CONT
if [ "${CONT}" = "" ]; then
CONT="Y"
fi
case "${CONT}" in
y | Y) break
;;
n | N) exit 0
;;
c | C) echo "Enter new Comments ? "
read COMMENT
while true
do
echo "Do you wish to continue (Y/N) [Default=y] ? "
read CONT
if [ "${CONT}" = "" ]; then
CONT="Y"
fi
case "${CONT}" in
y | Y) break
;;
n | N) echo "Enter new Comments ? "
read COMMENT
;;
esac
done
break
;;
esac
doneuseradd -m -d /home/"${USER}" -c "${COMMENT}" -g "${GROUP}" ${USER}
passwd -d ${USER}echo "User ${USER} created with no password. When the user logins for the first
time"
echo "the system will ask them for an initial password."
echo "The user's home directory is set to /home/${USER}.\n"NOTES: If you are using Advanced File & Print Services (AFPS) then include
the following commands into the script to add users for AFPS access
if your /usr/net/servers/lanman/lanman.ini file contains:[ lmxserver ]
createunixuser=nonet user ${USER} password /ADD
net user ${USER} /COMMENT:"${COMMENT}" /FULLNAME:"${USER}" /SCRIPTPATH:/home/${USER}
net group ${GROUP} ${USER} /ADDThis assumes that there already is a NT Domain Global Group called
${GROUP).To protect access to the user's $HOME directory add the following:
HOME_DIR="/home/${USER}"
chmod 777 ${HOME_DIR}
chown lmxadmin ${HOME_DIR}
chgrp DOS---- ${HOME_DIR}
net perms c:${HOME_DIR} /GRANT $USER:FullControl
net perms c:${HOME_DIR} /GRANT Administrator:FullControl
net perms c:${HOME_DIR} /GRANT Administrators:FullControl
net perms c:${HOME_DIR} /GRANT "Server Operators":FullControl
net perms c:${HOME_DIR} /CHANGE "Server Operators":FullControl
net perms c:${HOME_DIR} /REVOKE "CREATOR OWNER" Everyone Replicator "Account Operators" SYSTEM
chmod 700 ${HOME_DIR}
mapuname -a ${DOMAIN}:${USER} ${USER}This assumes that the variable DOMAIN contains the NT Domain name of
the AFPS Server. You can obtain this with the command:# /usr/net/servers/lanman/bin/getrole -drp See also
ta109984, "How can I automate the account creation and password assignment process on UnixWare 7?"
ta114483, "How can I automatically change passwords with OpenServer?"
ta109974, "How can user passwords be added or modified non-interactively?"
ta114521, "When I add users with either "scoadmin" or "useradd", how can I ensure that their .profiles are always the same regardless of which utility I choose?"
useradd(1M); passwd(1)