Adding Batches of Users to Groups

You need to add a whole bunch of users to a group.

Paste your list of login names directly into /etc/group.

Here’s a quick way to generate a list of users to add to /etc/group. This depends on having a UID numbering scheme already in place, so that you can easily sort out selected groups of users. Let’s add some Trainers to a group:

$ awk -F: ‘($3 >= 1050) && ($3 <=1060) { print $1}' /etc/passwd | tr '\n' ',' bcool,bkind,frnow,kthxbye,oknodo,

Now copy and paste into /etc/group.

What if you do not have a nice, tidy, organized UID scheme? This is where the GECOS fields come in handy. Go back and pick one for entering some kind of label. The “other” field is best, because users are blocked from changing it. Where is this “other” field? It’s inside the “full name,” or GECOS, field which contains five comma-delimited fields. It looks like this:

bcool:x:1300:1300:Bob Cool,,,,trainer:/home/bkind:/bin/bash
bkind:x:1055:1055:Bev Kind,,,,trainer:/home/bkind:/bin/bash

Once you’ve added the labels, grep and awk can easily fetch these users for you:

$ cat /etc/passwd | grep trainer | awk -F: ‘{ print $1}’ | tr ‘\n’ ‘,’