Change uid and gid of user/group (linux)

Posted on 2007-10-07
Last Modified: 2013-12-16
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?

Question by:acrocat
    LVL 48

    Expert Comment

    usermod -u 501 dspam
    groupmod -g 501 dspam
    LVL 14

    Expert Comment

    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.

    Author Comment

    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
    LVL 14

    Expert Comment

    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

    Author Comment

    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?
    LVL 14

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
    rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now