Technical article 116462

How do I add new users from within a script?

OpenServer 5.0.5OpenServer 5.0.6OpenServer 5.0.7UnixWare 7.1.0UnixWare 7.1.1UnixWare 7.1.3UnixWare 7.1.4Open UNIX 8

Written · Last revised

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
fi
echo "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
done
useradd -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=no
net user ${USER} password /ADD
net user ${USER} /COMMENT:"${COMMENT}" /FULLNAME:"${USER}" /SCRIPTPATH:/home/${USER}
net group ${GROUP} ${USER} /ADD
This 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)

From the Xinuos technical article database, record 116462. Reproduced with permission. The original is at www.sco.com/ta/116462, which is reachable but blocked from search engines.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.