Linux Tips

The way to change UID or GID safely in Linux

SEOClerks

Learn to change UID or GID safely in Linux. Additionally, know the right way to swap UID between two customers and GID between two teams with out impacting recordsdata possession they personal.

How to change UID or GID safely in LinuxThe way to change UID or GID safely in Linux

On this article, we are going to stroll you thru to alter UID or GID of present person or group with out affecting file possession owned by them. Later, we additionally defined the right way to swap GID between two teams and the right way to swap UID between two customers on the system with out affecting file possession owned by them.

Let’s begin with altering UID or GID on the system.

Present state of affairs :

Person shrikant with UID 1001
Group sysadmin with GID 2001

Anticipated state of affairs :

Person shrikant with UID 3001
Group sysadmin with GID 4001

Altering GID and UID is straightforward utilizing usermod or groupmod command, however it’s a must to take into account that after altering UID or GID it’s essential to change possession of all recordsdata owned by them manually since file possession is understood to the kernel by GID and UID, not by username.

The process will likely be –

Change UID or GID as beneath :

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

 

root@kerneltalks # usermod -u 3001 shrikant

root@kerneltalks # groupmod -g 4001 sysadmin

 

Now, search and alter all file’s possession owned by this person or group with for loop

root@kerneltalks # for i in `discover / -user 1001`; do chown 3001 $i; achieved
root@kerneltalks # for i in `discover / -group 2001`; do chgrp 4001 $i; achieved
OR
root@kerneltalks # discover / -user 1001 -exec chown -h shrikant ;
root@kerneltalks # discover / -group 2001 -exec chgrp -h sysadmin ;

 

root@kerneltalks # for i in `discover / -user 1001`; do chown 3001 $i; achieved

root@kerneltalks # for i in `discover / -group 2001`; do chgrp 4001 $i; achieved

OR

root@kerneltalks # discover / -user 1001 -exec chown -h shrikant ;

root@kerneltalks # discover / -group 2001 -exec chgrp -h sysadmin ;

 

That’s it. You’ve safely modified UID and GID in your system with out affecting any file possession owned by them!

The way to swap GID of two teams

Present state of affairs :

Group sysadmin with GID 1111
Group oracle with GID 2222

Anticipated state of affairs :

Group sysadmin with GID 2222
Group oracle with GID 1111

In above scenario, we have to use one intermediate GID which is at present not in use in your system. Verify /and many others/group file and choose one GID XXXX which isn’t current in a file. In our instance, we take 9999 as intermediate GID.

Now, the method is straightforward –

Change sysadmin GID to 9999Discover and alter group of all recordsdata owned by GID 1111 to sysadminChange oracle GID to 1111Discover and alter group of all recordsdata owned by GID 2222 to oracleChange sysadmin GID to 2222Discover and alter group of all recordsdata owned by GID 9999 to sysadmin

Listing of instructions for above steps are –

root@kerneltalks # groupmod -g 9999 sysadmin
root@kerneltalks # discover / -group 1111 -exec chgrp -h sysadmin ;
root@kerneltalks # groupmod -g 1111 oracle
root@kerneltalks # discover / -group 2222 -exec chgrp -h oracle ;
root@kerneltalks # groupmod -g 2222 sysadmin
root@kerneltalks # discover / -group 9999 -exec chgrp -h sysadmin ;

 

root@kerneltalks # groupmod -g 9999 sysadmin

root@kerneltalks # discover / -group 1111 -exec chgrp -h sysadmin ;

root@kerneltalks # groupmod -g 1111 oracle

root@kerneltalks # discover / -group 2222 -exec chgrp -h oracle ;

root@kerneltalks # groupmod -g 2222 sysadmin

root@kerneltalks # discover / -group 9999 -exec chgrp -h sysadmin ;

 

The way to swap UID of two customers

It may be achieved in the identical means we switched GID above through the use of intermediate UID.

 

Source link

Related Articles

Back to top button