useradd

New system user created

Supplementary instructions

useradd command is used to create new system users in Linux. useradd can be used to create user accounts. After the account is created, use passwd to set the password for the account. You can use userdel to delete the account. The account created using the useradd command is actually saved in the /etc/passwd text file.

In Slackware, the adduser command is a script program that uses conversation to obtain input user account information, and then creates a new user through the useradd command that actually creates an account. This makes it easier for administrators to create user accounts. In Red Hat Linux, the adduser command is the symbolic link of the useradd command. The two are actually the same command.

grammar

useradd(option)(parameter)

Options

-b, --base-dir BASE_DIR # If -d HOME_DIR is not specified, the system's default base directory. If this option is not specified, useradd will use the base directory specified by the HOME variable in /etc/default/useradd, or default to /home.
-c, --comment COMMENT # Add comment text. Any text string. It is usually a short description of the login name and is currently used as the field for the user's full name.
-d, --home HOME_DIR # New users will be created using HOME_DIR as the value of the user's login directory.
-D, --defaults # Change defaults.
-e, --expiredate EXPIRE_DATE # The date on which the user account will be disabled. Dates are specified in YYYY-MM-DD format.
-f, --inactive INACTIVE # Number of days after password expiration until account is permanently disabled.
-g, --gid GROUP # The group name or number of the user's initial login group. Group name must exist. The group number must refer to an existing group.
-G, --groups GROUP1[,GROUP2,...[,GROUPN]]] # List of supplementary groups of which the user is also a member. Separate each group with commas, without spaces.
-h, --help # Display help information and exit.
-k, --skel SKEL_DIR # Skeleton directory containing files and directories to be copied in the user's home directory when the home directory is created by useradd.
-K, --key KEY=VALUE # Override /etc/login.defs default values (UID_MIN, UID_MAX, UMASK, PASS_MAX_DAYS, etc.).
-l, --no-log-init # Do not add users to the lastlog and faillog databases.
-m, --create-home # If the user's home directory does not exist, create it.
-M # Do not create a user's home directory even if the system-wide setting in /etc/login.defs (CREATE_HOME) is set to yes.
-N, --no-user-group # Do not create a group with the same name as the user. Instead, add the user to the group specified by the -g option or the GROUP variable in /etc/default/useradd.
-o, --non-unique # Allow creation of user accounts with duplicate (non-unique) UIDs. This option is only effective in combination with the -o option.
-p, --password PASSWORD # The encrypted password returned by crypt(3). The default is to disable passwords.
-r, --system # Create a system account.
-s, --shell SHELL #The name of the user login shell.
-u, --uid UID # Numeric value of user ID.
-U, --user-group # Create a group with the same name as the user and add the user to the group.
-Z, --selinux-user SEUSER # SELinux user for user login. This field is left blank by default, which causes the system to select the default SELinux user.

# Change default value
# When called with only the -D option, useradd will display the current default value. When called with -D and other options, useradd will update the default values for the specified options. Valid default change options are:

Parameters

Username: The username to create.

Exit value

The useradd command exits with the following value:

0 success
1 Unable to update password file
2 Invalid command syntax
Invalid argument for option 3
4 UID is already in use (and without -o)
6 The specified group does not exist
9 username already taken
10 Unable to update group file
12 Unable to create home directory
13 Unable to create mail spool
14 Unable to update SELinux user mapping

document

/etc/passwd # User account information.
/etc/shadow # Protect user account information.
/etc/group # Group account information.
/etc/gshadow #Protect group account information.
/etc/default/useradd # Default value for account creation.
/etc/skel/ # Directory containing default files.
/etc/login.defs # Shadow cipher suite configuration.

Example

Add a new user to the group:

useradd –g sales jack –G company,employees # -g: Join the primary group, -G: Join the secondary group

Create a new user account and set the ID:

useradd caojh -u 544

It should be noted that when setting the ID value, try to be greater than 500 to avoid conflicts. Because some special users will be created after Linux is installed, values between 0 and 499 are generally reserved for system accounts such as bin and mail.

Create a new ordinary user:

useraddlutixia

Create a new system user. System users are generally used to manage services and do not require login, so assign nologin to restrict their login to the system:

useradd -r -s /sbin/nologin mq

Modify the default parameters for creating a user and set the inactivity time after the password expires to be permanently disabled to 30 days:

useradd -D -f 30