Changing a user's UID/GID

Need to update a user’s UID/GID for whatever reason? Sometimes it’s just something you need to do when conforming with a new management scheme or when you’re moving to a new LDAP server, for example. This doesn’t have to be as difficult an undertaking as you might think.

Regardless of why you need to do this, we can get it done in two simple steps from the console. First off, make sure you’re logged in as root.

Update user UID/GID

We’ll do this in two steps so follow one or both, depending on what you need to accomplish:

# change UID
usermod -u <newUID> user

# change GID
usermod -g <newGID> user

This will update the ownership information on all files in the user’s home directory automatically. Pretty slick, right? But, what about files the user owns elsewhere on the system?

Update other file ownership information

If the user in question only has files in his/her home directory, then you can skip this step entirely. Otherwise, we have two commands to do the heavy lifting for us. In this example, I’m recursing the entire drive starting at the root directory. For large file systems, this might take some time. If you know the only files that need updating are in a particular directory, you can substitute that directory for ‘/‘ in the example to speed things up. I guess I should mention that this command is recursive, just so you are aware.

# update GID for files owned by user
find / -group <oldGID> -exec chgrp -h <username> {} \;

# update UID for files owned by user
find / -user <oldUID> -exec chown -h <username> {} \;

Note that I’ve updated the GID before the UID? I have found that following this order gets far more consistent results in all cases when I’ve had to make this type of change. Comment below if you have a different experience since I find this order counter-intuitive.

Final thoughts

Well, that wasn’t as painful as you thought, right? Just thought I’d make a quick note to help others out and help me remember how to do this too!

Thanks for reading my techie-thoughts on this issue. Have any comments or suggestions? Want to add your tips? Things you want me to cover in a future article? Comment below!