Adding/Removing a User from a Group in Linux

Adding/Removing a User from a Group in Linux

Linux allows multiple users to connect to it and work in collaboration. Hence it, by default, is a multi-user system. Managing all the users is a fundamental task for any Linux admin. User management involves everything from creating users, to managing their privileges to removing them when not necessary. All in all, there’s a life cycle involved with every user in the Linux system. In this article, learn how you can add or remove a user from a group in Linux.

Checking the User

Before you can add an user, you need to check if they already exist in a particular group. Otherwise, there’s no point in adding them. To check for the user, use the groups command suffixed with the username like this:

Code:
# groups user1
To check for the groups, simply execute the groups command like this:

Code:
# group
Adding an User

To add a user to a group, you must ensure that the user exists on the Linux system. You can use the cat command to view the list of all users available:

Code:
$ cat /etc/passwd
To add a user to a group, you can use the usermod command. By adding -a flag, you instruct usermod to add that user to the group. Here’s how it looks like:

Code:
# usermod -aG postgres user1
Here -G specifies the group, which is postgres. The command will add ‘user1’ to postgres group.

Removing the User

Once you’ve added an user, you may feel the need to remove them. You can do that with the gpasswd command followed by the -d flag. Here’s what it looks like:

Code:
# gpasswd -d user1 postgres
User1 will be deleted from the group successfully.

Another command you can use is the deluser command like this:

Code:
$ sudo deluser user1 postgres
So that you can add or remove user from a particular. Add more users to the list if you’ve multiple users to add/remove.
Author
kumkumsharma
Views
1,718
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top