• Home
  • Disclaimer
  • Contact
  • Archives
  • About
  • Subscribe
  • Support
  • Advertise

Kernel Talks

Unix, Linux, & Cloud!

  • How-to guides
    • Howto
    • Disk management
    • Configurations
    • Troubleshooting
  • OS
    • HPUX
    • Linux
  • Miscellaneous
    • Software & Tools
    • Cloud Services
    • System services
    • Virtualization
  • Certification Preparations
    • AWS Certified Solutions Architect – Associate
    • AWS Certified Solutions Architect – Professional
    • AWS Certified SysOps Administrator – Associate
    • AWS Certified Cloud Practitioner
    • Certified Kubernetes Administrator
    • Hashicorp Certified Terraform Associate
    • Oracle Cloud Infrastructure Foundations 2020 – Associate
  • Tips & Tricks
  • Linux commands
You are here: Home / Tips & Tricks

How to change UID or GID safely in Linux

Published: April 28, 2018 | Modified: June 25, 2020



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.

⇠ Previous article
How to safely remove disk from LVM
Next article ⇢
Why ps output shows UID instead of username

Related stuff:

  • Run command on multiple linux servers from windows
  • 4 tools to download any file using the command line in Linux
  • Create beautiful ASCII text banners in Linux
  • GitBash not prompting for MFA in AWS CLI
  • How to get directory size in Linux
  • How to configure login banners in Linux (RedHat, Ubuntu, CentOS, Fedora)
  • Cowsay: Fun in Linux terminal
  • Format date and time for Linux shell script or variable
  • Hollywood movie MATRIX like desktop in Linux terminal
  • 8 ways to generate random password in Linux
  • How to save PuTTY session output automatically
  • 3 tricks to get multiple commands output in the same row

Filed Under: Tips & Tricks Tagged With: change gid without affecting file owenship, change uid without affecting file owenrships, How to change UID or GID safely in Linux, how to switch gid between two groups, how to switch uid between two users

If you like my tutorials and if they helped you in any way, then

  • Consider buying me a cup of coffee via paypal!
  • Subscribe to our newsletter here!
  • Like KernelTalks Facebook page.
  • Follow us on Twitter.
  • Add our RSS feed to your feed reader.

Comments

  1. Gert van den Berg says

    October 17, 2018 at 4:57 pm

    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 says

    December 13, 2019 at 7:58 pm

    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
    • Shrikant Lavhate says

      December 13, 2019 at 8:00 pm

      Thats true only for files under /home for that user. Anything outside home has no auto change impact of uid or gid.

      Reply
  3. Antolin says

    April 11, 2022 at 4:49 pm

    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

Share Your Comments & Feedback: Cancel reply

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

Get fresh content from KernelTalks

  • Email
  • Facebook
  • RSS
  • Twitter

Get Linux & Unix stuff right into your mailbox. Subscribe now!

* indicates required

This work is licensed under a CC-BY-NC license · Privacy Policy
© Copyright 2016-2023 KernelTalks · All Rights Reserved.
The content is copyrighted to Shrikant Lavhate & can not be reproduced either online or offline without prior permission.