Change Multiple UID and GID on UNIX using a script

I have a mixture of Unix systems on which UID and GID are inconsistent. I want to make them consistant.

I want a script which intakes new_UID and new_GID  files and compares it with the present existing users and groups in a system and changes them. If they are NOT present it should not...


Right now I am using the following to chage the UID and GID individually but its a pain..
usermod -u <new uid> username
find / -uid <old uid> -exec chown <new uid> {} \; -print
find / -user old gid -print | xargs -t chgrp new gid
jdenver247Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jdenver247Author Commented:
Like I want to pass new_uid.txt and new_gid.txt as parameters..something like

./changeUID-GID.sh new_uid.txt new_gid.txt
0
TintinCommented:
What is the format of new_uid.txt and new_gid.txt?
0
jdenver247Author Commented:
Here is the format

Inside the new UID.txt
PJEPPPI   16092
JGANGEN  63248
......
Inside the new GID.txt
UADMN  500
DBA  234
LDAP  212....and so on

The new UID and GID txt files have complete information. Some servers have fewer UID and GID ...Please assist tintin


0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

TintinCommented:

#!/bin/sh
if [ $# -ne 2 ]
then
   echo "Usage: `basename $0` [uid file] [gid file]"
   exit 1
fi
 
UIDS=$1
GIDS=$2
 
if [ ! -f $UIDS ]
then
   echo "UID file $UIDS not found"
   exit 1
fi
 
if [ ! -f $GIDS ]
then
   echo "GID file $GIDS not found"
   exit 1
fi
 
while read user uid
do
  olduid=`grep "^$user:" /etc/passwd | cut -f3 -d:`
  if [ -n "$olduid" ]
  then
     echo "Updating user $user"
     usermod -u $uid $user
     find / -uid $olduid -exec chown $uid {} \;
  else
     echo "$user does not exist on this server"
  fi
done <$UIDS
 
while read group gid
do
  oldgid=`grep "^$group:" /etc/group | cut -f3 -d:`
  if [ -n "$oldgid" ]
  then
     echo "Updating group $group"
     groupmod -g $gid $group
     find / -gid $oldgid -exec chgrp $gid {} \;
  fi
done <$GIDS

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jdenver247Author Commented:
Hello Tintin,
Dont we need to kill the process what the user owns while changing the UID and GID..???
Thanks
0
TintinCommented:
Well, it would certainly be recommended that you perform this action when the system either has no users logged on or is in single user mode.
0
jdenver247Author Commented:
Hello Tintin,

The script is handing, Here are litlle modifications I made to the script
655>./uidgid.sh uid.txt gid.txt
Updating user tesuser
...........Hangs

For an example I want to change the UID/GID for tesuser and tesgroup

665>id tesuser
uid=232(tesuser) gid=800 groups=1(staff),802(tesgroup)
root@aixser1:/home/jdenve
666>cat uid.txt
tesuser 253
root@aixserv1:/home/jdenve
667>cat gid.txt
tesgroup 803

Also if I use -uid option in your script it says its not valid...

#!/bin/sh
if [ $# -ne 2 ]
then
   echo "Usage: `basename $0` [uid file] [gid file]"
   exit 1
fi
 
UIDS=$1
GIDS=$2
 
if [ ! -f $UIDS ]
then
   echo "UID file $UIDS not found"
   exit 1
fi
 
if [ ! -f $GIDS ]
then
   echo "GID file $GIDS not found"
   exit 1
fi
 
while read user uid
do
  olduid=`grep "^$user:" /etc/passwd | cut -f3 -d:`
  if [ -n "$olduid" ]
  then
     echo "Updating user $user"
     usermod  -u $uid $user
     find / -user  $olduid -exec chown $uid {} \;
  else
     echo "$user does not exist on this server"
  fi
done <$UIDS
 
while read group gid
do
  oldgid=`grep "^$group:" /etc/group | cut -f3 -d:`
  if [ -n "$oldgid" ]
  then
     echo "Updating group $group"
     chgroup "id=$gid" $group
     find / -group $oldgid -exec chgrp $gid {} \;
  fi
done <$GIDS

Open in new window

0
TintinCommented:
When you say the script hangs, how long are you leaving it to run?  Remember that it may take a very long time for the find command to complete.  You may want to change it to /home instead.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.