Link to home
Start Free TrialLog in
Avatar of lolaferrari
lolaferrariFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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


...
....
Avatar of farzanj
farzanj
Flag of Canada image

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/
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.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmm, can't find "groupmod -R" in Ubuntu or RedHat.

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