?
Solved

removal of users from a group on Linux

Posted on 2012-09-05
4
Medium Priority
?
339 Views
Last Modified: 2012-11-28
I have some users on a linux host that have local users and local groups.
I need to remove some members from the groups file. Some members exist in the password file and some not.   I have a file that tells me which members have to be deleted from which group but it's a huge file. Don't know how to automate this.

Here is a sample of the file

W:user1 group1
w:user2 group1
w:user3 group1
w:user4 group1
....
....
...
...
w:user5 group2
w:user6 group2
w:user6 group2
.....
w:user7 group3
w:user8 group4


...
....
0
Comment
Question by:lolaferrari
  • 2
4 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 38368756
First you need to find the groups a user is in.  Then you have to take away the group the user should be removed from. Then you have to modify the user using usermod command to contain lesser number of secondary groups.

http://www.cyberciti.biz/faq/howto-linux-remove-user-from-group/
0
 
LVL 20

Expert Comment

by:simon3270
ID: 38368842
It might be easier to edit the /etc/groups file directly.  There is often a "vigr" comand to open the group file and lock it, but if you are sure that nobody else will be editing the file, you could use the following script:

sed 's/^[Ww]://' file_with_groups | while read usr grp; do
  sed -i -e "/^${grp}:/s/\([:,]\)${usr},/\1/" -e "/^${grp}:/s/:${usr}\$/:/"  -e "/^${grp}:/s/,${usr}\$//" /etc/group
done

Open in new window


This assumes that the "w:" at the start of the input file is not supposed to be part of the user name.  It also allows for upper- and lower-case w.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 38371300
There is "groupmod" in Linux:

awk -F: '{print $2}' file_with_members_to_delete|while read user group
    do
       echo groupmod -R $user $group
    done

I put echo in front of "groupmod" so the script does just a "dry run".
Remove it to actually perform the operations.
0
 
LVL 20

Expert Comment

by:simon3270
ID: 38371773
Hmm, can't find "groupmod -R" in Ubuntu or RedHat.

On RedHat, you can do:
    groupmems --group $group --delete $user
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question