37

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.

61

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
  • 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 Oct 26 '13 at 3:02
  • 1
    @kitex I updated to wording slightly, hope that helps you understand. – jordanm Oct 26 '13 at 3:21
  • 2
    @Kitex updated the answer – jordanm Oct 26 '13 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 Feb 1 '16 at 20:10
  • 1
    @kinkajou I used Gnu/Linux for all of my computing at home. Desktops, laptops, raspberry-pi's. I have been using it since 1991 (originally Gnu on Solaris, Dec, HP). I keep adding to my knowledge; No need to through it away, and start again (as with Microsoft's Windows). – ctrl-alt-delor Nov 22 '18 at 13:35
9

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/

  • 6
    Groups are not always stored in /etc/group. They could be stored in NIS, LDAP, etc. – jordanm Oct 26 '13 at 4:02
  • 1
    Do not work with ldap group, use getent group. – MUY Belgium Apr 15 '15 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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