How to change group memberships?

QuestionsQuestions4 SkillsProYour First Linux LabSep, 04 2025
0154

To change group memberships in Linux, you can use the usermod command. Here’s how to do it:

Adding a User to a Group:

To add a user to a group, use the following command:

sudo usermod -aG groupname username
  • -a: Appends the user to the group (without this, the user will be removed from other groups).
  • -G: Specifies the group to which you want to add the user.
  • groupname: The name of the group.
  • username: The name of the user.

Example:

To add a user named labex to the sudo group:

sudo usermod -aG sudo labex

Removing a User from a Group:

To remove a user from a group, you can use the gpasswd command:

sudo gpasswd -d username groupname

Example:

To remove labex from the sudo group:

sudo gpasswd -d labex sudo

Verifying Group Memberships:

After making changes, you can verify the user's group memberships with:

groups username

Note:

  • You may need to log out and back in for changes to take effect.

If you have any further questions or need more details, feel free to ask!

0 Comments

no data
Be the first to share your comment!