How To Remove User From Group In Linux

If you’re managing a Linux system, it may be necessary at times to remove a user from a certain group. This operation is quite simple and straightforward once you know the right commands. This blog post will guide you on how to remove a user from a group in Linux.

The ‘gpasswd’ command

The gpasswd command in Linux is used for managing ‘/etc/group’ and ‘/etc/gshadow’. This command is used to administer /etc/group, and /etc/gshadow. Every group can be identified by its group name.

Basic Syntax

The basic syntax to remove a user from a group is:

        sudo gpasswd -d username groupname
        

Replace username with the name of the user you wish to remove and groupname with the name of the group from which the user should be removed.

An Example

Let’s say you want to remove the user ‘john’ from the group ‘developers’. You would type the following command:

        sudo gpasswd -d john developers
        

The ‘deluser’ command

Another command you can use to achieve this is the deluser command.

Basic Syntax

The basic syntax to remove a user from a group is:

        sudo deluser username groupname
        

Again, replace username with the name of the user you wish to remove and groupname with the name of the group from which the user should be removed.

Either of these commands will remove the user from the group. It’s important to note that the user’s primary group cannot be removed using these commands. The primary group can only be changed using usermod -g command.

Conclusion

It is important to manage your group memberships properly for security and to maintain an organized Linux system. Fortunately, removing a user from a group in Linux is a simple task once you know these commands. Remember to replace ‘username’ and ‘groupname’ with your specific user and group names. Happy Linuxing!