Link to home
Start Free TrialLog in
Avatar of jdenver247
jdenver247

asked on

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
Avatar of jdenver247
jdenver247

ASKER

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
Avatar of Tintin
What is the format of new_uid.txt and new_gid.txt?
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


ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello Tintin,
Dont we need to kill the process what the user owns while changing the UID and GID..???
Thanks
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.
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

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.