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:

ADVERTISEMENTS

  1. Become superuser or get an equivalent role using sudo command/su command
  2. First, assign a new UID to user using the usermod command.
  3. Tutorial details
    DifficultyEasy (rss)
    Root privilegesYes
    RequirementsNone
    Time2m
  4. Second, assign a new GID to group using the groupmod command.
  5. Finally, use the chown and chgrp commands to change old UID and GID respectively. You can automate this with the help of find command.
  6. Verify that the group owner of the file has changed using the ls command

Change a USER and GROUP ID on Linux

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 is 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

How to Change a USER and GROUP ID on Linux For All Owned Files
Let us see some examples and commands to change a user and group ID in Linux:

Linux command to change UID and GID

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
Verify that you changed UID and GID for given users with the help of ls command:
# ls -l
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
# find / -user foo -ls
# find / -group sales -ls

Conclusion

This page explained how to change group and user ownership of a file in Linux operating systems using the command-line utilities such as chgrp and others. See man page of chgrp for more info here.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
8 comments… add one
  • Jalal Hajigholamali Oct 14, 2015 @ 5:15

    Hi,
    Very useful article…
    Thanks a lot..

  • Philippe Petrinko Oct 14, 2015 @ 6:01

    Nice article, thanks a lot Vivek.

    __Philippe

  • Alexander Oct 15, 2015 @ 5:39

    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 Oct 17, 2015 @ 6:54

    It’s useful article.. Thanks

  • sapbasis Apr 8, 2016 @ 10:55

    Thx. Very usefull.

  • Andr Verwijs Nov 13, 2016 @ 5:34

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

  • Imran Mar 30, 2017 @ 15:02

    Another way to change file ownership:

    Steps;
    1- find / -gid 1001

    2- for i in `find / -gid 1001`; do chgrp 4001 $i; done

  • Brian Sep 14, 2017 @ 0:08

    Thank you very much! It’s great!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.