Gentoo Wiki


This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other

Unless I am mistaken, in unix, there is no 'standard' easy way to add a bunch of users to the same group. Yes, you can find graphical tools to do it, but it would be nice just to have a small script in your bag of tricks too, don't you think?

Here is one:

Code: adduserstogroup

 # adduserstogroup  -- adds a bunch of users to a single group.
 # Ten-minute hack by Michael Lockhart,
 # for use by sysadmin.
 #  usage:  adduserstogroup grp usr1 usr2 ... usrn
 #  will add usr1...usrn to group grp
 # AUTHORS:  Michael Lockhart  (MJL)
 #           Thomas Margreiter (T M)
 # Version:  0.2
 # Copyright 2005, Michael Lockhart.  You may use, copy, modify and distribute
 # this script under the terms of the GPL, ver. 2.0.  For details of the
 # GPL, please visit
 # NO WARRANTY.  This script is provided "as is" with no warranty.  If it
 # breaks your system, you get to keep all of the pieces.
 # MJL20050702 - Created.
 # MJL20050704 - Added a check that the group actually exists.
 # T M20051124 - Changed the script for working with RedHat Releases
 if [ $# -ge 2 ]; then
   if [ $UID == 0 ]; then
     egrep ^$1 /etc/group > /dev/null
     if [ $? == 0 ]; then
       while [ $# -gt 0 ]; do
         echo $GROUPNAME `groups $CURRENT` |sed 's/.*: //g' | sed 's/ /,/g' | usermod -G `cat -`,$GROUPNAME $CURRENT
       echo "the group $1 does not exist."
     echo "you must be ROOT to run this script."
   echo "usage:  $0 grp usr1 [usr2 ... usrN]"

Now you can add a bunch of users to a single group. This might be handy, say, when you add a new group to control a device or system service (cdrom, or ppp come to mind...). For example, this is how you would create a group called "ppp" and add existing users to it:

 # groupadd ppp
 # adduserstogroup ppp mjl jen esr bob joe


Retrieved from ""

Last modified: Fri, 05 Sep 2008 23:20:00 +0000 Hits: 17,203