Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Being new to Linux administration, I'm a little confused about the following commands:


I've just finished reading the user administration book in the Linux/Unix Administrator's handbook, but some things are still a little hazy.

Basically useradd seems straight forward enough:

useradd -c "David Hilbert" -d /home/math/hilbert -g faculty -G famous -m -s /bin/sh hilbert

I can add "David Hilbert" with username hilbert , setting his default directory, shell, and groups. And I think that -g is his primary/default group and -G are his other groups.

So these are my next questions:

  1. Would this command still work if the groups faculty and famous did not exist? Would it just create them?
  2. If not, what command do I use to create new groups?
  3. If I remove the user hilbert and there are no other users in those groups, will they still exist? Should I remove them?
  4. After I run the useradd command above, how do I remove David from the famous group, and reassign his primary group to hilbert which does not yet exist?
share|improve this question
up vote 15 down vote accepted

The usermod command will allow you to change a user's primary group, supplementary group or a number of other attributes. The -g switch controls the primary group.

For your other questions...

1 - If you specify a group, groupname, that does not exist during the useradd stage, you will receive an error - useradd: unknown group groupname

2 - The groupadd command creates new groups.

3 - The group will remain if you remove all users contained within. You don't necessarily have to remove the empty group.

4 - Create the hilbert group via groupadd hilbert. Then move David's primary group using usermod -g hilbert hilbert. (Please note that the first hilbert is the group name and the second hilbert is the username. This is important in cases, where you are moving a user to a group with a different name)

You may be complicating things a bit here, though. In many Linux distributions, a simple useradd hilbert will create the user hilbert and a group of the same name as the primary. I would add supplementary groups specified together using the -G switch.

share|improve this answer
thanks. if i do usermod -g hilbert hilbert will that remove hilbert's other groups? If so, how would I keep those other groups?If not, wow would I remove him from his other groups? – cwd Dec 15 '11 at 23:06
Test it out and see for your particular environment. – ewwhite Dec 16 '11 at 6:39

You need to read the man usermod which explains what happens with the various options:

usermod -g hilder hilder

will replace your login group from 'faculty' to 'hilder', as long as the group 'hilder' exists. If it doesn't exist then you first need to create it with groupadd.

When you use the -G option you should also use the -a option to append new groups to the current list of supplementary groups that user 'hilder' belongs. Without the -a option you will replace current supplementary groups with a new group set. Therefore use this cautiously.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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