removal of users from a group on Linux

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


...
....
lolaferrariAsked:
Who is Participating?
 
woolmilkporcConnect With a Mentor Commented:
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
 
farzanjCommented:
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
 
simon3270Commented:
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
 
simon3270Commented:
Hmm, can't find "groupmod -R" in Ubuntu or RedHat.

On RedHat, you can do:
    groupmems --group $group --delete $user
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.