Advertisement
Advertisement


Is there a command to list all Unix group names?


Question

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.

2017/05/07
1
616
5/7/2017 1:26:05 AM

Accepted Answer

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

2019/06/03
876
6/3/2019 6:03:01 PM

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
2020/04/26

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.

2020/06/20