0

While working on Linux Essentials, I was experimenting with removing a user (me) from a group I created. Following online instructions, I ended up deleting myself from all the default groups and my only membership is now in my own group.

Here are my questions:

  1. How do I remove a user from a specific group? If usermod allows addition using -a -G, there must be a similarly simple way to remove the user from the group.
  2. Is there a command that I can use to restore my group memberships? (i.e., sudo group, xdm group, etc.)
  3. Finally, does manually editing /etc/passwd or /etc/group manipulate group memberships, additions, and deletions?

As mentioned above, I just finished Linux Essentials, so I'm a beginner. Please keep that in mind when giving your input and any instructions that it might include.

0

I'll answer your questions below:

  1. How do I remove a user from a specific group?
    You can use usermod without the -a option and with the -G option, but you'll need to list all the secondary groups you want the user to be a member of. I don't know of a way to remove a user from a group by name.

  2. Is there a command that I can use to restore my group memberships?
    Yes, usermod. You'd use usermod -G <comma_separated_list_of_groups> <username>.

  3. Does manually editing /etc/{passwd,group} manipulate group memberships, additions, and deletions?
    Yes, tools like usermod safely manipulate those files. You can do it manually, but if you make a mistake you might end up with a user that can no longer log in.

1
  • Thank you very much. I really appreciate it. I'll look at Ubuntu's documentation for the default groups to which a user is added and add them back using usermod. – user2425931 Jun 30 '20 at 22:10
0

The most direct answer is yes -> everything happens with just these two files /etc/passwd and /etc/group. Keeping in mind the password for a given account is stored in /etc/shadow.

By default (for example in RHEL/CentOS 7) any new user is created and given their own unique group name (or id number). But not belonging to a group does not outright prevent login. Which is to say I believe new user accounts get their own unique group made when their account is made but they are not part of the generic users group having the typical group id of 100. And things work just fine.

While it may not be recommended, and for valid reasons, you can technically simply manually edit /etc/passwd and /etc/group to create new user accounts and new groups. That is in fact all the commands useradd and usermod technically do.

Where you can have problems is when the user's home account is not owned by that user, so upon login it would typically result in a very simple shell prompt such as $ along with a warning. But the user can still technically log in, you will just lose a lot of functionality taken for granted. A login over the network via SSH usually still works fine. But a login at the console where you have graphics, that will likely bomb out because you can't access your own home folder (and not because of group permissions). Hope that helps.

1
  • Thank you very much!! – user2425931 Jun 30 '20 at 22:11
0

In addition to usermod which Andy Dalton covered, you can also use gpasswd:

gpasswd -d username groupname

That removes the user from the named group. This way is less tedious as you can simply use the username and groupname as arguments instead of all of the secondary groups that are necessary with usermod.

Manually editing /etc/passwd and /etc/group has the same effect but if you are going to do that, use, respectively, the tools vipw -s or vigr -s. They will prevent text errors in the files that can possible render it impossible for anyone to log in. Don't edit them with a text editor such as vim for that very reason.

1
  • Thank you very much!! That makes life - in Linux - easier. – user2425931 Jun 30 '20 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.