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 1001Group sysadmin with GID 2001

Expected scenario :

User shrikant with UID 3001Group 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.

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 1111Group oracle with GID 2222

Expected scenario :

Group sysadmin with GID 2222Group 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.