Skip to content

Instantly share code, notes, and snippets.

@reikoNeko
Created March 27, 2018 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reikoNeko/9b1e47117f56278027425e69cc0dafbb to your computer and use it in GitHub Desktop.
Save reikoNeko/9b1e47117f56278027425e69cc0dafbb to your computer and use it in GitHub Desktop.
Renumber a batch of users' uids and gids, and fix file ownerships
#!/bin/bash
while IFS="," read -r name olduid oldgid newuid newgid
do
/usr/sbin/groupmod $name -g $newgid
/usr/sbin/usermod $name -u $newuid -g $newgid
/bin/chown --changes --silent --no-dereference --recursive \
--from=:$oldgid :$name /home/$name
done << END
tinker,8001,8001,701,701
evans,8002,8005,702,702
chance,8003,8006,703,703
END
@reikoNeko
Copy link
Author

This is not for random fun (at least not on a production machine). This is for when you need to put a legacy machine onto LDAP, AD, or another system that has been assigned a block of UIDs/GIDs and you have existing users and groups in that range. Do research to futureproof your decisions, and then it helps to make a spreadsheet and export the values as CSV to be added into the data section.

@reikoNeko
Copy link
Author

This works in Centos 6, where usermod automatically changes file permissions but groupmod does not. YMMV

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment