40

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?

3
  • 2
    Isn't the primary group listed first?
    – simlev
    Commented Dec 12, 2017 at 8:48
  • @simlev I reached here looking for info on that. On my recent arch linux install that doesn't seem to be the case. It shows "wheel" as the first group. But doing id -gn shows the primary group that I would expect.
    – codepoet
    Commented Sep 29, 2021 at 21:05
  • @reportaman I cannot confirm this: I just installed a new arch, ran useradd myuser; usermod -aG wheel myuser and got gid=1000(myuser) groups=1000(myuser),998(wheel).
    – simlev
    Commented Oct 1, 2021 at 14:26

2 Answers 2

49

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.

0
1

Since groups <username> will list the primary group of the user first, you can use the following to only extract the primary group of the specified user:

groups <username> | awk '{print $3}'

1
  • 3
    On Archlinux 2023-7-23, this is not true. The output of groups is sorted alphabetically. Commented Jul 31, 2023 at 20:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .