[luau] adding new users

Keith krjw at optonline.net
Wed Sep 17 17:49:00 PDT 2003


* Nakashima <pnakashi at k12.hi.us> [17/09/2003 2257EDT]:
> The below script is intended to add a new user to our LTSP system.
> Most of it works, but I can't get useradd -g -G to work.
> 
> Even if I try to add the user to his own primary group and also to his
> class group, when I look at the results, the user is not in those
> groups.
> 
> Is there an error in my script? Maybe where I set the GROUPS variable?
> 
> As a work-around, is there a way to assign users to groups in a
> separate command?

Greetings.  A couple of comments --

First, if /bin/bash is available, use it.  It is much more powerful than
traditional Bourne shell.  Even if /bin/sh is a symlink to /bin/bash and
you execute /bin/sh it will try to mimic "historical versions of sh".
This is explained in the bash man page in the INVOCATION section.

Secondly, as a point of shell programming style, use lowercase variable
names.  Uppercase names are typically used for environment variables
(the ones you see when you type 'env' or 'set' at the bash shell
prompt).  In the case of your script, you are reading in $GROUPS which
is used by bash in the environment and is subsequently overwritten in
the context of your script.

Thirdly, I must ask what distro your LTSP clients are based off of.  If
it is RH (and I assume it is because RH added the -f flag to groupadd;
-f is NOT a standard groupadd flag) then you must be alerted to the fact
that RH's implementation of 'useradd' will create a new group ID (gid)
with the same name as the new user to be added and will make this new
gid the primary group ID of the new user *by default*.  Again, this is a
non-standard behavior (RH likes these things).  So, if you add a user
'joe' with useradd, 'joe' will have a new group called 'joe' created and
the user 'joe' will have a primary group of 'joe'.  What this means for
you is that you do NOT need to call groupadd prior to or after useradd
if you plan on putting each user in his/her own primary group with the
same name as his/her username.  It will be done for you automagically by
RH's useradd.  Your script could be reduced to this:

#!/bin/bash
echo "User name in lowercase please:"
read username
echo "Comma separated list of SUPPLEMENTAL groups (NOT primary group):"
read groups
useradd -G"$groups" -m "$username"
# and so on...

Also note that if some user 'foo' has a primary group ID of 'bar', this
is not reflected in /etc/group, i.e., you will not see user 'foo' on
the line for group 'bar' in /etc/group.  Primary group IDs are stored
for each user in /etc/passwd and are the 4th field (the 3rd field is the
user ID).

HTH,
krjw.
-- 
Keith R. John Warno                  [k r j w  at  optonline dot net]
Distinctive, adj.:
        A different color or shape than your competitors.



More information about the LUAU mailing list