How to change UID or GID safely in Linux

Learn how to change UID or GID safely in Linux. Also, know how to switch UID between two users and GID between two groups without impacting files ownership they own.

How to change UID or GID safely in Linux

In this article, we will walk you through to change UID or GID of existing users or groups without affecting file ownership owned by them. Later, we also explained how to switch GID between two groups and how to switch UID between two users on the system without affecting file ownership owned by them.

Let’s start with changing UID or GID on the system.

Current scenario :

User shrikant with UID 1001
Group sysadmin with GID 2001

Expected scenario :

User shrikant with UID 3001
Group sysadmin with GID 4001

Changing GID and UID is simple using usermod or groupmod command, but you have to keep in mind that after changing UID or GID you need to change ownership of all files owned by them manually since file ownership is known to the kernel by GID and UID, not by username.

The procedure will be –

Change UID or GID as below :

root@kerneltalks # usermod -u 3001 shrikant
root@kerneltalks # groupmod -g 4001 sysadmin

Now, search and change all file’s ownership owned by this user or group with for loop

root@kerneltalks # for i in `find / -user 1001`; do chown 3001 $i; done
root@kerneltalks # for i in `find / -group 2001`; do chgrp 4001 $i; done
OR
root@kerneltalks # find / -user 1001 -exec chown -h shrikant {} \;
root@kerneltalks # find / -group 2001 -exec chgrp -h sysadmin {} \;

That’s it. You have safely changed UID and GID on your system without affecting any file ownership owned by them!

How to switch GID of two groups

Current scenario :

Group sysadmin with GID 1111
Group oracle with GID 2222

Expected scenario :

Group sysadmin with GID 2222
Group oracle with GID 1111

In the above situation, we need to use one intermediate GID which is currently not in use on your system. Check /etc/group file and select one GID XXXX which is not present in a file. In our example, we take 9999 as intermediate GID.

Now, the process is simple –

  1. Change sysadmin GID to 9999
  2. Find and change the group of all files owned by GID 1111 to sysadmin
  3. Change oracle GID to 1111
  4. Find and change the group of all files owned by GID 2222 to oracle
  5. Change sysadmin GID to 2222
  6. Find and change the group of all files owned by GID 9999 to sysadmin

List of commands for above steps are –

root@kerneltalks # groupmod -g 9999 sysadmin
root@kerneltalks # find / -group 1111 -exec chgrp -h sysadmin {} \;
root@kerneltalks # groupmod -g 1111 oracle
root@kerneltalks # find / -group 2222 -exec chgrp -h oracle {} \;
root@kerneltalks # groupmod -g 2222 sysadmin
root@kerneltalks # find / -group 9999 -exec chgrp -h sysadmin {} \;

How to switch UID of two users

It can be done in the same way we switched GID above by using intermediate UID.

4 thoughts on “How to change UID or GID safely in Linux

  1. Gert van den Berg

    Replacing “\;” with “+” in the finds would speed this up a LOT (by running chgrp with multiple parameters instead of starting it once per file)

    Reply
  2. DF

    I don’t think this is actually true. At least not on RHEL 7 — changing UID, owned files seem to be associated with new UID without needing to manually change them to new UID.

    Reply
  3. Antolin

    Saved my hide today 🙂 Needed to change the UID/GID for a specific user and broke some things and this command fixed it.

    Also thanks to Gert for the optimization since the other command was taking quite a while.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.