Linux command (22) useradd command

useradd command

  • Function Description: add user or update default new user information

  • Usage: useradd [options] login_name

    option explain
    -u,--uid UID When adding a new user, specify the UID number manually. The default is the UID+1 of the previous user, the UID usage range (CentOS6) 500 +, (CentOS7) 1000+
    -g,--gid group_name When adding a new user, manually specify the basic group to which the user belongs, which can be the group name or GID, but this means that a private group with the same name as the user name will not be created for him. Note: this group needs to exist in advance or there will be an error when it is created.
    -c,--comment COMMENT Indicates comment information, usually enclosed in quotation marks if there are spaces
    -G,--groupsGROUP1[,GROUP2,...[,GROUPN]]] Indicates the additional group to which the user belongs. Multiple groups are separated by commas. Note: groups must exist in advance.
    -d,--home HOME_DIR Add a new user's home directory with the specified path. Create by copying / etc/skel and renaming the directory. If the specified home directory path exists in advance, the environment configuration file will not be copied for the user. Therefore, when creating a user specified home directory, the target path is best not to exist. If not specified, a directory with the same name as its user name will be created in / home by default.
    -s,--shell SHELL Indicates the shell used by the user, and the list of all normal shells that can be used is stored in the / etc/shells file
    -r,--system Create a system user
    -M Do not create user home directory. Even in / etc/login.defs The create home directory is specified in the configuration file and will not be created with - M. /etc/login.defs This file indicates the default configuration information when the user is created
    -D,--defaults Display the default configuration property information of the created user
    -D [options] Modify the value of the specified option
    -e,--expiredate Specify the expiration date of the account in the format of YYYY-MM-DD

Example 1: add a user named user1

[root@node1 ~]# useradd user1
[root@node1 ~]# tail -1 /etc/passwd

Note: when creating a user, if no group is specified, a group with the same name as the user name will be automatically created as the user's private group, and only one user will be included.

[root@node1 ~]# tail -1 /etc/shadow

The second field above is the password segment, which is displayed as "!", indicating the disabled status. Because we have not set the password after adding the user, the Linux system prohibits the login of the empty password user.

Example 2: add an openstack user with a UID of 1500

[root@node1 ~]# useradd -u 1500 openstack
[root@node1 ~]# tail -1 /etc/passwd

Example 3: add a user as clouddocker and specify the basic group as cloud

[root@node1 ~]# useradd -g cloud cloudsocker
useradd: group 'cloud' does not exist

Note: when adding users, you need to specify the basic group of users manually, and this group must exist in advance. Otherwise, you will be prompted that this group does not exist.

Example 4: add a user as nfs, and specify the annotation information as "network file server"

[root@node1 ~]# useradd -c "network file server" nfs
[root@node1 ~]# tail -1 /etc/passwd
nfs:x:1501:1501:network file server:/home/nfs:/bin/bash

Example 5: add a user named trump and specify its home directory as / tmp/trump

[root@node1 ~]# useradd -d /tmp/trump trump
[root@node1 ~]# tail -1 /etc/passwd
[root@node1 ~]# ls -a /tmp/trump
.  ..  .bash_logout  .bash_profile  .bashrc

In order to initialize the user environment, the system usually creates a home directory by copying the / etc/skel directory and renaming it to the name of the home directory specified by the user instead of creating an empty directory directly.

Example 6: the mytest directory exists in the / tmp directory in advance. When adding the mytest user, specify the / tmp/mytest directory

[root@node1 ~]# mkdir -p /tmp/mytest
[root@node1 ~]# useradd -d /tmp/mytest mytest
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
[root@node1 ~]# ls -a /tmp/mytest/
.  ..
Tip: this directory already exists and does not copy any files from the / etc/skel directory to / tmp/mytest, so the user's environment initialization profile will not exist in the / tmp/mytest directory.

Example 7: how to view all available shell s of the current system

[root@node1 ~]# cat /etc/shells 

Example 8: add a user named obama and indicate that the shell type used is zsh

[root@node1 ~]# useradd -s /bin/zsh obama
[root@node1 ~]# tail -1 /etc/passwd
//Now obama's shell has been set to / bin/zsh

Example 9: display the default configuration property information of the created user

[root@node1 ~]# useradd -D
GROUP=100				# Do you need to create a private group with the same name as the user when creating the user
HOME=/home				# The starting place of home directory when creating a user. Create a home directory with the same name as the user name
INACTIVE=-1				# Specify how many days after the password expires to close the account. The default value is - 1, which means it is not disabled
EXPIRE=					# Account expiration time. If it is blank, it means it will never expire
SHELL=/bin/bash			# shell used by default when creating users	
SKEL=/etc/skel			# Which file does the user's skeleton information copy from by default when creating users	
CREATE_MAIL_SPOOL=yes	# When creating a user, do you want to create a mail cache queue for the user? If yes, a mailbox with the same name as the user name will be created in the / var/spool/mail directory by default for receiving mail
//In fact, the results modified by useradd – D can be saved in the / etc/default/useradd file, or the file can be modified directly.

Example 10: changing the shell used to add users by default to csh

[root@node1 ~]# useradd -D -s /bin/csh
[root@node1 ~]# useradd -D

Example 11: add user jason and see the shell it uses

[root@node1 ~]# tail -1 /etc/passwd

Keywords: Linux shell OpenStack network

Added by j152 on Mon, 25 May 2020 14:46:35 +0300