[comp.unix.questions] changing gid and uids

emcguire@cadfx.ccad.uiowa.edu (Ed McGuire) (05/02/90)

From article <11615@unix.SRI.COM>, by ric@ace.sri.com (Richard Steinberger):
> I would like to change a set of uids and gids for users on one computer

I had to change a bunch of uids (only) recently.  If you change the uid
and gid in /etc/passwd, you must change the uid and gid of the users'
files too.  Here's a find(1) command that works for me.

The following symbols are assumed:
    $homedir   user's home directory path
    $olduid    old uid
    $newuid    new uid

    find $homedir -user $olduid -print -exec chown $newuid '{}' ';'

What might work to also change gids follows.

    $oldgid    old gid
    $newgid    new gid

    find $homedir -user $olduid -group $oldgid -print\
     -exec chown $newuid.$newgid '{}' ';'

The effect is to look for objects owned by the user, and change the uid
and gid to reflect the change in /etc/passwd.  Limiting the search to
the home directory tree dramatically improves performance.  However,
objects owned by the user in other places would not be fixed up.

Note that it is necessary to test the present uid and gid.  Otherwise,
objects linked into the target user's home directory but owned by some
other user would be mistakenly changed as well.  Note also that if you
have users belonging to several groups at once only the files which
belong to the user's initial group from /etc/passwd will be changed.
In this case you could look for the files with other gids on them
afterwards with this command.

    find $homedir -user $olduid -exec ls -dgl '{}' ';'

How to use this on your system depends on what UNIX you're using.
Find(1) is somewhat variable, though I believe that the primary
operators I used here are universal.  Ls(1) may or may not require the
-g to type the gids of files.  Also, some chown(1) editions can change
both the object's uid and gid; others can only change the uid and would
require you to run chgrp(1) separately to change the gid.  Finally, you
should be sure you know what your editions of find(1), chown(1) and
chgrp(1) do to symbolic links and to the linked objects.

One other caveat: I'm sure I haven't thought of nearly enough caveats :-)

peace.  -- Ed
---
peace.  -- Ed
"I've been indirected and abbreviated, lexical functed and symbol substituted."

doug@jhunix.HCF.JHU.EDU (Douglas W O'neal) (05/02/90)

In article <1397@ns-mx.uiowa.edu> emcguire@cadfx.ccad.uiowa.edu (Ed McGuire) writes:
>    find $homedir -user $olduid -group $oldgid -print\
>     -exec chown $newuid.$newgid '{}' ';'

You should also remember to change /var/spool/mail/username to be owned
by the new uid.gid.

-- 
Doug O'Neal, Distributed Systems Programmer, Johns Hopkins University
doug@jhuvms.bitnet, doug@jhuvms.hcf.jhu.edu, mimsy!aplcen!jhunix!doug 
Like many of the features of UNIX, UUCP appears theoretically 
unworkable... - DEC Professional, April 1990

rsalz@bbn.com (Rich Salz) (05/03/90)

This program, in volume18 of the comp.sources.unix archives, is invaluable
in this kind of thing:
    pwdiff		Tool to aide in merging password files
-- 
Please send comp.sources.unix-related mail to rsalz@uunet.uu.net.
Use a domain-based address or give alternate paths, or you may lose out.

larry@macom1.UUCP (Larry Taborek) (05/03/90)

From article <11615@unix.SRI.COM>, by ric@ace.sri.com (Richard Steinberger):
> I would like to change a set of uids and gids for users on one computer
> so they will be the same as they are for the same users on a second
> machine.  (Eventually yellow pages may be set up, but not for another
> few months).  My question is this:  If I just edit /etc/passwd, and
> /etc/group, making the appropriate changes, do I need do anything else
> (i.e., do I have to explicitly manipulate all affected users' files
> to reflect this new state of affairs?)?  Thanks for any responses.
> 
> regards,
> 
> 	ric steinberger
> 	ric@ace.sri.com
Yup,

Files are actually stamped in their inode with the users uid
number, not their name.  When you do a 'ls' command, ls opens the
passwd file and reads it and transposes the owner and group
numbers with the appropriate names, thus appearing as if the
files were stamped with the users name and group name.  Simply
chaning the uid in the passwd file will not do ANYTHING to change
the uids of the files in the file systems that are already
stamped with the origional uids.

To properly change the passwd uid and file uids heres how I would
do it.

1).  Find all the files on the machine that belonged to user
uid '101' (or what ever uid is the target to change the uid
from), and change them to the new uid '102'.

find / -user 101 -exec chown 102 {} \;

Where 101 is the old user uid number and 102 is the new user id
number.  Make sure that 102 is a new unique uid and that no one
else has this number.  (If they do, then that owner will own
their own files and all these new files and you will have to
figure out how to split them up later).

2).  Delete the fred uid entry in the passwd file (which in our
example is 101) and change it to be 102.

Hope this helps...

Larry
-- 
Larry Taborek	..!uunet!grebyn!macom1!larry	Centel Federal Systems
		larry@macom1.UUCP		11400 Commerce Park Drive
						Reston, VA 22091-1506
My views do not reflect those of Centel		703-758-7000