I asked a question previously about recovering a RAID 1 partition where the UUID's of the member partitions had all been zeroed. Having received no response in that thread, I'm now forced to reinstall CentOS on the OS partition and rebuild the system.

One of the big tasks of the rebuild will be trying to recover the user database, for which there is no recent backup. My goal is simply to recreate the user database by using a script on a list of UID's and usernames to create everyone's account with the same password which they can then change on login.

/home was on a different and still extant raid array, so in that partition I have an exhaustive list of users, and by reading the UID of each user's directory, I also have each user's old UID. What I'm struggling with is a pair of scripts. The first will iterate /home and create a file with matched pairs of UID's and username, and then the second will use that output file to create all of those accounts on the new installation, so the UID's of the home directories will then translate to a current user. It's a samba file server, so during the second script I'll also create the matching samba username.

Does anyone know of a tool or script already written to do this task, so I can avoid recreating the wheel?


1 Answer 1


You could create a csv file from your home dirs containing "user:home:uid:gid" like this:

find /home -mindepth 1 -maxdepth 1 -type d -not -name "lost+found*" \
  -printf "%f:%p:%U:%G\n" >/tmp/user.txt

Then re-create the users with like this

cat /tmp/user.txt | while IFS=: read -r user home uid gid; do \
  echo useradd -d "$home" -u "$uid" -g "$gid" "$user"

Remove the echo if output looks correct. You should be able to adopt this useradd command for your needs and to add the right samba command to the loop too.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .