CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Users & Groups / groups

groups

List the groups a user belongs to

Beginnergroupspermissionsdebugging

groups prints group names: for your current shell, or for any named user from the account database. It's the quickest way to answer 'am I in docker / sudo / adm yet?' when a permission error appears.

Syntaxgroups [USER]...

Watch it run

Reading /var/log/syslog fails and you want to know whether you're in the adm group.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 5
tail -2 /var/log/syslog

syslog is root:adm with mode 640, so only root and members of adm can read it.

Anatomy of a command

$
commandgroups

Prints group names (coreutils). Equivalent to id -Gn.

How it works

groups is a thin wrapper around the same code as id -Gn. With no argument it calls getgroups() and getegid() on its own process, so it shows the groups your current shell actually carries, set at login by initgroups(). With user names it resolves each through NSS (getpwnam for the primary group, getgrouplist for the rest), which reads /etc/passwd and /etc/group or LDAP/SSSD. That difference is why groups and groups $USER can disagree right after someone runs usermod -aG: the database is updated immediately, the processes of an existing session never are.

Options you'll actually use

(no argument)Groups of the current process (your shell)
USER...Groups of each named user, from the account database
id -GnSame list without the 'user :' prefix
getent group NAMECompanion: who is listed as a member of a group
newgrp GROUPCompanion: start a shell with a newly added group active

Common mistakes

Running groups right after usermod -aG and concluding it failed

Without an argument, groups shows your existing session, which never picks up new groups.

groups $USER to check the database; log in again (or newgrp) to use the group
Checking membership with grep docker /etc/group only

Primary-group members aren't in the member list, and LDAP/SSSD users aren't in the file at all.

getent group docker and id -Gn USER (both go through NSS)
Adding users to the docker group as a harmless convenience

Docker socket access lets you run a container with / mounted, which is root on the host.

Only trusted admins in docker; use rootless Docker or sudo rules for others

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print your current groups, then the groups of root.

  2. 2

    List the members of the adm group from the account database.

  3. 3

    Add devops to a new group labteam, show that groups devops sees it but plain groups doesn't, then use newgrp to get it.

Related commands

idShow user and group identityusermodChange an existing user accountgroupaddCreate a groupchgrpChange a file's group
PreviouswNextchage
Linux / Users & Groups / groups

groups

List the groups a user belongs to

Beginnergroupspermissionsdebugging

groups prints group names: for your current shell, or for any named user from the account database. It's the quickest way to answer 'am I in docker / sudo / adm yet?' when a permission error appears.

Syntaxgroups [USER]...

Watch it run

Reading /var/log/syslog fails and you want to know whether you're in the adm group.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 5
tail -2 /var/log/syslog

syslog is root:adm with mode 640, so only root and members of adm can read it.

Anatomy of a command

$
commandgroups

Prints group names (coreutils). Equivalent to id -Gn.

How it works

groups is a thin wrapper around the same code as id -Gn. With no argument it calls getgroups() and getegid() on its own process, so it shows the groups your current shell actually carries, set at login by initgroups(). With user names it resolves each through NSS (getpwnam for the primary group, getgrouplist for the rest), which reads /etc/passwd and /etc/group or LDAP/SSSD. That difference is why groups and groups $USER can disagree right after someone runs usermod -aG: the database is updated immediately, the processes of an existing session never are.

Options you'll actually use

(no argument)Groups of the current process (your shell)
USER...Groups of each named user, from the account database
id -GnSame list without the 'user :' prefix
getent group NAMECompanion: who is listed as a member of a group
newgrp GROUPCompanion: start a shell with a newly added group active

Common mistakes

Running groups right after usermod -aG and concluding it failed

Without an argument, groups shows your existing session, which never picks up new groups.

groups $USER to check the database; log in again (or newgrp) to use the group
Checking membership with grep docker /etc/group only

Primary-group members aren't in the member list, and LDAP/SSSD users aren't in the file at all.

getent group docker and id -Gn USER (both go through NSS)
Adding users to the docker group as a harmless convenience

Docker socket access lets you run a container with / mounted, which is root on the host.

Only trusted admins in docker; use rootless Docker or sudo rules for others

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print your current groups, then the groups of root.

  2. 2

    List the members of the adm group from the account database.

  3. 3

    Add devops to a new group labteam, show that groups devops sees it but plain groups doesn't, then use newgrp to get it.

Related commands

idShow user and group identityusermodChange an existing user accountgroupaddCreate a groupchgrpChange a file's group
PreviouswNextchage