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 / Permissions & Ownership / chgrp

chgrp

Change a file's group

Beginnerpermissionsgroupsownership

chgrp changes the group that owns a file, which decides who gets the group permission bits. Unlike chown, a file's owner can do it without sudo, as long as they're a member of the target group. Use it to share directories and logs with a team.

Syntaxchgrp [-R] [-h] [-v | -c] GROUP FILE...

Watch it run

devops owns /srv/reports and wants everyone in devteam to read and add reports there.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
ls -ld /srv/reports

Group is devops's private group, so the group bits help nobody else.

Anatomy of a command

$
commandchgrp

Change group: sets the GID stored in each inode (coreutils). Same as chown :GROUP.

How it works

chgrp resolves the group name through NSS and calls fchownat() with the owner left as -1, so only the GID in the inode changes. The kernel allows it if the caller is root, or owns the file AND is a member of the new group (checked against the process's current groups, so a group you were just added to only works after a new login). As with chown, changing the group of an executable clears its setuid/setgid bits. chgrp doesn't change permission bits: the group still gets whatever the middle rwx triplet says. For a shared directory you normally pair it with chmod g+ws (setgid) so new files inherit the group, and a 002 umask or default ACL so they're group-writable.

Options you'll actually use

-RRecursive
-v / -cReport every file / only changes
-hChange a symlink itself, not its target
--reference=FILEUse another file's group
GIDA numeric group ID works without a name
chmod g+s DIRCompanion: new files in DIR inherit its group

Common mistakes

chgrp devteam on a shared directory and stopping there

Files people create later still get each creator's private group, so others can't edit them.

chgrp devteam DIR && chmod g+ws DIR (plus umask 002 or a default ACL)
Running chgrp right after being added to the group and getting 'Operation not permitted'

The kernel checks your session's current groups, which don't include the new group until you log in again.

Log in again or newgrp GROUP, then chgrp
Fixing log permissions with chgrp but not the logrotate config

At the next rotation logrotate creates a new file with the old group and readers lose access.

Set 'create 0640 appsvc adm' in /etc/logrotate.d/app

Interview questions

Practice

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

  1. 1

    Create ~/data/team and change its group to adm (use sudo if you're not in adm).

  2. 2

    Make ~/data/team setgid and group-writable, then create a file inside and check its group.

  3. 3

    Recursively change the group of /var/log/app to adm and list each change.

Related commands

chownChange file owner and groupchmodChange file permissionsgroupaddCreate a groupgroupsList the groups a user belongs to
PreviouschownNextumask
Linux / Permissions & Ownership / chgrp

chgrp

Change a file's group

Beginnerpermissionsgroupsownership

chgrp changes the group that owns a file, which decides who gets the group permission bits. Unlike chown, a file's owner can do it without sudo, as long as they're a member of the target group. Use it to share directories and logs with a team.

Syntaxchgrp [-R] [-h] [-v | -c] GROUP FILE...

Watch it run

devops owns /srv/reports and wants everyone in devteam to read and add reports there.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
ls -ld /srv/reports

Group is devops's private group, so the group bits help nobody else.

Anatomy of a command

$
commandchgrp

Change group: sets the GID stored in each inode (coreutils). Same as chown :GROUP.

How it works

chgrp resolves the group name through NSS and calls fchownat() with the owner left as -1, so only the GID in the inode changes. The kernel allows it if the caller is root, or owns the file AND is a member of the new group (checked against the process's current groups, so a group you were just added to only works after a new login). As with chown, changing the group of an executable clears its setuid/setgid bits. chgrp doesn't change permission bits: the group still gets whatever the middle rwx triplet says. For a shared directory you normally pair it with chmod g+ws (setgid) so new files inherit the group, and a 002 umask or default ACL so they're group-writable.

Options you'll actually use

-RRecursive
-v / -cReport every file / only changes
-hChange a symlink itself, not its target
--reference=FILEUse another file's group
GIDA numeric group ID works without a name
chmod g+s DIRCompanion: new files in DIR inherit its group

Common mistakes

chgrp devteam on a shared directory and stopping there

Files people create later still get each creator's private group, so others can't edit them.

chgrp devteam DIR && chmod g+ws DIR (plus umask 002 or a default ACL)
Running chgrp right after being added to the group and getting 'Operation not permitted'

The kernel checks your session's current groups, which don't include the new group until you log in again.

Log in again or newgrp GROUP, then chgrp
Fixing log permissions with chgrp but not the logrotate config

At the next rotation logrotate creates a new file with the old group and readers lose access.

Set 'create 0640 appsvc adm' in /etc/logrotate.d/app

Interview questions

Practice

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

  1. 1

    Create ~/data/team and change its group to adm (use sudo if you're not in adm).

  2. 2

    Make ~/data/team setgid and group-writable, then create a file inside and check its group.

  3. 3

    Recursively change the group of /var/log/app to adm and list each change.

Related commands

chownChange file owner and groupchmodChange file permissionsgroupaddCreate a groupgroupsList the groups a user belongs to
PreviouschownNextumask