How to list groups with gid in redhat?

The groups command lists groups that the user is currently a member of, not all the groups available on the system. You can lookup a group by name or gid using the getent command.

getent group oinstall
getent group 500

To show all the groups, just leave your search query off of the command:

getent group

You can list groups by using getent or if not present, look in /etc/group

When running the command, the GID is the third value in the output, e.g:

$ getent group
man:x:15:
audio:x:63:
nobody:x:99:
users:x:100:
<--snipped-->
utmp:x:22:

So to only list groupnames and their GID, use awk to print the columns you want like this:

$ getent group | awk -F ":" '{ print $1,$3 }'
man 15
audio 63
nobody 99
users 100
<--snipped-->
utmp 22

See also http://man7.org/linux/man-pages/man1/getent.1.html

In the case that getent is not present.

$ grep $group /etc/group

The GID is the 3rd value in the output. E.g

$ grep users /etc/group
users:x:100:

In the above, the GID for the group users is 100. To only get group name and GID, you can use awk and grep like this:

$ cat /etc/group | awk -F ":" '{ print $1,$3 }' 
man 15
audio 63
nobody 99
users 100
<--snipped-->
utmp 22

Be aware that for systems set up to use external authentication (like LDAP), all groups will not be present in the /etc/group file. See also http://www.cyberciti.biz/faq/understanding-etcgroup-file/

Tags:

Command

Rhel