Operating System - HP-UX
1834445 Members
1874 Online
110067 Solutions
New Discussion

Re: Need to change User IDs and GROUP IDs for many users.

 
SOLVED
Go to solution
John Wolfe_1
Advisor

Need to change User IDs and GROUP IDs for many users.

Hello Experts,

I have a situation where I need to change the User IDs and Group IDs for about 250 users. This came about as a result of a company merger. Many of the ID's overlap so that adds to the confusion. Right now, I change one user and then do a find changing all his files to the new User ID and Group ID. The problem is that the find takes about 40 minutes per user to complete. At this rate, I'll be doing this until I retire. Can anyone think of a better, faster way?

Thanks in advance,
John Wolfe
At least I have a job.
10 REPLIES 10
Sridhar Bhaskarla
Honored Contributor

Re: Need to change User IDs and GROUP IDs for many users.

Hi John,

I would suggest to run 'find' only once and collect the uid/gid information of all the files into one file. Then you can use that file to find out the files|directories owned by a user and simply work on them to change. For ex.,

find / -type f -o -type d |xargs ll -d > /somewhere/userlist

This will also help you to backout the change easily and you don't have to run find again and again for each user.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Steven E. Protter
Exalted Contributor

Re: Need to change User IDs and GROUP IDs for many users.

You should be able to do this:
ls -1 -r / | grep username > /tmp/file

while read -rr aa
do
chown newid:newgroup $aa
done < /tmp/file

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Need to change User IDs and GROUP IDs for many users.

This must be the 3rd or so request doing much the same thing in the last few days. There must be some strange "must change UID/GID" compulsion being broadcast. I will admit that in your case, it makes sense. The solution to your problem is the number of find passes. You should really do only one and let it build a file listing UID, GID, pathname, and mode. Mode really only needs to identify symbolic links because theyt are handled differently, if you are worried about them at all. You then should sort this output in uid,gid,filename order so that chown commands can be grouped together.

I would make two tables to do the translations with this format:
OLDUID NEWUID User Optional_Comments

The other table should be the same except is should list the GID's.
OLDGID NEWGID Group Optional_Comments

Actually, only the OLDUID and NEWUID columns need to be meaningful.

You should then be able to loop thru your sorted filelist and apply the changes. Because the filelist is sorted in UID/GID order, it makes sense to cache the last lookup so that you don't need to rescan your translation tables each time.

You also need to apply usermod based upon your UID translation table but that is very simple.

This should be done during a scheduled maintenance window because changing UID/GID's "on the fly" is going to wreak havoc upon any running users.

If this were me, I'd do it in Perl.

Final thought: Don't forget when you are writing these here scripts that some of them there filenames might have spaces in them. I ain't saying it's a good idea but it does not mean that they ain't out there just waiting to clobber some poor unsuspecting script.

Food for thought, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Need to change User IDs and GROUP IDs for many users.

Okay John, here's one (the easy) part of your task. It does a find and lists
uidgidpathnamefiletype
and optionally sorts the output.

With a little "cut 'n paste" this was about 5 minutes of Perl complete with a Usage() subroutine.

invoke it like this:
find.pl -s /home /dog /cat /platypus > /var/tmp/filelist
or find.pl -u for usage. If no directories are specified it starts at the CWD. -s says sort the output.

This should get you started and note the intentional field delimiters. Your code (awk,Perl) to do the uid/gid xlations should use to parse the data --- not simply whitespace because again, them pesky filenames just might have spaces that another OS is so fond of.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Need to change User IDs and GROUP IDs for many users.

Another thought:
In your script that does the UID/GID translations, I would also not change files with UID's below a certain value; e.g. <= 100. Well disciplined admins start normal UID's no lower than 101; the others are reserved for system stuff.

If it ain't broke, I can fix that.
John Wolfe_1
Advisor

Re: Need to change User IDs and GROUP IDs for many users.

Thanks everyone. Sri and Steven, I gave A. Clay the 10's because he added such a neat script. I've already run it and I am now thinking about a shell script to read the UID and GID tables he suggested and apply the changes.

A. Clay, you made a very good point about spaces in filenames. Frankly, I had not even considered that problem.

Thanks,
John Wolfe
At least I have a job.
A. Clay Stephenson
Acclaimed Contributor

Re: Need to change User IDs and GROUP IDs for many users.

Okay John, I spent a few minutes over lunch today throwing together a Perl script which should do the trick for you using the output of my previous script. I already had binary search routines and routines to load tables so this was rather easy. I did not do the usermod's in this pass; I'll leave that up to you. This will change the UID's and GID's of all the files in the input list. If this were me I would comment out the actual chmod's and the system call to do the chmod -h's on the symbolic links to make sure everything is ok before doing the real run.

I used Perl's internal chmod for everything except symbolic links. Those are handled with a call to the chmod -h gid:uid command. I could have had you load a Perl module that include lchmod but this method requires no extra modules. I figure that you have so few symlinks which actually need translating that the overhead of the system() call will be negligible.

Use it like this assuming that your UID table is called "uids" and the GID translation table is called "gids".

fixuids.pl -U uids -G gids -e 100 -v < /var/tmp/filelist

Invoke as fixuids.pl -u for full usage.

This should be very close but use at your own risk.



If it ain't broke, I can fix that.
John Wolfe_1
Advisor

Re: Need to change User IDs and GROUP IDs for many users.

Wow Clay! Thanks! You really go the extra mile.

I've been looking over your script and I think I've found an error although perl -c fixuids.pl says that the syntax is ok.

$cc = do_1_file($s_in[0],$s_in[1],$s_in[2],
($s_in[3] eq SYMLINK));

I think you need an if statement to use "eq". You can't use it like ($s_in[3] eq SYMLINK) can you?

Please answer soon or any other Perl experts out there feel free to answer for A. Clay. I want to fixthe user id's this weekend.

Thanks,
John Wolfe
At least I have a job.
A. Clay Stephenson
Acclaimed Contributor

Re: Need to change User IDs and GROUP IDs for many users.

Sorry John, the syntax is correct and intentional.


($s_in[3] eq SYMLINK) evaluates to true (non-zero) or false (0). Given that SYMLINK is a constant for the string '12', we are testing $s_in[3] to see whether or not it is '12'. Octal 12 is the file type (actually mode = 0120000) that corresponds to symbolic links.
All we need to know is whether or not this file is a symbolic link. The expression is a rather standard method to evaluate an expression as a boolean.

If it ain't broke, I can fix that.
John Wolfe_1
Advisor

Re: Need to change User IDs and GROUP IDs for many users.

Thanks Clay. Now it makes perfect sense. I wish I could give you 100 points. When I compare my attempt at this to your Perl script it makes me feel like I'm still in the 1st grade.

John Wolfe
At least I have a job.