在linux下创建用户和用户组的命令分别是useradd和groupadd,用法很简单,我们只需要--help或者man一下即可迅速的了解并且掌握其用法。那在这里,来探究一下这两个命令到底做了些什么操作。通过手动创建用户以及组来一探究竟。
首先,来了解一下相关的文件:
创建用户的默认全局配置文件是/etc/login.defs:
#
# Please note that the parameters in this configuration file control the
# behavior of the tools from the shadow-utils component. None of these
# tools uses the PAM mechanism, and the utilities that use PAM (such as the
# passwd command) should therefore be configured elsewhere. Refer to
# /etc/pam.d/system-auth for more information.
#
# *REQUIRED*
# Directory where mailboxes reside, _or_ name of file, relative to the
# home directory. If you _do_ define both, MAIL_DIR takes precedence.
# QMAIL_DIR is for Qmail
#
#QMAIL_DIR Maildir
MAIL_DIR /var/spool/mail #设置用户的mail(邮件)目录
#MAIL_FILE .mail
# Password aging controls: #密码控制相关的配置
#
# PASS_MAX_DAYS Maximum number of days a password may be used.
# PASS_MIN_DAYS Minimum number of days allowed between password changes.
# PASS_MIN_LEN Minimum acceptable password length.
# PASS_WARN_AGE Number of days warning given before a password expires.
#
PASS_MAX_DAYS 99999 #密码最大使用天数(99999表示无限制)
PASS_MIN_DAYS 0 #密码最短使用天数(0表示不限制)
PASS_MIN_LEN 5 #密码最短长度
PASS_WARN_AGE 7 #通知更改密码的时间(表示密码过期前7天通知)
#
# Min/max values for automatic uid selection in useradd #添加非系统用户
#
UID_MIN 1000 #最小的UID
UID_MAX 60000 #最大的UID
# System accounts #添加系统用户
SYS_UID_MIN 201 #最小的UID
SYS_UID_MAX 999 #最大的UID
#
# Min/max values for automatic gid selection in groupadd #添加非系统组
#
GID_MIN 1000 #最小的GID
GID_MAX 60000 #最大的GID
# System accounts #添加系统组
SYS_GID_MIN 201 #最小的GID
SYS_GID_MAX 999 #最大的GID
#
# If defined, this command is run when removing a user. #删除用户相关的配置
# It should remove any at/cron/print jobs etc. owned by
# the user to be removed (passed as the first argument).
#
#USERDEL_CMD /usr/sbin/userdel_local #删除用户的命令
#
# If useradd should create home directories for users by default
# On RH systems, we do. This option is overridden with the -m flag on
# useradd command line.
# 如果要为用户创建家目录,这个配置会和useradd -m选项重复,并且会覆盖-m指定的参数
CREATE_HOME yes #默认创建家目录
# The permission mask is initialized to this value. If not specified,
# the permission mask will be initialized to 022.
UMASK 077 #umask 如果没有指定,默认022
# This enables userdel to remove user groups if no members exist.
#
USERGROUPS_ENAB yes
# Use SHA512 to encrypt password. #设置密码加密方式
ENCRYPT_METHOD SHA512
再看看用户的密码相关文件/etc/passwd:
root:x:0:0:root:/root:/bin/bash
该文件以冒号(:)为分隔符,将关键字段分隔开
用户名:加密后的密码(这里只是一个占位符,真正的加密后的密码在/etc/shadow文件中)
:UID
:GID
:用户相关的注释
:用户家目录
:用户默认shell
!!!如果在/etc/passwd中的用户的密码字段是用x代替的,表示该用户一定有密码,如果该字段为空,那么有些操作系统是不允许这样的用户登录系统的!!!
/etc/shadow:(俗称影子文件)
root:$6$EspiXmeJw0v.1KJZ$tLQE/4kwUWqbPFZr1BXcx.NSrPGNWw8wU/gH1kU7MNei5hYUdUhFVn10BxGgF0EoGmCpY8xb0nZV3JlNY5qEQ.:17756::::::
该配置文件以冒号(:)作为分隔符,将关键字段分隔开
用户名
:加密后的密码(如果此字段为!或者*用户无法使用密码登录系统,但是可以通过其他方式登录,如果此字段为空,可以不用密码登录,但是有些linux系统是不允许这样登录的
:最后一次更改密码的日期(时间戳格式,即1970年1月1日到现在的天数
:密码使用的最短期限
:密码使用的最长期限
:密码警告时间期限(提示用户密码还有多少天过期)
:密码禁用期限(过了密码最大使用期限,还没有改密码的用户会直接被禁止登录,需要管理员解冻)
:账户过期日期(时间戳,空字段表示永不过期)
:保留字段
组相关的文件/etc/group:
root:x:0:root
同样的以冒号(:)为分隔符
组名
密码
GID
改组下的用户列表
/etc/gshadow:
root:::root
组名(必须是系统中已经存在的组)
加密的密码(不常用,规则和/etc/shadow一致)
管理员
组成员
最后,创建用户的模板文件/etc/skel
.bash_logout .bashrc .dir_colors .local/ Readme .xinitrc
.bash_profile .config/ .face .profile .Xclients