Tuesday, May 1, 2018

How to change UID or GID safely in Linux

https://kerneltalks.com/tips-tricks/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
How to change UID or GID safely in Linux

In this article, we will walk you through to change UID or GID of existing user or group 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 :
Now, search and change all file’s ownership owned by this user or group with for loop
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 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 group of all files owned by GID 1111 to sysadmin
  3. Change oracle GID to 1111
  4. Find and change group of all files owned by GID 2222 to oracle
  5. Change sysadmin GID to 2222
  6. Find and change group of all files owned by GID 9999 to sysadmin
List of commands for above steps are –

How to switch UID of two users

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

No comments:

Post a Comment