Chapter 7. Removing a user from a group from the command line

You can remove a user from a primary or supplementary group by overriding the groups the user belongs to with a new set of groups that does not contain the group you want to remove the user from.

7.1. Overriding the primary group of the user

This section describes how to use the usermod command to override the primary group of the user.

Procedure

  • To override the primary group of the user, use:

    # usermod -g group-name username

    Replace group-name with the name of the group, and replace username with the name of the user.

Example

  • If the user sarah belongs to the primary groups sarah1, and you want to change the primary group of the user to sarah2, use:

    # usermod -g sarah2 sarah

Verification steps

  • To verify that the primary group of the user is overridden, use:

    # groups sarah

    The output returns:

    sarah : sarah2

7.2. Overriding the supplementary groups of the user

This section describes how to use the usermod command to override the supplementary groups of the user.

Procedure

  • To override the supplementary groups of the user, use:

    # usermod -G group-name username

    Replace group-name with the name of the group, and replace username with the name of the user.

Example

  • If the user sarah belongs to the system-administrator group and to the developer group and you want to remove the user sarah from the system-administrator group, you can do that by replacing the old list of groups with a new one. To do that, use:

    # usermod -G developer sarah

Verification steps

  • To verify that the supplementary groups of the user are overridden, use:

    # groups sarah

    The output returns:

    sarah : sarah developer