Linux Commands – useradd

Linux Commands – useradd

Linux Commands – useradd

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed groupmod command in Linux which is used to modify an existing group attribute.

https://cloudaffaire.com/linux-commands-groupmod/

In this blog post, we will discuss useradd command in Linux. useradd command is used to create a new user in Linux. 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 may also create the new user’s home directory and copy initial files. User name can be up to 32 characters long and you need root or sudo privileges to create a user.

User Configurations:

  • 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 overridden 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. The default value for GID_MIN (resp. GID_MAX) is 1000 (resp. 60000).
  • 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.
  • 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.
  • PASS_MAX_DAYS (number): The maximum number of days a password may 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).
  • 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.
  • SYS_GID_MAX (number), SYS_GID_MIN (number): Range of group IDs used for the creation of system groups by useradd, groupadd, or newusers. The default value for SYS_GID_MIN (resp. SYS_GID_MAX) is 101 (resp. GID_MIN-1).
  • SYS_UID_MAX (number), SYS_UID_MIN (number): Range of user IDs used for the creation of system users by useradd or newusers. The default value for SYS_UID_MIN (resp. SYS_UID_MAX) is 101 (resp. UID_MIN-1).
  • UID_MAX (number), UID_MIN (number): Range of user IDs used for the creation of regular users by useradd or newusers. The default value for UID_MIN (resp. UID_MAX) is 1000 (resp. 60000).
  • UMASK (number): The file mode creation mask is initialized to this value. If not specified, the mask will be initialized to 022.
  • 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.

User Files:

  • /etc/passwd: User account information.
  • /etc/shadow: Secure user account information.
  • /etc/group: Group account information.
  • /etc/gshadow: Secure group account information.
  • /etc/default/useradd: Default values for account creation.
  • /etc/skel/: Directory containing default files.
  • /etc/login.defs: Shadow password suite configuration.

Linux Commands – useradd:

You can use useradd command to create a new user.

You can use useradd -p or –password PASSWORD option to set the user login credentials. The default is to disable the password.

Note: This option is not recommended because the password (or encrypted password) will be visible by users listing the processes. Instead set the user password using passwd command.

You can use useradd -d or –home-dir HOME_DIR options to create a custom home directory (HOME_DIR) for the user. By default, the user’s home directory is created as /home/user_name.

You can use useradd -b or –base-dir BASE_DIR options to create a custom base directory for the user if -d HOME_DIR is not specified. BASE_DIR is concatenated with the account name to define the home directory. The BASE_DIR must exist otherwise the home directory cannot be created. If this option is not specified, useradd will use the base directory specified by the HOME variable in /etc/default/useradd, or /home by default.

You can use useradd -c or –comment COMMENT options to add a short description of the user.

You can use useradd -e or –expiredate EXPIRE_DATE options to define the date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD. If not specified, useradd will use the default expiry date specified by the EXPIRE variable in /etc/default/useradd, or an empty string (no expiry) by default.

You can use useradd -f or –inactive INACTIVE options to define the number of days after a password expires until the account is permanently disabled. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature. If not specified, useradd will use the default inactivity period specified by the INACTIVE variable in /etc/default/useradd, or -1 by default.

You can use useradd -s or –shell SHELL options to define the name of the user’s login shell. The default is to leave this field blank, which causes the system to select the default login shell specified by the SHELL variable in /etc/default/useradd, or an empty string by default.

You can use useradd -r or –system options to create a system account. There is no difference between a system user and a normal user, only system users will be created with no aging information in /etc/shadow, and their numeric identifiers are chosen in the SYS_UID_MIN-SYS_UID_MAX range, defined in /etc/login.defs, instead of UID_MIN-UID_MAX (and their GID counterparts for the creation of groups). Also, for system user useradd will not create a home directory, regardless of the default setting in /etc/login.defs (CREATE_HOME). You have to specify the -m options if you want a home directory for a system account to be created.

You can use useradd -m or –create-home options to create the user’s home directory if it does not exist. The files and directories contained in the skeleton directory (which can be defined with the -k option) will be copied to the home directory. By default, if this option is not specified and CREATE_HOME is not enabled, no home directories are created.

You can use useradd -k or –skel SKEL_DIR options to copy the content of skeleton directory to user’s home directory. This option is only valid if the -m (or –create-home) option is specified. If this option is not set, the skeleton directory is defined by the SKEL variable in /etc/default/useradd or, by default, /etc/skel.

You can use useradd -M or –no-create-home options to disable home directory creation even if the system wide setting from /etc/login.defs (CREATE_HOME) is set to yes.

You can use useradd -u or –uid UID options to provide custom user id. This value must be unique and non-negative, unless the -o option is used. The default is to use the smallest ID value greater than or equal to UID_MIN and greater than every other user.

You can use useradd -o or –non-unique options to allow the creation of a user account with a duplicate (non-unique) UID. This option is only valid in combination with the -u option.

You can use useradd -K or –key KEY=VALUE options to overrides /etc/login.defs defaults (UID_MIN, UID_MAX, UMASK, PASS_MAX_DAYS and others).

You can use useradd -U or –user-group options to create a group with the same name as the user, and add the user to this group. The default behavior (if the -g, -N, and -U options are not specified) is defined by the USERGROUPS_ENAB variable in /etc/login.defs.

You can use useradd -g or –gid GROUP options to add the user to an existing group. -g options accept both group name or number of the user’s initial login group. If not specified, the behavior of useradd will depend on the USERGROUPS_ENAB variable in /etc/login.defs. If this variable is set to yes (or -U/–user-group is specified on the command line), a group will be created for the user, with the same name as her loginname. If the variable is set to no (or -N/–no-user-group is specified on the command line), useradd will set the primary group of the new user to the value specified by the GROUP variable in /etc/default/useradd, or 100 by default.

You can use useradd -G or –groups GROUP1[,GROUP2,…[,GROUPN]]] options to add the user to multiple secondary group during user creation. All the groups must pre-exist and needs to be separated from the next by a comma, with no intervening whitespace.

You can use useradd -N or –no-user-group options to not create a group with the same name as the user, but add the user to the group specified by the -g option or by the GROUP variable in /etc/default/useradd. The default behavior (if the -g, -N, and -U options are not specified) is defined by the USERGROUPS_ENAB variable in /etc/login.defs.

You can use useradd -D or –defaults options to update the default user configurations defined in /etc/default/useradd file. You can change below default configurations using -D option

  • -b, –base-dir BASE_DIR: The path prefix for a new user’s home directory.
  • -e, –expiredate EXPIRE_DATE: The date on which the user account is disabled.
  • -f, –inactive INACTIVE: The number of days after a password has expired before the account will be disabled.
  • -g, –gid GROUP: The group name or ID for a new user’s initial group.
  • -s, –shell SHELL: The name of a new user’s login shell.

Hope you have enjoyed this article. In the next blog post, we will discuss passwd command in Linux.

 

Leave a Reply

Close Menu