lolaferrari
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
...
....
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
...
....
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:
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.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hmm, can't find "groupmod -R" in Ubuntu or RedHat.
On RedHat, you can do:
groupmems --group $group --delete $user
On RedHat, you can do:
groupmems --group $group --delete $user
http://www.cyberciti.biz/faq/howto-linux-remove-user-from-group/