Red Hat Training

A Red Hat training course is available for RHEL 8

Chapter 23. Removing a user from a group using 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.

23.1. Overriding the primary group of a user

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

Prerequisites

  • Root access

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 23.1. Changing the primary group of a user

    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

23.2. Overriding the supplementary groups a user

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

Prerequisites

  • Root access

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 23.2. Changing the supplementary group of a user

    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