changing gid and uids

Ed McGuire emcguire at cadfx.ccad.uiowa.edu
Wed May 2 18:35:11 AEST 1990


>From article <11615 at unix.SRI.COM>, by ric at 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."



More information about the Comp.unix.questions mailing list