Add Linux user, how to use useradd command (password specification)

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:

Check your home directory

[root@CENTOS7 ~]# cd /data
[root@CENTOS7 data]# ls
test
[root@CENTOS7 data]# cd test
[root@CENTOS7 test]# ls -a
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc
[root@CENTOS7 test]#

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

Add Linux user, how to use useradd command (password specification)
Linux user addition, how to use the useradd command
[Linux] How to use the echo command
How to use the Linux grep command
(Remember quickly) How to use the LINUX command line
How to use MBDyn (command setting)
How to use FastAPI ① Tutorial --User Guide
How to use FastAPI ② Advanced --User Guide
[python] How to use __command__, function explanation
How to calculate Use% of df command
How to use CUT command (with sample)
[C language] How to use the crypt function on Linux [Password hashing]
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 shogun
How to use Pandas 2
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
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
[Linux] Change user password
How to use imutils
How to use import
[For beginners] How to use say command in python!
[Python] [Django] How to use ChoiceField and how to add options
[Ansible] How to use SSH password authentication when executing ansible
Beginners! Basic Linux commands and how to use them!
How to use C216 Audio Controller on Arch Linux
How to use the grep command and frequent samples
[Tips] How to use iPhone as webcam on Linux
How to use GitHub on a multi-person server without a password
How to use Qt Designer
How to use linux commands in Visual Studio Code terminal
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
How to use the generator