Fedora 16 instabilities (uid/gid change script)

Dr. Michael J. Chudobiak mjc at avtechpulse.com
Wed Jan 11 20:57:02 UTC 2012


On 01/11/2012 03:45 PM, Joe Zeff wrote:
> On 01/11/2012 12:39 PM, Dr. Michael J. Chudobiak wrote:
>> Yes it is, but that's easily fixed with a one-time script. I can send
>> you a sample perl script if you need one.
>
> As I've mentioned elsewhere, when I upgrade my machine I'll move to a 64
> bit system and deal with the UID issue. I have three users: myself and
> two friends who have ssh access for various reasons. If your script can
> handle multiple users without editing, please send me a copy for future
> use.

The perl script for for fixing uid/gid numbering is below.

First run:

script.pl --foundonly

and that will display a "dry run" of the bash commands it would execute. 
That will not actually write any changes to disk.

To do an actual run, do:

script.pl --foundonly --write

The --foundonly switch limits it to gids and uids actually found in your 
/etc/passwd and /etc/group files.

- Mike



#!/usr/bin/perl

use strict;
use Getopt::Long;

my $old_start  = 500;
my $max_old_id = 525;
my $new_start  = 1000;
my $write      = 0;
my $foundonly  = 0;

my $start_at = "/";

GetOptions(
     'write'     => \$write,
     'foundonly' => \$foundonly
);

my $delta = $new_start - $old_start;

my %fix_uids = ();
my %fix_gids = ();

print "\n-- update user database ----\n\n";

# get user info
while ( ( my $name, my $pass, my $uid ) = getpwent() ) {
     if ( $uid >= $old_start && $uid < $new_start ) {
         $fix_uids{$uid} = 1;
         my $new_uid = $uid + $delta;
         my $cmd     = "/usr/sbin/usermod --uid $new_uid -o $name";
         &do_command($cmd);
     }

     if ( ( $uid >= $new_start ) && $uid < ( $new_start + $delta ) ) {
         $fix_uids{ $uid - $delta } = 1;
     }
}

print "\n-- update group database ---\n\n";

# get group info
while ( ( my $name, my $passwd, my $gid ) = getgrent() ) {
     if ( $gid >= $old_start && $gid < $new_start ) {
         $fix_gids{$gid} = 1;
         my $new_gid = $gid + $delta;
         my $cmd     = "/usr/sbin/groupmod --gid $new_gid -o $name";
         &do_command($cmd);
     }

     if ( ( $gid >= $new_start ) && $gid < ( $new_start + $delta ) ) {
         $fix_gids{ $gid - $delta } = 1;
     }
}

if ( !$foundonly ) {
     foreach my $num ( $old_start .. $max_old_id ) {
         $fix_uids{$num} = 1;
         $fix_gids{$num} = 1;
     }
}

print "\n-- update file uid info ----\n\n";

foreach my $orig_uid ( sort keys %fix_uids ) {
     my $new_uid = $orig_uid + $delta;
     &do_command( "/bin/find $start_at -uid "
           . $orig_uid
           . " -print0 | xargs -0 chown "
           . $new_uid );
}

print "\n-- update file gid info ----\n\n";

foreach my $orig_gid ( sort keys %fix_gids ) {
     my $new_gid = $orig_gid + $delta;
     &do_command( "/bin/find $start_at -gid "
           . $orig_gid
           . " -print0 | xargs -0 chgrp "
           . $new_gid );
}

print "\n-- update NIS database -----\n\n";

&do_command("cd /var/yp/; make");

print "\n-- done --------------------\n\n";

if ( !$write ) {
     print "NO CHANGES were actually made.\n";
     print "To actually execute the above commands, run:\n";
     print "$0 --write\n\n";
}

sub do_command {
     my $arg = $_[0];
     print $arg . "\n";

     if ($write) {
         print `$arg` . "\n";
     }
}


More information about the users mailing list