Skip to content

groupmod Command Cheat Sheet

groupmod modifies the definition of the specified GROUP by modifying the appropriate entry in the group database system files.


Synopsis

groupmod [options] GROUP

Basic Usage

Rename a Group (-n)

Change the name of the group from old_name to new_name.

sudo groupmod -n team_alpha team_a
Note: This changes the name, but the GID remains the same. All files owned by the group remain owned by the new name.

Change Group ID (-g)

Change the numerical GID of the group.

sudo groupmod -g 2500 team_alpha

⚠️ Important Side Effect: Files owned by the old GID are not automatically updated. They will now appear to be owned by a nameless numeric ID (the old GID). You must update them manually.


Advanced Options

Allow Duplicate GID (-o)

Force a group to check a GID that is already in use (create an alias).

sudo groupmod -g 0 -o admin_alias
Now admin_alias has GID 0 (root).

Override Password (-p)

Change the encrypted password.

sudo groupmod -p "$ENCRYPTED_PASS" team_alpha

Handling GID Changes

If you run groupmod -g, you break file ownerships. Here is how to fix them.

Step-by-Step GID Migration

  1. Identify old GID:

    OLD_GID=$(getent group team_beta | cut -d: -f3)
    NEW_GID=3000
    

  2. Modify Group:

    sudo groupmod -g $NEW_GID team_beta
    

  3. Find and Fix Files: Search for files with the OLD_GID and update them to NEW_GID.

    sudo find / -gid $OLD_GID -exec chgrp team_beta {} \;
    


Comparison: groupmod vs usermod

  • groupmod: Changes the definition of the group itself (Name, GID).
  • usermod: Changes a user's relationship to groups (add/remove user from group).

Don't confuse them:

# Rename group 'staff' to 'employees'
groupmod -n employees staff

# Add user 'bob' to group 'employees'
usermod -aG employees bob


Exit Status

Code Meaning
0 Success
2 Command syntax error
3 Argument error
4 User does not exist (when modifying user-group)
6 Group does not exist
9 Group name already in use
10 Cannot update group file

Notes

  • Changes apply immediately to the database, but logged-in users must re-login (or run newgrp) to see the changes.
  • /etc/group and /etc/gshadow are the files modified.