Will there be always a pair of same UID and GID?

12,811

Solution 1

There is no guarantee that the groupname = username will exist.

The most common scenario is that system administrators use on Linux is creating a new user locally on the system is without an explicit specification for the group, which means that the group will be created by default same as the user name and assign the user to have the default GID to be of the newly created group.

This doesn't mean that another system administrator will follow this procedure and if you throw other naming services into the mix like NIS or LDAP the group assigned to be default will likely not be same as the new GID with groupname same as username.

So you shouldn't rely on the fact that username=groupname

Solution 2

Although the user private group idiom is often used in modern Linux systems, *nix does not actually require a unique primary group for each user, and it's quite possible for several user accounts to share a common primary group such as users or staff.

Even in cases where the UPG idiom is enforced, there is no particular reason that the numeric GID of a particular UPG should be equal to the owner's UID, although most account-creation tools will try to assign UIDs and GIDs in order, if they are available. The IDs can easily get out of step if the users and/or groups need to be made consistent across multiple systems (for example to accommodate pre-existing NFS shares).

Solution 3

As Ramesh has indicated by default the UID and the GID are equal for ordinary users. However this is not always the case so you must not make such an assumption as this may wreak havoc to your system. Assigning file/folder permissions to a wrong group is somewhat messy, isn't it?

So, the short answer: no, UID is not always equal to GID. Still, /etc/passwd contains both the UID and GID of the default group on the same line so it's rather easy to extract them.

Solution 4

From this link, I see the below information.

A user ID (UID) is a unique positive integer assigned by a Unix-like operating system to each user. Each user is identified to the system by its UID, and user names are generally used only as an interface for humans.

UIDs are stored, along with their corresponding user names and other user-specific information, in the /etc/passwd file, which can be read with the cat command as follows:

cat /etc/passwd

The third field contains the UID, and the fourth field contains the group ID (GID), which by default is equal to the UID for all ordinary users.

EDIT

However, as @Karlson points out it is not true that the UID will be equal to GID by default.

The statement is true only if no ordinary groups are added separately and no groups are explicitly specified on any user creation. By default gid for the next added user = max(gid) + 1. There is no correlation between UID and GID required by any *NIX system they just happen to coincide in some cases.

More references

http://www.thegeekstuff.com/2009/06/useradd-adduser-newuser-how-to-create-linux-users

Share:
12,811

Related videos on Youtube

Rohit Kapur
Author by

Rohit Kapur

Updated on September 18, 2022

Comments

  • Rohit Kapur
    Rohit Kapur almost 2 years

    I have to change file/directory's ownership using chmod .I know only owners's username but want to change default group name simultaneosly . At this stage group name is not decided so I am putting same as username

    chown -R username:username path_to_dir
    

    Command may run for various sytems/users , So my concern is , will linux always has a pair username(user):username(group) OR at some point this default group_name may give error.

    Although I have started to find GID from the user belongs

    id username | tr '(=)' ':' | awk -F: '{print $3}
    

    But for knowledge I want to know . Till now analysys of /etc/passwd /etc/groups except few users like "shutdown,halt,sync,opeartor" all users have listed in groups as well.

    EDIT 1: id -gn username is also good option

    • user1686
      user1686 about 10 years
      In the second example, were you looking for id -g username?
    • Cristian Ciupitu
      Cristian Ciupitu about 10 years
      Or id -gn username which prints the name instead of the ID.
    • Baard Kopperud
      Baard Kopperud about 10 years
      In Linux, there are also lots of "device-groups" - eg. disk, mem, kmem, audio, video, etc. - which don't have corresponding user.
  • Karlson
    Karlson about 10 years
    The last statement is completely false. UID=GID only in the case when the new user is created on the local system and GID isn't explicitly assigned to an existing group.
  • Erathiel
    Erathiel about 10 years
    No, it's not completely false, as it clearly says "by default". Some clarification would be much appreciated here.
  • Karlson
    Karlson about 10 years
    @Erathiel By default GID is equal to GID assigned at user creation. If user is created locally and without specifying GID in -g flag on useradd the program will create a group for you and unless a GID shift has happened it will = UID. However, if the creation method is not known you cannot rely on the fact that this will be the case: uid=123456(ME) gid=777(ABC) groups=777(ABC)
  • Ramesh
    Ramesh about 10 years
    @Karlson, I never mentioned it will always be equal. It is clearly specified that by default it is equal to the UID for all ordinary users.
  • Karlson
    Karlson about 10 years
    @Ramesh Which can also not be true. Run: useradd abc, groupadd def, useradd hij. For hij the gid should be uid + 1. The statement is true only if no ordinary groups are added separately and no groups are explicitly specified on any user creation. By default gid for the next added user = max(gid) + 1. There is no correlation between UID and GID required by any *NIX system they just happen to coincide in some cases.
  • Erathiel
    Erathiel about 10 years
    @Karlson The clarification request was more of a comment on how Ramesh could improve his answer rather than a request for myself, but thank you anyway.
  • Ramesh
    Ramesh about 10 years
    @Karlson, true. But when you have not messed up with the groups since the installation, then uid can be equal to gid which is the case in most of the machines. Again, am not saying uid will always be equal to gid but saying most of the times it will be equal.
  • Karlson
    Karlson about 10 years
    @Ramesh Then again coming back to statement you made: by default. It is not by default. It is conditional, if you have not explicitly added groups and if you have not messed with /etc/default/useradd and distribution uses standard useradd: thegeekstuff.com/2009/06/…
  • Ramesh
    Ramesh about 10 years
    @Karlson, thanks for pointing out the mistakes I made. I have edited the answer to add your comments. Please let me know if it is fine :)
  • Jules
    Jules about 10 years
    It may also depend not only on local admin policy, but on the tools included with various distributions. Many sites will just accept the defaults used by the tools provided. While most of the distributions I've used over the years ship with a useradd script that by default creates a group with the same name, not all have. Last time I used it (many, many years ago), slackware's user creation script defaulted to having a single group users which all users were made a member of instead. docs.slackware.com/slackbook:users suggests this may still be the case.
  • jstarek
    jstarek about 10 years
    "by default the UID and the GID are equal for ordinary users" -- actually, this statement is only true if you add "on some popular Linux distributions". Yes, Ubuntu et al. do it this way, but I would consider a common "users" group more of a default.
  • Erathiel
    Erathiel about 10 years
    Yes, if we aim to be 100% precise then you are right. We would probably need to add a dozen more various 'ifs' to never miss a single exception. All of my Debian-based distros (LMDE, Crunchbang, pure Debian) behave this way and one CentOS 6.5 I have as well. Mind you that since there is no need for me to add huge amounts of users I tend to use adduser rather than useradd and the defaults in /etc/adduser.conf are set to USERGROUPS=yes which creates a group for each new user. These groups typically (not always, though) have the same GID as the UID of the user for whom they were created.