Previous Section  < Day Day Up >  Next Section

Recipe 8.19. Adding Batches of Users to Groups

8.19.1 Problem

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

8.19.2 Solution

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 (as we discussed in Recipe 8.2 with our "Trainers, Coaches, Players" example). 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' ','

bkind,bcool,

8.19.3 Discussion

Here is the complete /etc/passwd fields scheme:

username:passwd:UID:GID:full name,room number,work phone,home phone,other:directory:shell

8.19.4 See Also

  • passwd(5), awk(1)

  • sed & awk Pocket Reference, by Arnold Robbins (O'Reilly)

    Previous Section  < Day Day Up >  Next Section