2

I have been banging my head on this little project the past few days and here is how it goes...

I need to organize all the UIDs for multiple servers so all users have the same UID in their /etc/passwd. Obviously I am trying to find a proper script for this so I do not have to do this manually.

If I have 1 correct server with the correct UIDs could someone recommend a manageable script to sync other servers /etc/passwd with the correct one?

I got as far as orgizing it with using

awk -F ':' '{print$1,$3}' /etc/passwd  

Then I can use diff or sort to compare the updated passwd file with the old passwd file.

slhck
  • 235,242

1 Answers1

0

grawity has the right idea in his comment...if it had to be a script, it would need to be pretty complicated to work without a reboot...as in change all uids to something crazy high then to the right base number with usermod

it would be way easier in python...and i think that's included in most linux distros as standard now. if you need a python script that does the work, say so.


#!/usr/bin/env python
import subprocess, shlex


newlistolists = []
with open('/root/masterpasswd', 'r') as newetcpass:
    for line in newetcpass:
        alist = line.split(':')
        newlistolists.append(alist[:])

for entry in newlistolists:
    cmd = 'usermod -o -u ' + entry[2] + ' ' + entry[0]
    thecmd = shlex.split(cmd)
    subprocess.Popen(thecmd)

#insert additional logic for setting groups, shells, etc with relevant commands
#you need to do some error handling too, but it's a fast ugly UID set script

Only include usernames you want to change in /root/masterpasswd

RobotHumans
  • 5,934