Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 107319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:44:07+00:00 2026-05-11T01:44:07+00:00

I am putting together a Samba-based server as a Primary Domain Controller, and ran

  • 0

I am putting together a Samba-based server as a Primary Domain Controller, and ran into a cute little problem that should have been solved many times over. But a number of searches did not yield a result. I need to be able to remove an existing user from an existing group with a command line script. It appears that the usermod easily allows me to add a user to a supplementary group with this command:

usermod -a -G supgroup1,supgroup2 username 

Without the ‘-a’ option, if the user is currently a member of a group which is not listed, the user will be removed from the group. Does anyone have a perl (or Python) script that allows the specification of a user and group for removal? Am I missing an obvious existing command, or well-known solution forthis? Thanks in advance!

Thanks to J.J. for the pointer to the Unix::Group module, which is part of Unix-ConfigFile. It looks like the command deluser would do what I want, but was not in any of my existing repositories. I went ahead and wrote the perl script using the Unix:Group Module. Here is the script for your sysadmining pleasure.

#!/usr/bin/perl # # Usage:   removegroup.pl login group # Purpose: Removes a user from a group while retaining current primary and #          supplementary groups. # Notes:   There is a Debian specific utility that can do this called deluser, #          but I did not want any cross-distribution dependencies # # Date:   25 September 2008  # Validate Arguments (correct number, format etc.) if ( ($#ARGV < 1) || (2 < $#ARGV) ) {   print '\nUsage: removegroup.pl login group\n\n';   print 'EXIT VALUES\n';   print '     The removeuser.pl script exits with the following values:\n\n';   print '     0 success\n\n';   print '     1 Invalid number of arguments\n\n';   print '     2 Login or Group name supplied greater than 16 characters\n\n';   print '     3 Login and/or Group name contains invalid characters\n\n';   exit 1; }  # Check for well formed group and login names if ((16 < length($ARGV[0])) ||(16 < length($ARGV[1]))) {   print 'Usage: removegroup.pl login group\n';   print 'ERROR: Login and Group names must be less than 16 Characters\n';   exit 2; }  if ( ( $ARGV[0] !~ m{^[a-z_]+[a-z0-9_-]*$}) || ( $ARGV[0] !~ m{^[a-z_]+[a-z0-9_-]*$} ) ) {   print 'Usage: removegroup.pl login group\n';   print 'ERROR: Login and/or Group name contains invalid characters\n';   exit 3; }  # Set some variables for readability $login=$ARGV[0]; $group=$ARGV[1];  # Requires the GroupFile interface from perl-Unix-Configfile use Unix::GroupFile;  $grp = new Unix::GroupFile '/etc/group'; $grp->remove_user('$group', '$login'); $grp->commit(); undef $grp; exit 0; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:44:08+00:00Added an answer on May 11, 2026 at 1:44 am

    Web Link: http://www.ibm.com/developerworks/linux/library/l-roadmap4/

    To add members to the group, use the gpasswd command with the -a switch and the user id you wish to add:

    gpasswd -a userid mygroup

    Remove users from a group with the same command, but a -d switch rather than -a:

    gpasswd -d userid mygroup

    ‘man gpasswd’ for more info…

    I looked for ages to find this. Sometimes it takes too much effort not to reinvent the wheel…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Using the rewrite engine is a pretty heavyweight way to… May 12, 2026 at 12:10 am
  • Editorial Team
    Editorial Team added an answer You may be looking for something like this: #if DEBUG… May 12, 2026 at 12:10 am
  • Editorial Team
    Editorial Team added an answer I've asked the same question in the DevBUZZ foruns, and… May 12, 2026 at 12:10 am

Related Questions

I am putting together a proposal for a large multinational company for our licenced
I am putting together a build system and wanted to know if there is
I am putting together a REST API and as I'm unsure how it will
I am putting together a simple simulated network in java, where i can add

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.