150

I added a user account to the admin group and discovered I added wrong user. So I now have to remove the account from the admin group.

How can I remove an account from a group without deleting the user?

4 Answers 4

189

Portablejim's answer is accurate but dangerous -- if you typo something, your system may be unusable, especially if you alter the admin group improperly. If you must edit the groups file, use the vipw -g or vigr commands, which verify the syntax before saving. Even then, there are better ways.

From a commandline, the one you probably want to use is the following (as root):

deluser <username> <groupname>

This will remove the specified user from the specified group. You must relogin to see the effect. It will not delete the user, or the group, just the membership. There are also ways of doing it with the usermod command, but it's harder to use as you need to replace the entire list of groups for a user in order to remove a single group. The gpasswd command is also capable of doing this (as sagarchalise points out), but is mostly deprecated. As always, see the man pages for more details.

6
  • 13
    gpasswd is not deprecated. Its entire purpose is to administer /etc/group and /etc/gshadow. Also, while your method does work, and is mentioned in the deluser man page, it's a bit risky. If you accidentally hit enter before you type the group name, you've remove the user. Better hope you remember its UID so you can add it back quickly. It would be safer to use gpasswd which is designed for this purpose.
    – Starfish
    Jul 27, 2013 at 23:53
  • 1
    I thought about this risk too. I guess you can always start such a command with a '#', hit enter, and then rerun it while just removing the first character. Dec 6, 2013 at 23:47
  • "You must relogin to see the effect." ...or you could just write: 'cat /etc/group' Nov 5, 2014 at 16:10
  • 1
    I had found deluser while searching man pages (here with Ubuntu 18 in 2018), but the first line in the man page states: "remove a user or group from the system", so I moved on in my search. Clearly the man page needs to be worded better.
    – tim.rohrer
    Jun 20, 2018 at 3:27
  • 1
    I really dislike this answer, starting with "Portablejim's answer is accurate but dangerous" and then stating deluser as solution where missing the second parameter will immediately delete the user. The "gpasswd" answer is way better in my optinion.
    – Marcus K.
    Jan 5, 2021 at 8:01
79

You can do this:

sudo gpasswd -d username group

See: http://manpages.ubuntu.com/manpages/focal/en/man1/gpasswd.1.html

-d, --delete user Remove the user from the named group.

4
  • 5
    One advantage of gpasswd is that it's more cross-platform and also works on Red Hat-based distributions. Apr 19, 2017 at 14:53
  • 2
    This is the right answer. If only it rose to the top.
    – user383919
    Sep 13, 2018 at 1:08
  • 1
    Can you edit your answer to explain what the -d flag does? (I've voted for it as the best answer, anyway! Thank you!)
    – Aerendir
    Jun 8, 2019 at 17:40
  • Incorrectly getting gpasswd: user 'me' is not a member of 'me_group'
    – Gulzar
    Jan 26, 2022 at 14:05
1

The easiest and safest way:

sudo -H gedit /etc/group

and delete it manually.

3
  • I see this one enrty. ssh:x:118: What is the meaning of the number 118 here ?
    – ankit7540
    Jan 19, 2017 at 5:09
  • 4
    definitely NOT the safest way!
    – callmebob
    May 9, 2019 at 10:16
  • this worked fo rme, I removed the user from the group and I was good to go. Dec 8, 2019 at 11:34
1

See zanfur's answer.

Edit the /etc/group file as root (i.e. gksudo gedit /etc/group) and remove the username from the line that starts with 'admin'

i.e.

...
admin:x:120:adminuser,adminuser2,userthatshouldnotbeadmin
...

becomes

...
admin:x:120:adminuser,adminuser2
...

Be warned, typos within the file could break your system.

3
  • 15
    Please don't advice to edit these system files manually. In case you screw up your system might become inaccessible.
    – gertvdijk
    Dec 4, 2012 at 16:28
  • Or at least back them up first. (Related information.) Jan 11, 2013 at 0:53
  • 4
    I think you should post this as it is valuable insight into how Linux works. However, should include a warning that there is a much safer way Jan 9, 2014 at 1:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .