57

I first listed the groups using :

groups 

I added group using

groupadd -g 300 oinstall
groupadd –g 500 dba

and then when I do

groups 
root bin daemon sys adm disk wheel sfcb

I am unable to find groups I added. How to list groups with group id? Also, if I try to add it again it says groups is already present.

2 Answers 2

91

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
14
  • 2
    @Kitex because you created the group but you are not a member of it. You need to add your user to the group then logout and log back in for it to show up with the groups command.
    – jordanm
    Commented Oct 26, 2013 at 3:02
  • 1
    @kitex I updated to wording slightly, hope that helps you understand.
    – jordanm
    Commented Oct 26, 2013 at 3:21
  • 1
    Also be aware that for groups to show a newly added group, the affected user have to log out and in.
    – M_dk
    Commented Oct 26, 2013 at 3:37
  • 2
    @Kitex updated the answer
    – jordanm
    Commented Oct 26, 2013 at 16:22
  • 3
    @kinkajou lots of professional experience as well as a lot of time spent on sites such as this one and IRC Linux support. I have been a professional and an enthusiast for about 8 years. I think being an enthusiast is an important part because you are more likely to remember things that you care about.
    – jordanm
    Commented Feb 1, 2016 at 20:10
11

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/

3
  • 7
    Groups are not always stored in /etc/group. They could be stored in NIS, LDAP, etc.
    – jordanm
    Commented Oct 26, 2013 at 4:02
  • 2
    Do not work with ldap group, use getent group. Commented Apr 15, 2015 at 8:50
  • 1
    For NIS, use ypcat group. For LDAP, getent group may work -- LDAP has to be configured to work with getent, otherwise it will only show the contents of /etc/group. $0.02
    – Scottie H
    Commented Sep 29, 2022 at 16:55

You must log in to answer this question.

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