Add and Remove User from Group

Add and Remove User from Group

User administration includes a multiple tasks where the administrator has to add users to certain group of users.

The most common thing to do is use the command usermod to accomplish this.

Add and Remove User from Group – Add using usermod

To add a user to a group without changing the current set of groups the user is in, we use the command usermod.

Let’s an example for user gagan who needs to be added to the group admin. The user is already added under several different groups and we don’t need to make any changes in there.

Let’s collect the current UID and GID info for the user using the id command.

[[email protected] ~]# id gagan

Output:

uid=505(gagan) gid=505(gagan) groups=505(gagan),601(ops),604(devs)

We will now add the user gagan into another group called admin, without affecting the current group configs.

[[email protected] ~]# usermod -a -G admin gagan

Where,

the -a flag will append the users group list.
-G is the list of group the user is to be added. You can specify multiple groups seperated by comma.

Verify the changes.

[[email protected] ~]# id gagan

Output:

uid=505(gagan) gid=505(gagan) groups=505(gagan),601(ops),604(devs),611(admin)

Add and Remove User from Group – Remove using usermod

Now, let’s remove the user gagan from the group devs using usermod.

Get the information about the groups the user is added to using id again.

Using usermod, we will manually specify the groups where the user gagan should be.

[[email protected] ~]# usermod -G gagan,ops,admin gagan

Verify the results using id.

[[email protected] ~]# id gagan

Output:

uid=505(gagan) gid=505(gagan) groups=505(gagan),601(ops),611(admin)

Add and Remove User from Group – Add using gpasswd

Alternative, we can also use gpasswd to add and remove a user from a group.

So to add the user gagan to the group admin we will have to use the following command:

[[email protected] ~]# gpasswd -a gagan admin

Output:

[[email protected] ~]# gpasswd -a gagan admin
Adding user gagan to group admin

Verify using id.

Add and Remove User from Group – Remove using gpasswd

Removing the user from a specific group out of several different groups the user is under, can be relatively simple using gpasswd.

So to remove the user gagan from group devs we will simple use the following command.

[[email protected] ~]# gpasswd -d gagan devs

Output:

[[email protected] ~]# gpasswd -d gagan devs
Removing user gagan from group devs

Add and Remove User from Group

No Comments

Post a Comment

Time limit is exhausted. Please reload CAPTCHA.