[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 20545
  • Last Modified:

Change uid and gid of user/group (linux)

I have a whole bunch of folders/files with the owner of dspam and the group of dspam

Currently, id dspam shows:
uid=104(dspam) gid=105(dspam) groups=105(dspam)

I need to set the UID and GID to something above 500.  How can I do so?

  • 3
  • 2
1 Solution
usermod -u 501 dspam
groupmod -g 501 dspam
normally all the groups are listed in /etc/groups and the users are listed in /etc/passwd

check both the files to see which number (in terms of gid and uid is available) above 501 and select one.  and use the above command to change it to that uid or gid.  simplest would be to delete the user and add it again if you are not sure about what to do.  any new user added to the system would always get added beyond the 500 range when done through the command line interface.
acrocatAuthor Commented:
Problem is, if I run the commands above, I then get:

drwxr-xr-x  2  104  105  4096 Oct  7 21:51 templates

instead of

drwxr-xr-x  2  dspam  dpsam  4096 Oct  7 21:51 templates
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

whenever you want to change the group of a particular directory and the files contained therein (with the subfolders inside the entire tree)

chown -R dspam:dspam /some/directory/templates

this will change it to dspam and set the group also to dspam
acrocatAuthor Commented:
Right - but I have files and folders all over the filesystem owned by dspam - is there a way to go through all of them in a reasonable way?
one way of doing it would be to edit the /etc/groups file and the /etc/passwd file itself to change the uid and gid of the dspam user.  if you have gui enabled then this can also be done through that.  else edit the files and change it.

the second option is to run a command and see if it is the proper list of files

find / -uid 104 | xargs ls -al >> /root/olddspamuid.txt

this will write a list of all files owned by dspam's old uid number into the file

find / -gid 105 | xargs ls -al >> /root/olddspamgid.txt

this will write a list of all files owned by dspam's old gid number into the file

if they are all correct entries then you can simply do a

find / -uid 104 | xargs chown dspam
find / -gid 105 | xargs chgrp dspam

the last two commands should be run only after verifying the contents of the two text files created.  do not act in haste as they are a bit delicate commands to be run without verifying and may also cause the system to hang if not properly verified as the / file system contains far too many number of files which should be owned by root

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now