Table of Contents
Useradd adds a new user to the /etc/passwd and /etc/shadow and /etc/userattr files. The -A and -P options respectively assign authorizations and profiles to the user. The -R option assigns roles to a user. Useradd also creates supplementary group memberships for the user (-G option) and creates the home directory (-m option. Depending on command line options, the useradd command will update system files and may also create the new user's home directory and copy initial files. By default, a group will also be created for the new user (see -g, -N, -U, and USERGROUPSENAB). The options which apply to the useradd command are: -b, -base-dir BASEDIR. Mac OS X automatically generates a short name for use as your screen and Buddy name in iChat AV and various network applications. The short name is also the name of the folder that Mac OS X creates on the computer’s hard drive for this user. You can keep the default short name or type a new one, but it must not contain any spaces.
Linux useradd Command with Examples
The useradd is a low-level utility used to manage users on the Linux system. By using
useradd
utility you create new users and assign them to the groups. As Linux is a multiuser system it’s important for the administrator to manage tasks like adding user, removing user, assigning a user to the group, etc. In this tutorial, we are going to learn Linux useradd command with examples.Basic Syntax of useradd
Command
![Useradd For Mac Useradd For Mac](/uploads/1/2/6/4/126453527/439957139.png)
When creating a new user with given options using useradd command, It reads
/etc/default/useradd
file for default values and /etc/login.defs
file for password policies, user id ranges etc.Following is the basic syntax for
useradd
command in Linux.Where,
OPTIONS: These are the options used with useradd command specified here.
USERNAME: Name of the user that will be used with this command.
OPTIONS: These are the options used with useradd command specified here.
USERNAME: Name of the user that will be used with this command.
How to Create a New User in Linux System
To only create a new user using
useradd
command you run this command without any options.Run the following command to create a user names
demouser
in your Linux system.After completing the execution of the above command, it will create the new user using default values specified in
/etc/default/useradd
file.Now to make this user login to the system you should set the password to the newly created user. To set the password to
demouser
you can run below command:Now you will be asked to enter the password, enter the password and confirm it when you asked.
How to Create User with Specific User ID
![For For](/uploads/1/2/6/4/126453527/469641941.png)
When we create a user using useradd function user ID will be assigned to the user reading
/etc/login.defs
files. You can also create a new user with specific user id by using -u
option with useradd
command:To create a user with User ID 1401 you can run following command in the terminal:
Confirm and verify User ID with
id
command:The output should be:
How to Create a User With Home Directory
To create a user with home directory you should use
-m
option with useradd
command.Create a user named
demouser2
with home directory run below command:Here the new user will be created with home directory
/home/demouser2
.To confirm the creation of the directory run below command:
Conclusion
In this tutorial, you have learned Linux useradd Command with Examples successfully. If you have any of the queries regarding this then you can comment below.
Create a new user or update default new user information .
When invoked without the -D option, the useradd command creates a new user account using the values specified on the command line plus the default values from the system. Depending on command line options, the useradd command will update system files and can also create the new user's home directory and copy initial files.
By default, a group will also be created for the new user (see -g, -N, -U, and USERGROUPS_ENAB).
By default, a group will also be created for the new user (see -g, -N, -U, and USERGROUPS_ENAB).
Notes
The system administrator is responsible for placing the default user files in the /etc/skel/ directory (or any other skeleton directory specified in /etc/default/useradd or on the command line).
Mac Add User To Group
Caveats
You can not add a user to a NIS or LDAP group. This must be performed on the corresponding server.
Similarly, if the username already exists in an external user database such as NIS or LDAP, useradd will deny the user account creation request.
Usernames can only be up to 32 characters long.
Similarly, if the username already exists in an external user database such as NIS or LDAP, useradd will deny the user account creation request.
Usernames can only be up to 32 characters long.
Configuration
The following configuration variables in /etc/login.defs change the behavior of this tool:
CREATE_HOME (boolean)
Indicate if a home directory should be created by default for new users.
This setting does not apply to system users, and can be overriden on the command line.
CREATE_HOME (boolean)
Indicate if a home directory should be created by default for new users.
This setting does not apply to system users, and can be overriden on the command line.
GID_MAX (number), GID_MIN (number)
Range of group IDs used for the creation of regular groups by useradd, groupadd, or newusers.
Range of group IDs used for the creation of regular groups by useradd, groupadd, or newusers.
MAIL_DIR (string)
The mail spool directory. This is needed to manipulate the mailbox when its corresponding user account is modified or deleted. If not specified, a compile-time default is used.
The mail spool directory. This is needed to manipulate the mailbox when its corresponding user account is modified or deleted. If not specified, a compile-time default is used.
MAIL_FILE (string)
Defines the location of the users mail spool files relatively to their home directory.
The MAIL_DIR and MAIL_FILE variables are used by useradd, usermod, and userdel to create, move, or delete the user's mail spool.
If MAIL_CHECK_ENAB is set to yes, they are also used to define the MAIL environment variable.
MAX_MEMBERS_PER_GROUP (number)
Maximum members per group entry. When the maximum is reached, a new group entry (line) is started in /etc/group (with the same name, same password, and same GID).
The default value is 0, meaning that there are no limits in the number of members in a group.
This feature (split group) permits to limit the length of lines in the group file. This is useful to make sure that lines for NIS groups are not larger than 1024 characters.
If you need to enforce such limit, you can use 25.
Note: split groups might not be supported by all tools (even in the Shadow toolsuite). You should not use this variable unless you really need it.
Defines the location of the users mail spool files relatively to their home directory.
The MAIL_DIR and MAIL_FILE variables are used by useradd, usermod, and userdel to create, move, or delete the user's mail spool.
If MAIL_CHECK_ENAB is set to yes, they are also used to define the MAIL environment variable.
MAX_MEMBERS_PER_GROUP (number)
Maximum members per group entry. When the maximum is reached, a new group entry (line) is started in /etc/group (with the same name, same password, and same GID).
The default value is 0, meaning that there are no limits in the number of members in a group.
This feature (split group) permits to limit the length of lines in the group file. This is useful to make sure that lines for NIS groups are not larger than 1024 characters.
If you need to enforce such limit, you can use 25.
Note: split groups might not be supported by all tools (even in the Shadow toolsuite). You should not use this variable unless you really need it.
PASS_MAX_DAYS (number)
The maximum number of days a password can be used. If the password is older than this, a password change will be forced. If not specified, -1 will be assumed (which disables the restriction).
The maximum number of days a password can be used. If the password is older than this, a password change will be forced. If not specified, -1 will be assumed (which disables the restriction).
PASS_MIN_DAYS (number)
The minimum number of days allowed between password changes. Any password changes attempted sooner than this will be rejected. If not specified, -1 will be assumed (which disables the restriction).
The minimum number of days allowed between password changes. Any password changes attempted sooner than this will be rejected. If not specified, -1 will be assumed (which disables the restriction).
PASS_WARN_AGE (number)
The number of days warning given before a password expires. A zero means warning is given only upon the day of expiration, a negative value means no warning is given. If not specified, no warning will be provided.
The number of days warning given before a password expires. A zero means warning is given only upon the day of expiration, a negative value means no warning is given. If not specified, no warning will be provided.
SYS_GID_MAX (number), SYS_GID_MIN (number)
Range of group IDs used for the creation of system groups by useradd, groupadd, or newusers.
Range of group IDs used for the creation of system groups by useradd, groupadd, or newusers.
SYS_UID_MAX (number), SYS_UID_MIN (number)
Range of user IDs used for the creation of system users by useradd or newusers.
Range of user IDs used for the creation of system users by useradd or newusers.
UID_MAX (number), UID_MIN (number)
Range of user IDs used for the creation of regular users by useradd or newusers.
Range of user IDs used for the creation of regular users by useradd or newusers.
UMASK (number)
The file mode creation mask is initialized to this value. If not specified, the mask will be initialized to 022.
useradd and newusers use this mask to set the mode of the home directory they create
It is also used by login to define users' initial umask. Note that this mask can be overriden by the user's GECOS line (if QUOTAS_ENAB is set) or by the specification of a limit with the K identifier in limits(5).
USERGROUPS_ENAB (boolean)
Enable setting of the umask group bits to be the same as owner bits (examples: 022 -> 002, 077 -> 007) for non-root users, if the uid is the same as gid, and username is the same as the primary group name.
If set to yes, userdel will remove the user's group if it contains no more members, and useradd will create by default a group with the name of the user.
The file mode creation mask is initialized to this value. If not specified, the mask will be initialized to 022.
useradd and newusers use this mask to set the mode of the home directory they create
It is also used by login to define users' initial umask. Note that this mask can be overriden by the user's GECOS line (if QUOTAS_ENAB is set) or by the specification of a limit with the K identifier in limits(5).
USERGROUPS_ENAB (boolean)
Enable setting of the umask group bits to be the same as owner bits (examples: 022 -> 002, 077 -> 007) for non-root users, if the uid is the same as gid, and username is the same as the primary group name.
If set to yes, userdel will remove the user's group if it contains no more members, and useradd will create by default a group with the name of the user.
Files
Exit Values:
Examples
Create a new user and set a password:
$ useradd johng
$ passwd johng
$ passwd johng
Create a new user and set an account expiry date :
$ useradd -e 2021-12-31 johng
Create a new user and set a 30 day password expiry (30 days after the password expires the account will expire unless the password is changed):
$ useradd -f 30 johng
Create a new user and set an account and password expiry:
Create New User In Mac
$ useradd -e 2021-12-31 -f 30 johng
'Surround yourself with people that want more out of life. That won’t settle for average. People that you can connect with on a deeper level. Keep your circle fresh. Keep your circle full of quality rather than quantity. Full of cool ass humans that you can be yourself around. People that fill you up with nothing but love. People that want to see you succeed. People that GET IT. Good circle, good life!” ~ Genereux Philip
Related linux commands:
usermod - Modify user account.
userdel - Delete a user account.
chpasswd - Update passwords in batch mode.
passwd - Modify a user password.
crypt(3)
groupadd - Add a new group.
Equivalent Windows command: New-adUser - Create a new AD user.
userdel - Delete a user account.
chpasswd - Update passwords in batch mode.
passwd - Modify a user password.
crypt(3)
groupadd - Add a new group.
Equivalent Windows command: New-adUser - Create a new AD user.
Copyright © 1999-2020 SS64.com
Some rights reserved
Some rights reserved