removal of users from a group on Linux

Posted on 2012-09-05
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

Question by:lolaferrari
    LVL 31

    Expert Comment

    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.
    LVL 19

    Expert Comment

    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

    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.
    LVL 68

    Accepted Solution

    There is "groupmod" in Linux:

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

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

    Expert Comment

    Hmm, can't find "groupmod -R" in Ubuntu or RedHat.

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

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    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…
    Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now