651

I know there is the /etc/group file that lists all users groups.

I would like to know if there is a simple command to list all user group names in spite of parsing the world readable /etc/group file. I am willing to create an administrator web page that lists Linux accounts' group names.

4
  • 7
    So far he tried stackoverflow.com/questions/14059916/…
    – ott--
    Commented Dec 27, 2012 at 19:24
  • 2
    "I willing to create a web page that lists Linux users" - what problem are you trying to solve? This sounds like something that may cause some security problems (exposing list of users, exposing credentials).
    – user289086
    Commented Dec 27, 2012 at 19:34
  • I was trying to give an simple example. I would like to open an "administrator system web page to list current Linux accounts names". In Linux I could find commands to add a user, remove a user, change a user, find the groups of a given user but did not found a command to search a user by name fragment. I think the question is not so irrelevant. All I could do to rememebr a Linux group account was to do a lookup on /etc/group file
    – cavila
    Commented Dec 27, 2012 at 19:54
  • Similar: How do you find out what group a given user is in via command line?
    – kenorb
    Commented Apr 13, 2015 at 21:52

3 Answers 3

920

To list all local groups which have users assigned to them, use this command:

cut -d: -f1 /etc/group | sort

For more info- > Unix groups, Cut command, sort command

4
  • 3
    Yes MichaelIT is right the groups command did not list all groups. I asked this because unsure if there is a simple command like groups to lists all groups names or even a swith to it like groups [-a|--all] to list all system groups without doing file scan.
    – cavila
    Commented Dec 27, 2012 at 20:02
  • 1
    ok so for now the answer is NO. Need to use text editing to filter group file.
    – cavila
    Commented Dec 27, 2012 at 20:10
  • 3
    What is "cut -d: -f1"?
    – zed
    Commented Jan 31, 2017 at 14:20
  • 6
    @zed cut is another command which extracts the specific column from an input. Here I'm extracting field 1 where fields are delimited by :
    – Arpit
    Commented Feb 1, 2017 at 10:40
296

If you want all groups known to the system, I would recommend using getent group instead of parsing /etc/group:

getent group

The reason is that on networked systems, groups may not only read from /etc/group file, but also obtained through LDAP or Yellow Pages (the list of known groups comes from the local group file plus groups received via LDAP or YP in these cases).

If you want just the group names you can use:

getent group | cut -d: -f1
5
  • 11
    For some usecases a sorted list of group names might be preferable: getent group | cut -d: -f1 | sort Commented Jul 29, 2016 at 18:12
  • 2
    If numbered lines are desirable, do getent group | cut -d: -f1 | sort | cat -n.
    – MLC
    Commented Sep 22, 2016 at 20:28
  • 1
    great point for networked systems like LDAP!
    – Maziyar
    Commented Nov 18, 2018 at 21:20
  • @dasup do you know how to create ID that will only assign local group not from LDAP in case of networked system.
    – Dwija
    Commented Jul 15, 2020 at 11:33
  • Need some explanation for the output format. Commented Oct 3, 2022 at 3:15
55

On Linux, macOS and Unix to display the groups to which you belong, use:

id -Gn

which is equivalent to groups utility which has been obsoleted on Unix (as per Unix manual).

On macOS and Unix, the command id -p is suggested for normal interactive.

Explanation of the parameters:

-G, --groups - print all group IDs

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

-p - Make the output human-readable.

6
  • 11
    And id -Gn someusername returns the list of groups for the specified user.
    – grim
    Commented Jun 10, 2016 at 17:21
  • 8
    id -Gnz | xargs -0 -I% echo % will list each group on a separate line. This is useful if the group names have spaces in them. Commented Dec 12, 2017 at 19:29
  • 1
    How is groups obsolete? Any sources? I searched "unix groups command obsolete" but did not find anything. Commented Aug 27, 2018 at 14:13
  • 1
    @FranklinYu It's in the BSD manual page for groups.
    – kenorb
    Commented Aug 28, 2018 at 10:50
  • 4
    Doest answer original question pertaining to groups outside of current user.
    – Nay
    Commented Jan 25, 2019 at 19:41

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