21

The following command will list all of the groups of someUser (the primary group and the supplementary groups):

groups someUser

But is there a way to only get the primary group?

  • 1
    Isn't the primary group listed first? – simlev Dec 12 '17 at 8:48
25

To get the primary group name for some user called someUser:

id -gn someUser

To get the primary group's numerical ID (GID):

id -g someUser

The id utility is a POSIX utility and should therefore be portable across Unix systems. See the POSIX specification of id.

| improve this answer | |
  • 3
    Please don't roll back the improvements made to this answer. If you do, then consider giving a reason for doing so (possibly as a edit comment, or as a comment here). Note that your original answer was lacking the fact that the question asks for a way to got the primary group of a specific user (it was therefore not a correct answer). – Kusalananda yesterday
  • 3
    @bruce this is a collaborative site. Reverting edits that are clearly improving your answer is basically harming the site, so please stop it. If you don't agree with the wording of the improvements, feel free to rewrite them yourself, but stop removing helpful additions that make your answer more useful. – terdon yesterday
  • 3
    Please stop this. At the very least, talk to us and explain if you have a good reason to remove useful additions. – terdon 22 hours ago
18

See the FreeBSD handbook (information also valid for Linux):

Group ID (GID)

The Group ID (GID) is a number used to uniquely identify the primary group that the user belongs to. Groups are a mechanism for controlling access to resources based on a user's GID rather than their UID. This can significantly reduce the size of some configuration files and allows users to be members of more than one group. It is recommended to use a GID of 65535 or lower as higher GIDs may break some software.

If so, running id <username> will show gid=<primary group>:

id <username>
uid=1000(<username>) gid=1000(<username>) groups=1000(<username>),4(adm),24(cdrom),27(sudo)

If you want the command to return just the primary group name, see man id:

   -g, --group
          print only the effective group ID

   -G, --groups
          print all group IDs

   -n, --name
          print a name instead of a number, for -ugG

so, id -gn <username> should give you what you want.

| improve this answer | |

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.