1

From The Linux Programming Interface, about /etc/passwd:

It is possible (but unusual) to have more than one record in the password file with the same user ID, thus permitting multiple login names for the same user ID. This allows multiple users to access the same resources (e.g., files) using different passwords. The different login names can be associated with different sets of group IDs.

Group ID (GID) field is the numeric ID of the first of the groups of which this user is a member. Further group memberships for this user are defined in the system group file.

about /etc/group:

User list field is a comma-separated list of names of users who are members of this group. (This list consists of usernames rather than user IDs, since, as noted earlier, user IDs are not necessarily unique in the password file.)

In /etc/passwd,

  • does the group ID field depend on user name or on user ID?

  • In other words, can different usernames for the same user ID have different group IDs, or must all the usernames for the same user ID have the same group ID?

Thanks.

3

The key in the user database, /etc/passwd or something else, is the login name: that’s all that you provide to identify yourself when you log in. From that key, a program can retrieve all the other information stored in the user database; this happens with no regard for any other user in the user database, even other users with the same user id. (Typically, this is done with getpwnam or getpwnam_r, either directly or via PAM.)

Thus the login name leads to the stored password, the user id, the (primary) group id, the GECOS information, the home directory and shell. This means that two users can share the same user id, yet have different home directories and shells! (This was commonly used in the past to provide a fall-back, statically-linked shell for root; you’d have the usual root user with id 0 and shell /bin/bash or whatever, and another user, say sashroot, with id 0 and a different shell.)

Hence the answer to

does the group ID field depend on user name or on user ID?

is that it depends only on the user name.

The key in the group database is also the group name. From that key, a program can retrieve all the other information stored in the group database; again this happens with no regard for any other group in the group database. (When determining a user’s secondary groups, the process is more complex than reading the user database: there is no function to list groups to which a given user belongs, so this is typically done in a loop involving getgrent and endgrent.)

Thus the group name leads to the group password, group id, and the list of group members, which is a list of user names. To build a user’s set of secondary groups, all the groups are enumerated, and the user’s login name is matched against the group’s members. So not only can two different users with the same user id have different primary groups, they can belong to a different set of secondary groups!

Hence the answer to

In other words, can different usernames for the same user ID have different group IDs, or must all the usernames for the same user ID have the same group ID?

is that a user’s groups only depend on the user name, and different user names can share a user id yet have different primary and secondary groups.

  • Thanks. So there is a mapping from user names to primary group IDs defined in /etc/passwd. Is there any mapping or relation between user IDs and primary group IDs? If yes, is such a mapping or relation defined outside /etc/passwd? – Tim Sep 4 '18 at 16:12
  • There is a mapping, and it’s in /etc/passwd, but the user id isn’t a unique key so the mapping isn’t one-to-one — you can have multiple group ids listed as the primary group for a given user id. – Stephen Kitt Sep 4 '18 at 16:32
  • Thanks. When we mention the primary group of a username, do we always mean the group ID not group name? Is it correct that there can be more than one group names for a group ID? In Linux in particular. (Note that when we mention the supplementary groups of a username, we always mean the group names not the group IDs, correct?) – Tim Sep 4 '18 at 16:43
  • I’ve never come across a situation where the difference is important; strictly speaking it should be the group id since that’s what’s defined in /etc/passwd (and theoretically you could have multiple groups matching a given group id). For supplementary groups, since they’re keyed by name, either works — you’ll get the same set of groups in both cases. You might find it useful to read chapter 8 of The Linux Programming Interface (if you haven’t already). – Stephen Kitt Sep 4 '18 at 16:51
  • Since there can be more than one group names for a group ID, when we mention the supplementary groups of a username, it makes difference whether we mean the group names or the group IDs. I guess? – Tim Sep 4 '18 at 16:59
1

Does the group ID field depend on user name or on user ID?

No. Each account entry has required fields of a username, UID (userid), and GID (group ID) in the /etc/passwd entry line. But GID is not 'dependent' on anything. You can add your own custom groups to /etc/group with the addgroup command.

In other words, can different usernames for the same user ID have different group IDs, or must all the usernames for the same user ID have the same group ID?

Yes, there is no 'dependency' between UID and GID if that's what you're wondering. The GID in the /etc/passwd entry denotes the default group ID of the user, but you can assign the user to as many groups as you want in /etc/group using usermod command:

sudo usermod -a -G groupname username

You can also change the default GID to whatever you want. The default is to create a GID under the same name as the user, but this isn't a hard requirement. You can change it.

  • Thanks. (1) in /etc/passwd, can two lines with different user names and the same user ID have different values for the group ID field, or must they have the same value for the group ID field? (2) Is there no difference between the “primary” group and any supplementary group of a username in terms of both concepts and uses? – Tim Sep 4 '18 at 10:53
  • Like I was saying, the UID and GID are not hard wired to one another. So you can change the default GID of a user account to be whatever you want using the usermod and groupmod commands. – A H Sep 5 '18 at 18:15
  • By 'primary' I'm assuming you mean your default GID in /etc/passwd? Nope, if you're a member of the group you have the same group permissions as any other member of the group. It doesn't matter if it reflects as your 'primary' group in /etc/passwd at all. – A H Sep 5 '18 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.