?
Solved

Change Multiple UID and GID on UNIX using a script

Posted on 2008-11-13
8
Medium Priority
?
1,414 Views
Last Modified: 2013-12-21
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
0
Comment
Question by:jdenver247
  • 4
  • 4
8 Comments
 

Author Comment

by:jdenver247
ID: 22954264
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
 
LVL 48

Expert Comment

by:Tintin
ID: 22954608
What is the format of new_uid.txt and new_gid.txt?
0
 

Author Comment

by:jdenver247
ID: 22954816
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Accepted Solution

by:
Tintin earned 2000 total points
ID: 22955243

#!/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
 

Author Comment

by:jdenver247
ID: 22955805
Hello Tintin,
Dont we need to kill the process what the user owns while changing the UID and GID..???
Thanks
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22955941
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
 

Author Comment

by:jdenver247
ID: 22961286
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
 
LVL 48

Expert Comment

by:Tintin
ID: 22966137
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question