Posted :
useradd is used to create a new user account , or to update the default options of creating a user .
When creating a user , using useradd , the user is assigned a group , based on the specified options , or on the default system options .
on
the command line ,
useradd ,
will check the /etc/logins.defs
file ,
for the variable named :
USERGROUPS_ENAB
.
@root:~$ cat /etc/login.defs | grep USERGROUPS_ENAB # grep the USERGROUPS_ENAB variable , # from : /etc/login.defs USERGROUPS_ENAB yes # In this /etc/login.defs file , # USERGROUPS_ENAB value , is set to : # yes
If USERGROUPS_ENAB
is set to yes ,
then useradd , will create a group , for that user ,
which has a name , same as , the user : username . If such group already exists , then
the useradd command will fail .
If
USERGROUPS_ENAB
is set to no ,
then useradd , will check the value , of
the variable :
GROUP
, defined inside :
/etc/default/useradd
@root:~$ cat /etc/default/useradd | grep GROUP # grep the value of GROUP # from /etc/default/useradd # GROUP=100 # The value GROUP is commented # out .
If
GROUP
is
defined and not commented out , then useradd ,
will add the user , to the group , specified by :
GROUP
.
The GROUP
specified in /etc/default/useradd
must exist , and it can be either : a number , or a name .
If GROUP
is
not defined , or is commented out , then useradd , will
assign the user , to the group with the group id of : 100
.
The group , with the group id of 100
, has a name of : users
.
USER_GROUP
, or the --gid USER_GROUP_ID
, options are used
then the user , is assigned to the specified USER_GROUP
or USER_GROUP_ID
.
USER_GROUP
must be :
a group id , or a group name , which exists . And USER_GROUP_ID
must be a group id which exists .
For example :
@root:~$ /sbin/useradd -g 100 user-1 # Create a user account , with the # username of : user-1 # Using the -g option , assign user-1 to # the group , with the group id of : 100 @root:~$ groups user-1 # Print the groups , assigned to : # user-1 user-1 : users # user-1 is assigned to the group : # usersIf the -N , or --no-user-group , options are used
and the
-g USER_GROUP
, or the --gid USER_GROUP_ID
are not used ,
then the
/etc/logins.defs
file , is not checked , for the variable
USERGROUPS_ENAB
,
but instead ,
only the /etc/default/useradd
file , is
checked for the existence of the variable : GROUP
.
If the variable GROUP
is defined , then the user
will be assigned , to the group defined , in the
GROUP
variable . The group defined in GROUP
,
must exist , it will not be created .
If GROUP
is not defined ,
then the user will be assigned to the default group ,
with an id of : 100
.
If the -N
or --no-user-group
options are used ,
with the -g USER_GROUP
or the --gid USER_GROUP_ID
options , then
the value defined in USER_GROUP
or USER_GROUP_ID
, will be assigned to the user ,
as its group . The group in USER_GROUP
or USER_GROUP_ID
, must exist , it will not be created .
@root:~$ /sbin/useradd -N -g daemon daemon-1 # Both the -N and -g options # are used . # The -g option assign the group # daemon , to the user daemon-1 . # The -N option is ignored . @root:~$ groups daemon-1 # Print the user account : daemon-1 , groups daemon-1 : daemon # daemon-1 is assigned to the group daemon @root:~$ cat /etc/login.defs | grep USERGROUPS_ENAB # grep the USERGROUPS_ENAB variable , # from : /etc/login.defs USERGROUPS_ENAB yes # USERGROUPS_ENAB is set to yes , as such , # if no group options are specified when # creating a user account , then the # user account , will be assigned to a # group , with the same name , as the # user account username . The group # will be created , and must not exist . # If it exists , useradd will fail . @root:~$ cat /etc/default/useradd | grep GROUP GROUP=root # The group variable in /etc/default/useradd # has a value of : root @root:~$ /sbin/useradd -N user-2 # The -N option is used , as such /etc/login.defs # is ignored , and /etc/default/useradd is # consulted for the existence , of the value # GROUP . GROUP is set to : root , # as such user-2 will be assigned to the group # root . The group root must exist , and will # not be created . @root:~$ groups user-2 # Print the user account : user-2 , groups user-2 : root # user-2 is assigned to the group root
The value GROUP
inside the /etc/default/useradd
file , can be set by using :
/sbin/useradd -D -g USER_GROUP # USER_GROUP can be a group name or id that exists . /sbin/useradd -D --gid USER_GROUP_ID # USER_GROUP_ID can be a group id , which exists . # For example @root:~$ /sbin/useradd -D -g 100 # Set the GROUP variable inside # /etc/default/useradd , to the # value of 100 . @root:~$ /sbin/useradd -D # Display the default options , # used by useradd GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/sh SKEL=/etc/skel CREATE_MAIL_SPOOL=noIf the -U , or --user-group , options are used
then a group , with the same user : username , will be created , and assigned to the user . The group must not exist , or the useradd command will fail .
The
-U
or --user-group
options ,
cannot be used with the -N
or --no-user-group
options , and
cannot be used with
the -g USER_GROUP
or the --gid USER_GROUP_ID
options .
@root:~$ /sbin/useradd -U user-3 # Create a user account , with the # username of : user-3 # -U is used , as such a group # user-3 will be created , and # assigned to the user : user-3 @root:~$ groups user-3 # Print the groups , assigned to : # user-3 user-3 : user-3 # user-3 is assigned to the group : # user-3