650

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.

ekad
  • 14,436
  • 26
  • 44
  • 46
cavila
  • 7,834
  • 5
  • 21
  • 19
  • 7
    So far he tried http://stackoverflow.com/questions/14059916/there-is-a-command-to-list-all-unix-group-names – ott-- Dec 27 '12 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). –  Dec 27 '12 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 Dec 27 '12 at 19:54
  • Similar: [How do you find out what group a given user is in via command line?](http://stackoverflow.com/q/350141/55075) – kenorb Apr 13 '15 at 21:52

3 Answers3

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

miken32
  • 42,008
  • 16
  • 111
  • 154
Arpit
  • 12,767
  • 3
  • 27
  • 40
  • 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 Dec 27 '12 at 20:02
  • 1
    ok so for now the answer is NO. Need to use text editing to filter group file. – cavila Dec 27 '12 at 20:10
  • 3
    What is "cut -d: -f1"? – zed Jan 31 '17 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 Feb 01 '17 at 10:40
295

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
dasup
  • 3,815
  • 1
  • 16
  • 25
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.

Community
  • 1
  • 1
kenorb
  • 155,785
  • 88
  • 678
  • 743