[CENTOS] Linux user addition, how to use the useradd command

Here's how to add a user using the ʻuseradd command on * Linux * (* CenOS7 * here). There are ʻuseradd command and ʻadduser command as a method to add a user with * Linux command *, but in * CentOS7 *, ʻadduser is a symbolic link of ʻuseradd`, so it is the same command.

Command reference result


[root@CENTOS7 ~]# ls -l /usr/sbin/useradd
-rwxr-xr-x.1 root root 137616 August 9 2019/usr/sbin/useradd
[root@CENTOS7 ~]# ls -l /usr/sbin/adduser
lrwxrwxrwx.1 root root 7 October 12 17:04 /usr/sbin/adduser -> useradd
[root@CENTOS7 ~]#

environment

CenOS 7 version confirmation result


[root@CENTOS7 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@CENTOS7 ~]#

1. Add a user with the useradd command (no options)

To add a user, run the following command.

ʻUseradd [username] `

Execution result


[root@CENTOS7 ~]# useradd yasushi
[root@CENTOS7 ~]#

Set the password with the following command. passwd [username]

Execution result


[[root@CENTOS7 ~]# passwd yasushi
Change password for user yasushi.
new password:
Please re-enter your new password:
passwd:All authentication tokens have been successfully renewed.
[root@CENTOS7 ~]#

If there is no option, the user will be added by default. You can check the default value with the following command. useradd -D

Execution result


[root@CENTOS7 ~]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@CENTOS7 ~]#

The meaning of each item is as follows.

item Contents
GROUP Group to which it belongs if not specified
HOME Home directory creation location
INACTIVE The period from when the password expires until the account becomes invalid
※(-1) has no deadline
EXPIRE Password expiration date
SHELL Login shell
SKEL Location of skeleton files
CREATE_MAIL_SPOOL Setting whether to create a mail spool

2. Confirmation of user information

2.1. Check the user list

To check the user list, refer to the / etc / passwd file with the following command. cat /etc/passwd

Execution result


[root@CENTOS7 ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
~~~~~
(abridgement)
~~~~~
yasushi:x:1002:1002::/home/yasushi:/bin/bash
[root@CENTOS7 ~]#

/ etc / passwd holds the following information separated by: .

item Information created
1 username yasushi
2 Dummy password x
3 User ID 1002
4 Group ID 1002
5 comment (Sky)
6 Home directory /home/yasushi
7 Login shell /bin/bash

2.2. Confirm password list

The / etc / shadow file contains a list of encrypted passwords. Only this file root can be referenced. cat /etc/shadow

Execution result


[root@CENTOS7 ~]# cat /etc/shadow
root:$6$n.LBF.Pi$pGDNeMrJNjOgXlcjQdguA/tZTryDlrDR2LCYgCrlT3KDpAu55nGmoh.4.OHlVL0zDw/YQlpV4HM6zzKCd2hQH.:18307:0:99999:7:::
~~~~~
(abridgement)
~~~~~
yasushi:$6$rwTX74Ir$YhhyryrTsbKdlAIWMKJqRQoQsK1TgelnQmHSpGmCDoXiGWQeQLTDtD73FEGur6tw5wZ50SbBo2QNVKEQUDcpV0:18307:0:99999:7:::
[root@CENTOS7 ~]#

/ etc / shadow holds the following information separated by: .

item Contents Information created
1 username User name yasushi
2 password 暗号化されたpassword (abridgement)
3 Last password change date Date when the password was last changed (displayed as the number of days elapsed since January 1, 1970) 18307
4 Password changeable days The number of days before the password can be changed again 0
5 Password expiration date Days before password change is required 99999
6 Password change period Warning notification date How many days in advance to notify the password expiration warning 7
7 Account invalid days The number of days until your account becomes unavailable if you do not change your password after the expiration date (Sky)
8 Account expiration date Days until the account becomes unavailable (displayed as the number of days elapsed since January 1, 1970) (Sky)
9 Reserved field unused (Sky)

2.3. Checking the group list

To check the group list, refer to the / etc / group file with the following command. cat /etc/group

Execution result


[root@CENTOS7 ~]# cat /etc/group
root:x:0:
~~~~~
(abridgement)
~~~~~
yasushi:x:1002:
[root@CENTOS7 ~]#

/ etc / group holds the following information separated by: .

item Information created
1 group name yasushi
2 Dummy password x
3 Group ID 1002
4 Users who belong as a subgroup
(Comma separated for multiple)
(Blank)

3. Add a user with the useradd command (with options)

To add a user with options, execute the following command.

ʻUseradd [optional] [username] `

The main options are:

