I am building a Samba-based server as the primary domain controller and have come across a cute little problem that had to be resolved many times. But a series of searches did not give a result. I need to remove an existing user from an existing group using the command line script. It seems that usermod easily allows you to add a user to an additional group with this command:
usermod -a -G supgroup1,supgroup2 username
Without the “-a” option, if the user is currently a member of a group that is not listed, the user will be removed from the group. Does anyone have a perl (or Python) script that allows you to specify the user and group to delete? Am I missing an obvious existing team or a known solution? Thanks in advance!
Thanks to JJ for a pointer to the Unix :: Group module, which is part of the Unix-ConfigFile. It appears that the deluser command will do what I want, but was not in any of the existing repositories. I went ahead and wrote a perl script using Unix: Group Module. Here is a script for your sysadmining fun.
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;
}
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;
}
$login=$ARGV[0];
$group=$ARGV[1];
use Unix::GroupFile;
$grp = new Unix::GroupFile "/etc/group";
$grp->remove_user("$group", "$login");
$grp->commit();
undef $grp;
exit 0;
source
share