How do I remove a user from a group?

Which command should I use to remove a user from a group in Debian?

When adding a user to a group, it can be done with:

usermod -a -G group user

However, I could not find a similar command (accepting a group and user as arguments) for removing the user from the group. The closest I could get is:

usermod -G all,existing,groups,except,for,group user

Is there a command like usermod OPTION group user with OPTION an option to make usermod (or a similar program) remove the user from group?

Asked By: Lekensteyn

||

You can use gpasswd:

# gpasswd --delete user group

The new group config will be assigned at the next login. If the user is logged in, the effects of the command aren’t seen immediately.

Answered By: user13742

On Debian, the adduser package contains a deluser program which removes a user from a group if you pass both as arguments:

deluser user group

If your distribution doesn’t have adduser, you can edit /etc/group and /etc/gshadow manually.

vigr
vigr -s
usermod -G "" username

removes all secondary/supplementary groups from username, leaving them as a member of only their primary group.
this worked in Solaris 5.9

Answered By: user208145

You can use the below command on SUSE distributions
(and, apparently, no others).

usermod -R group user_name

where group is the group that you want to remove the user from
and user_name the user that you want to remove from the group.
For example,

usermod -R root imnottheroot
pw groupmod "groupname|gid" -d "username|uid"

A solution if you are using CSH, for whatever reason.

Answered By: james

This is the “old school” approach…

Most *nix systems maintain group information into a plain text file /etc/group, where

  • each line contains the fields

    • group_name
    • password
    • GID, and
    • user_list

    delimited by the : character.

  • the user_list field is a list of user names, separated by commas.

Now suppose you want to remove a user named thisuser
from a group named thatgroup.  Start by backing up /etc/group,
then use the editor of your preference with su privileges
to edit the file /etc/group
and remove the thisuser reference from the thatgroup line entry, e.g.,

original line is something like this:

thatgroup:x:1274:someuser,thisuser,anotheruser

after editing should be left like this:

thatgroup:x:1274:someuser,anotheruser

As with all the other answers, this will not affect the user’s current session(s), if any (i.e., if the user is currently logged in). 
The change will take effect the next time the user logs in.

Answered By: p57

Consider:

  • username: abc2
  • group name: newgroup11

  • Task: Removing user abc2 from group newgroup11

[root@home1 ~]# groups abc2
abc2 : abc2
[root@home1 ~]# usermod -G newgroup11 abc2
[root@home1 ~]# groups abc2
abc2 : abc2 newgroup11
[root@home1 ~]# usermod -G newgroup11 abc2
[root@home1 ~]# usermod -G abc2 abc2
[root@home1 ~]# groups abc2
abc2 : abc2

** Kindly correct me if I am wrong. **

Answered By: new user

To continue using usermod in a distro (like Fedora) which does not have a remove option, where user=bob and group=deletethisgroup, command would be:

usermod -G `cat /etc/group |  grep bob | grep -v deletethisgroup | cut -d ':' -f 1 | tr 'n' ',' | sed 's/,$//'` bob

The pipes (1) get all group entries user belongs to, (2) take out the one which needs to be removed, (3) returns first column (group name), replaces newline with comma, and removes trailing comma.

Of course, you could put all that in a bash script which takes user and group to be deleted as parameters. awk could be used to shorten the end but I wanted to stick to grep, cut, tr and sed.

Answered By: Stephen

To remove a user from a group gpasswd is the best utility for this IMO.

Command Example:

sudo gpasswd -d group user

* Help Info *

Usage: gpasswd [option] GROUP

Options:
  -a, --add USER                add USER to GROUP
  -d, --delete USER             remove USER from GROUP
  -h, --help                    display this help message and exit
  -Q, --root CHROOT_DIR         directory to chroot into
  -r, --delete-password         remove the GROUP's password
  -R, --restrict                restrict access to GROUP to its members
  -M, --members USER,...        set the list of members of GROUP
  -A, --administrators ADMIN,...
                                set the list of administrators for GROUP
Except for the -A and -M options, the options cannot be combined.
Answered By: Brian Cotton

Suppose that username=student and groupname=research, therefore to remove student user from research group it’s need to do following:

gpasswd -d student research
Answered By: ravi

You can remove users from the group by executing usermod command without -a option.
Example, by executing

usermod -G group1 username

will add the user to the group1, and will remove it from any other groups where it is. Remember, you can keep users in various groups by listing the group’s names, separated with a comma.

Answered By: Helper

On OpenBSD

usermod -S "" user
Answered By: Volodymyr Boiko

The command to add a group to a user:

usermod -aG group user

The command to remove a group from a user is:

usermod -rG group user
Answered By: José Geraldo
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.