Finding Duplicate UIDs

Someone presented me with a problem where multiple users’ home directories were owned by the wrong accounts. The issue was that two fileservers had been combined without regard to duplicate user IDs. The script below is a quick python implementation to print out duplicate IDs in the /etc/passwd file.

#!/usr/bin/env python

from collections import defaultdict

# Initialize dictionary of user ids
uids = defaultdict(list)

# loop through password file, building dictionary of uid:[list of usernames]
with open("/etc/passwd") as passwd_file:
  for line in passwd_file: 
    line_array = line.split(":")

# loop though dictionary. 
# If duplicate usernames for uid found, print on standard out

for uid in uids:
  if len(uids[uid]) > 1:
    print ( uid + ": " + " ".join(uids[uid]))

comments powered by Disqus