On Centos 6.5 (and probably any) linux, I can create a group:

sudo groupadd mygroup

and add several users to it:

sudo usermod -a -G mygroup userA
sudo usermod -a -G mygroup userB
sudo usermod -a -G mygroup userC

The number of users in my particular case is 20. How can I use a one-liner like:

sudo usermod -a -G mygroup userA userB userC

Even using for loop would be fine, but I'm not a bash specialist, so wondering.


5 Answers 5


If gpasswd is available (should be on most distributions except e.g. Solaris) you can provide a comma separated list of users followed by the group name:

gpasswd -M userA,userB,userC mygroup
  • 2
    This one should be the answer.
    – ivanleoncz
    Aug 14, 2017 at 19:14
  • 9
    Keep in mind that this solution overwrites the existing group members list. This is a different thing than "adding" users like usermod -aG mygroup does.
    – Michael
    Oct 4, 2018 at 17:33
for user in userA userB userC; do sudo usermod -a -G mygroup "$user"; done
  • Strange that being as root this command errors out without sudo "-bash: syntax error near unexpected token usermod'"`
    – jedi
    Apr 3, 2022 at 16:46
  • you're probably missing a 'do' or a ';'
    – Masterfool
    May 30, 2022 at 6:44

This is a rudimental script with positional parameters, save the code below in a file called fill_group.sh and make it executable with chmod +x fill_group.sh.

Then add it to a PATH directory (/usr/local/bin) or execute it in the same dir where it's located with ./fill_group.sh <group_name> <user1> ... <userN>.

# Name: fill_group.sh
# Usage: 
# fill_group.sh <group_name> <user1> <user2> ... <userN>
# Description:
# add users to specific group passed as first parameter. 
# If the group doesn't exists add it to the system.
set -eu

# Exit if there is not at least 2 args (group,user1)
if [ $# -lt 2  ]; then exit 5; fi

group="${1}"; shift # extract group from arguments
if ! egrep --quiet "^${group}:" /etc/group; then
  sudo groupadd "${group}"

for user in "${@}"; do
    sudo usermod -a -G "${group}" "${user}"
  • 2
    Another way to validate the group: if ! getent group "$group" >/dev/null Dec 8, 2015 at 16:44
  • @glennjackman: thank you, i missed getent command till now (in my system there is also a nice autocomplete feature for all the databases inspected). Dec 8, 2015 at 16:57
  • Too complicated, I would wish to execute it from the command line.
    – Hordon Freeman
    Dec 8, 2015 at 18:00
  • @HordonFreeman: the script when saved is executed as a oneliner: fill_group.sh <group> <user1> ... <userN>. If you want really a oneliner: for user in "userA userB userC"; do sudo usermod -a -G "groupName" "${user}"; done or if you have a the user list one per line in a file called users.list: group=mygroup && while read user; do sudo usermod -a -G "${group}" "${user}"; done < users.list Dec 8, 2015 at 18:12

On Fedora, Red Hat, CentOS and other downstream distributions, the newusers utility enables you to very easily add a batch of new users to a system without having to write s Bash script.

May also be available on Debian and Ubuntu.


Regarding Tombart's and Masterfool's answers. For old distos (SLES 11 SP1), the shadow tools are slightly different. The manuals give no version number, but are dated 2009-2010.

The -M option in gpasswd doesn't exist, (but needed gpasswd for something else). So need to go for the loop option by Masterfool. But here -a does not exist, instead -A behaves as -a & -G.

I'm doing this to rollout access to load's of systems and my complete "create group and populate" so far looks like:

/usr/sbin/groupadd -g GID### mygroup
gpasswd -r mygroup
for user in userA userB userC; do sudo usermod -A mygroup "$user"; done

So, create group with set GID, unpassword/activate the new group, add some users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy