≡ Menu

How to Change a USER and GROUP ID on Linux For All Owned Files

I would like to know how to change a UID (USER ID)/GID (GROUP ID) and all belonging files on Linux operating system. Say, I want to change UID from 1005 to 2005 and GID from 1005 to 2005 on Linux. How do I make such change for belonging files and directories?

The procedure is pretty simple:

  1. First, assign a new UID to user using the usermod command.
  2. Second, assign a new GID to group using the groupmod command.
  3. Finally, use the chown and chgrp commands to change old UID and GID respectively. You can automate this with the help of find command.

It cannot be stressed enough how important it is to make a backup of your system before you do this. Make a backup. Let us say:
Our sample user name: foo

  1. Foo’s old UID: 1005
  2. Foo’s new UID: 2005
  3. Our sample group name: foo
  4. Foo’s old GID: 2000
  5. Foo’s new GID: 3000

Commands

To assign a new UID to user called foo, enter:
# usermod -u 2005 foo
To assign a new GID to group called foo, enter:
# groupmod -g 3000 foo
Please note that all files which are located in the user’s home directory will have the file UID changed automatically as soon as you type above two command. However, files outside user’s home directory need to be changed manually. To manually change files with old GID and UID respectively, enter:
# find / -group 2000 -exec chgrp -h foo {} \;
# find / -user 1005 -exec chown -h foo {} \;

The -exec command executes chgrp or chmod command on each file. The -h option passed to the chgrp/chmod command affect each symbolic link instead of any referenced file. Use the following command to verify the same:
# ls -l /home/foo/
# id -u foo
# id -g foo
# grep foo /etc/passwd
# grep foo /etc/group

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:



{ 6 comments… add one }
  • Jalal Hajigholamali October 14, 2015, 5:15 am

    Hi,
    Very useful article…
    Thanks a lot..

  • Philippe Petrinko October 14, 2015, 6:01 am

    Nice article, thanks a lot Vivek.

    __Philippe

  • Alexander October 15, 2015, 5:39 am

    Terrible runtime performance there. Better to have find invoke the exec command as seldom as possible. Ie., always use

    find… -exec blah {} +

    And *NOT* this (like it is shown on the blog post):

    find… -exec blah {} \;

  • tausif ahmad October 17, 2015, 6:54 am

    It’s useful article.. Thanks

  • sapbasis April 8, 2016, 10:55 am

    Thx. Very usefull.

  • André Verwijs November 13, 2016, 5:34 am

    is it a security risk to set UID an GID..??

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: