Being new to Linux administration, I'm a little confused about the following commands:
useradd
usermod
groupadd
groupmod
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:
- Would this command still work if the groups
faculty
andfamous
did not exist? Would it just create them? - If not, what command do I use to create new groups?
- If I remove the user
hilbert
and there are no other users in those groups, will they still exist? Should I remove them? - After I run the
useradd
command above, how do I remove David from thefamous
group, and reassign his primary group tohilbert
which does not yet exist?
Best Answer
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...
If you specify a group,
groupname
, that does not exist during theuseradd
stage, you will receive an error - useradd: unknown group groupnameThe
groupadd
command creates new groups.The group will remain if you remove all users contained within. You don't necessarily have to remove the empty group.
Create the
hilbert
group viagroupadd hilbert
. Then move David's primary group usingusermod -g hilbert hilbert
. (Please note that the firsthilbert
is the group name and the secondhilbert
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 userhilbert
and a group of the same name as the primary. I would add supplementary groups specified together using the-G
switch.