groups – print group memberships

groups command
groups command

groups prints the group memberships for a user.

Synopsis

groups [USER...]

Examples

Current user’s groups

$ groups
greys sudo docker www-data

Specific user

$ groups root
root : root

$ groups www-data
www-data : www-data

Multiple users

$ groups root greys admin
root : root
greys : greys sudo docker
admin : admin sudo

Alternative: id -Gn

$ id -Gn
greys sudo docker www-data

$ id -Gn root
root

Why Groups Matter

Groups control file access:

$ ls -l /var/run/docker.sock
srw-rw---- 1 root docker 0 Jan 29 10:00 /var/run/docker.sock

# Need to be in 'docker' group to access
$ groups | grep docker && echo "Can use Docker"

Managing Groups

Add user to group

$ sudo usermod -aG docker greys
# User must log out and back in for it to take effect

Create new group

$ sudo groupadd developers

List all groups

$ getent group
$ cat /etc/group

Tips

  • Log out required: Group changes take effect on next login
  • Use newgrp: newgrp docker to activate group in current session
  • Primary vs secondary: First group is primary, others are supplementary
  • Check with id: id -Gn gives same info with different format

See Also

  • id — Show user and group IDs
  • usermod — Modify user accounts
  • getent — Query system databases

Tutorials