option Contents
-c comment Set a comment
-d home_dir Specify home directory
-e expire_date The date when the user account becomes unavailable[YYYY-MM-DD]Specified in the format of
-f inactive_days Specify the number of days between the password expiration and the account becoming permanently unavailable
0: This account becomes unusable as soon as the password expires
-1: This function is disabled
-g initial_group Specify the group name or group ID of the main group to which the user belongs
-G group,[...] Specify a comma-separated list of auxiliary groups to which the user belongs
-m [-k skeleton_dir] Create home directory if home directory does not exist
-If you specify the k option at the same time, skeleton_If the files under dir are not specified/etc/The files under skel are copied to your home directory
-o Allow users to be created with duplicate UIDs
-p passwd Specify a password hashed with crypt
-s shell Specify the user's login shell
-u uid Specify UID

Create a yasushi02 user with the home directory" / data / test "and the login shell" / bin / sh "with the following command.

useradd -d /data/test -s /bin/sh yasushi02

Execution result


[root@CENTOS7 ~]# useradd -d /data/test -s /bin/sh yasushi02
[root@CENTOS7 ~]#

Set the password with the following command. passwd [username]

Execution result


[root@CENTOS7 ~]# passwd yasushi02
Change password for user yasushi02.
new password:
Please re-enter your new password:
passwd:All authentication tokens have been successfully renewed.
[root@CENTOS7 ~]#

Check the user list (/ etc / passwd)

/etc/passwd


root:x:0:0:root:/root:/bin/bash
~~~~~
(abridgement)
~~~~~
yasushi:x:1002:1002::/home/yasushi:/bin/bash
yasushi02:x:1003:1003::/data/test:/bin/sh

Check the password list (/ etc / shadow)

/etc/shadow


root:$6$n.LBF.Pi$pGDNeMrJNjOgXlcjQdguA/tZTryDlrDR2LCYgCrlT3KDpAu55nGmoh.4.OHlVL0zDw/YQlpV4HM6zzKCd2hQH.:18307:0:99999:7:::
~~~~~
(abridgement)
~~~~~
yasushi:$6$pJe9DpYg$6i9N217uNBwwIAGjuzfavGWffUyZVWMh0PpgaUEm5Ti3PN8T/KdUvEG4fibaBClUq7AzDphHfAqGuVgnEHfWf.:18307:0:99999:7:::
yasushi02:$6$lXu7BN1C$OcFWVxt/weU4Sh2EUNC4YO5s/e5kqeNQ5EEX0PtwLOf1t/Cm86AGmLdbbJr51Qz0xFWWKwZYmHl0.WPJcyqLU1:18307:0:99999:7:::

Check the group list (`` / etc / group')

/etc/group


root:x:0:
~~~~~
(abridgement)
~~~~~
yasushi:x:1002:
yasushi02:x:1003:

4. Add user by specifying password

If you specify a password, you must specify a crypt-hashed password.

The command to add a user with the user "yasushi03" and password "password03" is as follows. Here, "salt03" is set for salt (the character string added when encrypting the password). You can use any string for salt.

useradd -p $(perl -e 'print crypt("password03", "\$6\$salt03")') yasushi03

Execution result


[root@CENTOS7 ~]# useradd -p $(perl -e 'print crypt("password03", "\$6\$salt03")') yasushi03
[root@CENTOS7 ~]#

The above command uses perl as the crypt hashed password.

perl -e'print crypt ("[password] "," [hashing method symbol] [salt] ");'

The list of hashing methods is as follows.

Hashing method symbol Hashing method
$1$ MD5
$2$ Blowfish
$5$ SHA-256
$6$ SHA-512

perl -e 'print crypt("password03", "\$6\$salt03")'

Execution result


[root@CENTOS7 ~]# perl -e 'print crypt("password03", "\$6\$salt03")'
$6$salt03$/DhkQIuDsIIuvys.ISNOUB.OlWKxzgovMIBdCX2vlwCzEdNuIxMakytppnAGsKwT0hn12BW9XbCBd3KKXBh0/0[root@CENTOS7 ~]#

that's all

Recommended Posts

Linux user addition, how to use the useradd command
[Linux] How to use the echo command
Add Linux user, how to use useradd command (password specification)
(Remember quickly) How to use the LINUX command line
How to use the decorator
How to use the grep command and frequent samples
How to use the zip function
How to use the optparse module
How to use MBDyn (command setting)
[linux] kill command to kill the process
How to use the ConfigParser module
How to use FastAPI ① Tutorial --User Guide
How to use the Spark ML pipeline
[python] How to use __command__, function explanation
How to calculate Use% of df command
How to output the output result of the Linux man command to a file
How to operate Linux from the console
How to use CUT command (with sample)
How to use the IPython debugger (ipdb)
3 best ways to use the less command
[C language] How to use the crypt function on Linux [Password hashing]
How to use the C library in Python
How to use MkDocs for the first time
How to use the graph drawing library Bokeh
How to use the Google Cloud Translation API
How to operate Linux from the outside Procedure
How to use the NHK program guide API
[Algorithm x Python] How to use the list
How to create a shortcut command for LINUX
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use Pandas 2
How to use Virtualenv
How to use pytest_report_header
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to create an article from the command line