I'm trying to add a list of users to different groups on a debian VM. I am using the following sequence of commands in my bash script:

getent group $uid || groupadd $uid                        #add group before adding user
useradd -s $shell -m -d $users_directory -g $uid $uname   #add user with group id $uid

However running this prints out the following:

groupadd: group '5067' already exists
useradd: group '5067' does not exist

looking at my groups with cut-d: -f1 /etc/group I see that '5067' exists there, but when I run groups all I see is 'root' as the lone group.

Any tips?

  • No linux handy, but a group has a name and a gid, and having an all-digit name is likely to confuse programs that want to take a "name-or-gid" as parameter. (I'm not convinced groupadd $uid will create a group with gid $uid and not a group with name $uid.) – Ulrich Schwarz Oct 13 '16 at 15:14

Calling groupadd with a numeric group id is going to create a group named 5067. E.g.:

# groupadd 5067
# grep 5067 /etc/group

When you pass a numeric identifier to useradd -g ..., it looks only at group ids. So even though a group named 5067 exists:

# useradd -g 5067 testuser
useradd: group '5067' does not exist

I suspect this is the root of your problem.

That you don't see the new group when running groups is normal. Any new groups you've added a user to won't take effect until that user logs in. If the user is already logged in, they need to log out and log back in (os start a new login shell). That's why you don't see the new group in the output of groups although you do see it in /etc/group.

| improve this answer | |
  • I added the bit about groups not working to your answer instead of writing my own since you'd already done a better job of explaining the issue with the numerical ids. – terdon Oct 13 '16 at 15:49

re: the groups command showing only root.

If you simply type groups, you get the response for your uid. You need to use groups $uname to see the groups for that user.

Also, be aware that useradd may create a new group for the user with the same name. You can use -N to prevent that. Look in /etc/login.defs and /etc/default/useradd to check on option defaults.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.