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:

Adblock detected 😱

My website is made possible by displaying online advertisements to my visitors. I get it! Ads are annoying but they help keep this website running. It is hard to keep the site running and producing new content when so many people block ads. Please consider donating money to the nixCraft via PayPal/Bitcoin, or become a supporter using Patreon.

First, assign a new UID to user using the usermod command.

Second, assign a new GID to group using the groupmod command.

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

Foo’s old UID: 1005

Foo’s new UID: 2005

Our sample group name: foo

Foo’s old GID: 2000

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 {} \;

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

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 everyone who reads nixCraft, who likes it, helps fund it, my future would be more secure. You can donate as little as $1 to support nixCraft: