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?

Who is Participating?
ygouthamConnect With a Mentor Commented:
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
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.
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

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
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?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.