linux命令之useradd使用详解
Linux useradd命令用于建立用户帐号。帐号建好之后,再用passwd设定帐号的密码,而可用userdel删除帐号。使用useradd指令所建立的帐号,实际上是保存在/etc/passwd文本文件中,使用权限是超级用户。当然还有对用户信息的修改(usermod)。
语 法
useradd [-mMnr][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数>][-g <群组>][-G <群组>][-s <shell>][-u <uid>][用户帐号]
或
useradd -D [-b][-e <有效期限>][-f <缓冲天数>][-g <群组>][-G <群组>][-s <shell>]
useradd [-d home] [-s shell] [-c comment] [-m [-k template]] [-f inactive] [-e expire ] [-p passwd] [-r] name
useradd –D [选项]
创建一个新用户或更新默认新用户信息。useradd和adduser命令相同,adduser是useradd的软连接。如果使用时不带-D选项,useradd命令使用命令行上指定的值和系统的默认值创建一个新的用户。根据命令行选项,useradd命令也会更新系统文件和创建新用户的主目录并复制初始文件。默认上也会为用户创建组(查看-g,-N,-U和USERGROUPS_ENAB)。
参数:
-c<备注> 加上备注文字。备注文字会保存在passwd的备注栏位中。
-d<登入目录> 指定用户登入时的启始目录。
-D 变更预设值。
-e<有效期限> 指定帐号的有效期限。
-f<缓冲天数> 指定在密码过期后多少天即关闭该帐号。
-g<群组> 指定用户所属的群组。
-G<群组> 指定用户所属的附加群组。
-m 自动建立用户的登入目录。
-M 不要自动建立用户的登入目录。
-n 取消建立以用户名称为名的群组。
-r 建立系统帐号。
-s<shell> 指定用户登入后所使用的shell,默认值为/bin/bash。
-u<uid> 指定用户ID。该值在系统中必须是唯一的。0~499默认是保留给系统用户账号使用的,所以该值必须大于499。
用户的分类
超级用户:root 拥有对系统的最高管理权限 UID=0
普通用户:系统用户 UID1~999(CentOS7版本)、1~499(CentOS6版本)
本地用户UID:1000+ 、500+
虚拟用户:伪用户,一般不会用来登录系统,它主要用于维持摸个访问的正常运行,如ftp,apache等。
注意:UID即每个用户的身份标识,虽然可以修改/etc/passwd(命令设置的UID不允许重复),但尽量保持唯一性。
常用选项
-d,--home-dir HOME_DIR 创建新用户时,用HOME_DIR作为用户主目录。默认情况是将用户名附加到BASE_DIR(默认/home/)并将其用作主目录名称。
-m,--create-home 其实这是一个临时开关,表示如果不存在用户主目录,则生成用户主目录(/home/[用户名]),骨架目录中的文件和目录(可以使用-k选项指定)将会被复制到主目录。默认上,如果没有指定此选项,并且/etc/login.defs(CREATE_HOME)的系统设置没有启用,则不会创建主目录。创建用户主目录必须存在且具有适当的SELinux环境和权限。否则无法创建或访问用户的主目录。
-M,--no-create-home 和-m选项一样是一个临时开关,即使/etc/login.defs(CREATE_HOME)的系统设置是yes,也不创建用户的主目录。
-g,--gid GROUP 用户初始登录组的组名或号码。组名必须已存在,组号码必须指代已存在的组。如果没有指定,useradd的行为将会依赖于/etc/login.defs文件中的USERGROUPS_ENAB参数。如果此参数设置为yes(或在命令行上指定了-U/--user-group),将会为用户创建一个组,组名和登录名相同。如果选项设置为no(或在命令行上指定-N/--no-user-group),useradd会把新用户的主组设置为/etc/default/useradd中GROUP变量指定的值,再或者默认100。
-G,--groups GROUP1[,GROUP2,…[GROUPN]] 用户还属于的附加组列表。每个组都用逗号隔开,中间没有空格。这里的组受到-g选项给定的组同样的限制。默认上,用户只属于初始组。(一个用户只允许有一个主组,可以有多个附加组)
-s,--shell SHELL 用户登录shell名。默认为留空,让系统根据/etc/default/useradd中的SHELL变量选择默认的登录shell,默认为空字符串。有时需要禁止某些用户执行登录动作,例如用来执行系统服务的用户,这时将shell设置为/sbin/nologin就可以禁止用户登录。
-b,--base-dir BASE_DIR 如果未指定-d HOME_DIR,系统使用默认基目录(BASE目录)。此时BASE_DIR与账户名称联系起来定义主目录(HOME目录)。这时的BASE_DIR必须存在,否则无法创建主目录。如果选项没有指定,useradd将使用/etc/default/useradd中的HOMR变量,过默认的/home。
-c,--comment COMMENT 通常是关于登录的简短描述,当前用于用户全名,可以是任何字符串。
-D,--defaults 显示或更改默认的useradd配置。
-e,--expiredate EXPIRE_DATE 用户账户将被禁用的日期,以YYYY-MM-DD格式指定。如果没有指定,useradd将使用/etc/default/useradd中的EXPIRE变量指定的默认过期时间,或者一个空字符串(永不过期)。
-f,--inactive INACTIVE 密码过期后,账户被彻底禁用前的天数。0表示立即禁用,-1表示禁用这个功能。如果未指定,useradd将使用/etc/default/useradd中的INACTIVE指定的默认禁用周期,或默认为-1。
-k,--skel SKEL_DIR 基本信息模板目录,包含使用useradd创建用户时,要复制到用户主目录中的文件和目录。这个选项只有在指定-m(或--create-home)选项时才有效。如果此项没有设置,该目录使用/etc/default/useradd中的SKELeton的变量或默认为/etc/skel。如果可以,也复制ACL和扩展属性。
-K,--key KEY=VALUE 覆盖/etc/login.defs默认值(UID_MIN,UID_MAX,UMASK,PASS_MAX_DAYS及其他)。例如:-K PASS_MAX_DAYS=-1可以用于创建一个密码不会过期的系统账户,即使系统账户没有密码。你还可以指定多个-K选项,如:-K UID_MIN=100 –K UID_MAX=499。
-l,--no-log-init 不要将用户添加到登录和登录失败数据库。默认上,最近登录和登录失败中用户的条目会被重置,以避免重新使用先前删除的用户的条目。
-N,--no-user-group 不要创建同名组,而是将用户添加到-g选项指定的组,或根据/etc/default/useradd中的GROUP变量。如果没有指定-g,-N和-U选项,默认行为由/etc/login.defs中的USERGROUPS_ENAB变量指定。
-o,--non-unique 允许使用重复的UID创建用户账户。此选项只有和-u选项组合使用才有效。
-p,--password PASSWORD 加密了的密码,就像crypt(3)的返回值。默认为禁用密码。注意:不推荐使用这个选项,应为密码(或加密过的密码)会被用户通过列出这个过程而看到。你应该确保密码符合系统的密码政策
-r,--system 创建一个系统账户。将在/etc/shadow中创建没有陈旧信息的系统用户,并且在选择/etc/login.defs中定义的SYS_UID_MIN到SYS_UID_MAX范围中的数字标识,而不是UID_MIN到UID_MAX(以及它们的GID对应的创建组)。注意:useradd不会为这种用户或篡改建主目录,无论/etc/login.defs(CREATE_HOME)中的默认设置是怎样。如果香味要创建的系统账户创建主目录,需要指定-m选项。
-R,--root CHROOT_DIR 在CHROOT_DIR目录中应用更改,并使用CHROOT_DIR目录中的配置文件。
-u,--uid UID 用户ID的数字值。此值必须为唯一的,除非使用了-o选项。此值必须非负,默认使用大于等于UID_MIN,且大于任何其他用户ID最小值。
-U,--user-group 创建一个和用户同名的组,并将用户添加到组中。如果没有指定-g,-N和-UID_MIN选项,默认行为有/etc/login.defs中的USERGROUPS_ENAB变量指定。
-Z,--selinux-user SEUSER 用户登录的SELinux用户。默认为留空,这会造成系统选择默认的SELinux用户。
注意1:只带-D选项使用时,useradd将显示当前的默认值。-D和其他选项配合使用时,useradd将为指定的选项更新默认值。有效的更新默认值选项有-b,-e,-f,-g,-s。
注意2:系统管理源负责将默认的用户文件放在/etc/skel/目录中(或者命令行上、/etc/default/useradd中指定的任何其他目录)。
注意3:你可能不能向NISsan组或LDAP组添加用户。这只能在相应服务器上进行。相似的,如果用户名已经存在于外部用户数据库中,比如NIS或LDAP,useradd将拒绝创建用户账户的请求。
注意4:用户名不能超过32个字节长。
配置文件
在/etc/login.defs中有如下配置变量,可以用来更改此工具的行为:
CREATE_HOME(boolean) 指示是否应该为新用户默认创建主目录。此设置并不应用到系统用户,并且可以使用命令行覆盖。
GID_MAX(number),GID_MIN(number) useradd,groupadd或newusers创建的常规组的组ID范围。GID_MIN默认值1000(CentOS6为500),GID_MAX默认值60000。
MAIL_DIR(string)邮箱目录。修改或删除用户账户时需要处理邮箱,如果没有指定,将使用编译时指定的默认值。
MAIL_FILE(string) 定义用户邮箱文件的位置(相对于主目录)。
注意:MAIL_DIR和MAIL_FILE变量由useradd,usermod和userdel用于创建、移动或删除用户邮箱。如果MAIL_CHECK_ENAB设置为yes,它们也被用于定义MAIL环境变量。
MAX_MEMBERS_PER_GROUP(number) 每个组条目的最大成员数。达到最大值时,在/etc/group开始一个新条目(行)(使用同样的名称,同样的密码,同样的GID)。默认值是0,意味着组中的成员数没有限制。此功能(分割组)允许限制组文件中的行长度。这对于确保NIS组的行比长于1024字符。如果要强制这个限制,可以使用25。
注意:分割组可能不受所有工具的支持(甚至在Shadow工具集中)。如果没有必要你不应该使用这个变量。
PASS_MAX_DAYS(number) 一个密码可以使用的最大天数。如果密码比这个旧,将会强迫更改密码。如果不指定,就假定为-1,这会禁用此限制。
PASS_MIN_DAYS(number) 两次更改密码时间最小间隔。将会拒绝任何早于此的更改密码的尝试。如果不指定就假定为-1,将会禁用此限制。
PASS_WARN_AGE(number) 密码过期之前鬼畜警告的天数。0表示在过期当天警告,负值表示不警告。如果没有指定,不会给出警告。
SYS_GID_MAX(number),SYS_GID_MIN(number) useradd、groupadd或newusers创建的系统组的组ID范围。SYS_GID_MIN默认101(CentOS6为201),SYS_GID_MAX默认GID_MIN-1。
SYS_UID_MAX(number),SYS_UID_MIN(number) useradd或newusers创建的系统用户的用户ID的范围。SYS_UID_MIN默认101(CentOS6为201),SYS_UID_MAX默认UID_MIN-1。
UID_MAX(number),UID_MIN(number) useradd或newusers创建的普通用户的用户ID的范围。UID_MIN默认1000(CentOS6为500),UID_MAX默认60000。
UMASK(number) 文件模式创建掩码初始化为此值。如果没有指定,掩码初始化为022。Useradd和newusers使用此掩码设置它们创建的用户主目录的模式。也被login用于指定用户的初始umask。注意,此掩码可以被用户的GECOS行覆盖(当设置了QUOTAS_ENAB时),也可以被带K指示符的limits(5)定义的限制值覆盖。
USERGROUPS_ENAB(boolean) 如果uid和gid相同,用户名和主用户名也相同,使非root组的组掩码位和属主位相同(如:022->002,077->007)。如果设置为yes,组中也没有成员了,userdel将移除此用户组,useradd创建用户时,也会创建一个同名的默认组。
用户信息存放文件
/etc/passwd 用户基本信息
/etc/shadow用户安全信息(包含密码)
/etc/group 用户组信息
/etc/gshadow 用户组安全信息
/etc/default/useradd 用户创建的默认值
/etc/skel/ 包含默认文件的目录
/etc/login.defs Shadow密码套件配置
退出返回码
0 成功
1 无法更新命令文件
2 无效的命令语法
3 给了选项一个无效的参数
4 UID已经使用(且没有-o)
6 指定的组不存在
9 用户名已被使用
10 无法更新组文件
12 无法创建主目录
14 无法更新SELinux用户映射
示例
useradd freeoa
最简单的调用,会自动创建freeoa组,并成为freeoa用户的默认主组,同时默认登录shell为bash。
useradd freeoa -u 1024
建立用户且指定UID
useradd -d /home/freeoa freeoa
添加用户并指定主目录
useradd -g freeoa t1 //指定用户主组
# id t1
uid=1046(t1) gid=1001(freeoa) 组=1001(freeoa)
useradd -G freeoa,root t2 //指定用户附加组
# id t2
添加用户并加入指定用户组(主组和附加组)
useradd user with group with same uid and gid
# groupadd -r -g 1234567 nexus \
&& useradd -r -u 1234567 -g 1234567 -m -c "nexus role account" -d /sonatype-work -s /bin/false nexus
用户帐号删除命令userdel的使用可参考此处。
userdel [选项] LOGIN
userdel 用于删除用户账户和相关文件
-r,--remove 删除主目录和邮件池(/var/mail)
强制退出已经登录用户
查看当前登录用户的命令:w
这里知道了登录用户的tty是ps1,执行强制退出命令pkill:
命令原型: pkill -kill -t [TTY]
# pkill -kill -t ps1
执行之后再执行w指令可以看到用户已经退出。
用户的创建与修改
useradd可用选项包括:
-c"comment here"--提供了对新用户账户的描述。一般来说为用户的全名。可以使用用户账户名(-c Jake)替换 comment。使用双引号输入多个单词(例如,-c"Jake Jackson")。
-d home_dir--设置账户所使用的主目录。默认情况下使用登录名命名该目录,并将其放置到ome中。请使用目录名替换home_dir(例如,-d /mnt/home/jake)。
-D--该选项并不创建一个新的账户,而是将所提供的信息保存为任何新创建账户的默认设置。
-e expire_date--以YYYY-MM-DD格式分配账户的有效期限。可以使用一个日期替换expire_date(例如,如果想要使某一账户在2019年5月5日到期,可以使用-e 2019-05-05)。
-f-1--设置密码过期之后多少天账户被永久禁用。默认值-1禁用了该选项。而如果将其设置为0,则表示在密码过期之后马上禁用账户。请使用一个数字替换-1(即负1)。
-g group--设置新用户所在的主组(必须已经存在于/etc/group 文件中)。请使用组名替换group(例如,-gwheel)。如果没有使用该选项,则会创建一个新组(使用用户名作为组名),并将其作为用户的主组。
-G grouplist-将新用户添加到以逗号分隔的增补组列表中(例如,-Gwheel,sales,techlunch)(如果在后面的 usermod 命令中使用了-G,请确保使用的是-aG,而不是-G。否则,现有的增补组将被删除,而所提供的组将成为唯一可分配的组)。
-k skel_dir--设置包含了初始配置文件和登录脚本(这些配置文件和脚本将被复制到新用户的主目录)的框架目录。该参数可以与-m选项一起使用。请使用目录名替换 skel di(如果没有使用该选项,则使用/etc/skel目录)。
-m--自动创建用户的主目录,并将框架目录(/etc/skel)中的文件复制到该主目录中(对于RHEL来说,该选项时默认行为,所以不是必需的,但对于 Ubuntu 来说却不是默认行为)。
-M--当使用该选项时,即使默认行为被设置为创建主目录,也不会创建新用户的主目录。
-n--禁用创建一个与新用户名和用户I 相匹配的新组的默认行为。Fedora 和 RHEL 系统可以使用该选项。而其他Limnux系统通常将新用户分配给名为users的组。
-o--使用-u uid 创建一个与另一个用户账户具有相同 UID 的用户账户(如果想要让两个不同的用户名拥有对相同文件和目录组的权限,使用该选项是非常有效率的)。
-p passwd--为当前添加的账户输入一个密码,且必须是一个加密后的密码。如果此时不添加加密密码,日后还可以使用 passwd user 命令为用户添加密码(如果想要生成一个 MD5加密密码,请输入opensslpasswd)。
-s shell--指定账户所使用的命令Shell。请使用命令Shell替换shell(例如,-s /bin/csh)。
-u user_id--为账户指定用户ID号(例如,-u 793)。如果没有使用-u选项,默认行为是自动分配下一个可用的数字。请使用ID号替换user_id。
在创建账户的过程中,useradd 命令完成了以下活动:
读取/etc/login.defs 和 /etc/default/useradd 文件,获取创建账户时所使用的默认值。
检查命令行参数,找出哪些默认值被重写。
根据默认值和命令行参数,在/etc/passwd和/etc/shadow 文件中创建新用户条目。
在/etc/group文件中创建新的组条目(Fedora 使用新用户名创建组)。
在home目录中根据用户名创建一个主目录。
将/etc/skel目录中的所有文件复制到新的主目录中。其中通常包括登录和应用程序启动脚本。
如果想要查看其他的默认设置,可以参阅/etc/default/useradd 文件;此外还可以通过输入带有-D选项的 useradd 命令查看默认设置,如下所示:
# useradd-D
GROUP-100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE MAIL SPOOL=yes
还可以使用-D选项来更改默认值。当运行带有该选项的命令时,useradd不实际创建一个新用户账户;而是在/etc/default/useradd中将任何额外提供的选项保存为新默认值。并不是所有的useradd选项可以与-D选项一起使用。可以使用的只有5个选项,如下所示:
-b default_home--设置所创建的主目录所在的默认目录。用目录名替换default_home(例如,-b /freeoa)。通常默认目录为/home。
-e defautl_expire_date--设置禁用用户账户的默认的截止日期。应该使用格式为YYYY-MM-DD的日期替换 dafult_expire_date值(例如,-e 2022-10-17)。
-f default_inactive--设置密码过期后多少天账户被禁用。用一个表示天数的数字替换default_inactive(例如,-f7)。
-g default_group--设置新用户所在的默认组。通常useradd 命令创建与用户具有相同名称和 ID号的新组。用组名替换 default_group(例如,-g freeoa)。
-s default_shell--设置新用户的默认 Shell,通常为/bin/bash。用默认 Shell 的完整路径替换default_shell(例如,-s /usr/bin/ksh)。
如果想要设置任何默认值,可以首先提供-D选项,然后添加想要设置的默认值。例如,为将默认主目录设置为/home/everyone,将默认Shell 设置为/bin/tcsh,请输入以下命令:
#useradd -D -b/home/everyone -s /bin/tcsh
除了设置用户默认值,管理员还可以创建被复制到每个用户主目录的默认文件。这些文件可以包括登录脚本和 Shell 配置文件(比如.bashrc)。其他对于使用用户账户非常有用的命令包括usermod(用来修改现有账户的设置)和userdel(用来删除现有用户账户)。
usermod相关选项
-c username--更改与用户账户相关联的描述。请使用用户账户名替换username(-cjake)。如果输入多个单词,则使用双引号(例如,-c "Jake Jackson")。
-d home_dir--更改账户的主目录。默认情况下,主目录名称与登录名相同,且被放置在/home中。用目录名替换 home_dir例如,-d /mnt/homes/jake)。
-e expire_date --为账户分配一个 YYYY-MM-DD 格式的新过期日期。用一个日期替换expire date(对于2017年10月15日,使用-e 2017-10-15)。
-f-1--更改密码过期之后多少天永久禁用账户。默认值-1表示禁用该选项。而如何设置为0则表示密码过期后马上禁用账户。用一个数字替换-1。
-g group--更改用户所在的主组(/etc/group文件中所列出)。用组名替换group(例如,-gwheel)。
-G grouplist--将用户的附属组(secondary groups)设置为以逗号分隔的组列表。除了用户的私有组之外,如果用户还属于另一个组中,则还必须添加-a选项(-Ga)。如果没有添加,用户将只会属于新的组,而失去任何其他组的成员资格。
-l login_name更改账户的登录名。
-L--通过在/etc/shadow文件中的加密密码前添加一个感叹号,可以锁定账户。虽然账户被锁定,但仍然允许用户保持密码不变(使用-U选项解除账户锁定)。
-m--只有当使用-d选项时该选项才可用,并将用户主目录中的内容复制到新目录中。
-o--只能与-u uid 一起使用,才能取消 UID 必须唯一的限制。
-s shell--为账户指定一个不同的命令Shell。用命令Shell 替换 shell(例如,-s bash)。
-u user_id--更改账户的用户D号。用ID号替换 user_id(例如,-u 1081)。
-U--解除用户账户的锁定(通过删除加密密码开头的感叹号)。
一些示例:
#usermod -Ga sales,marketing, freeoa
为用户 freeoa 添加了增补组。-a选项(-Ga)确保将该增补组添加到用户chris 的所有现有组。如果没有使用-a,则 chris 的现有增补组将被清除,而分配给该用户的新组列表将只包括新的增补组。
只有拥有root权限的人才可以将用户分配到用户组。
每一个用户都被分配给一个主组。在RHEL中主组默认为与用户同名的一个新组。所以如果将用户命名为sara,那么分配给该用户的组也为sara。主组由/etc/passwd 文件中每一条目的第三个字段的数字来表示。例如,组ID1007如下所示:
sara:x:1002:1007:Sara Green:/home/sara:/bin/tcsh
该组在/etc/group 文件中条目的入口点为:
sara:x:1007:
用户sara可以属于多个增补组,或不属于任何增补组。用户可以使用newgrp指令来为不同的用户组创建文件,使该文件的主组文件为该用户组。还可以通过使用 newgrp 命令允许用户临时成为某一组的成员,而不是实际成为该组成员为此,拥有root 权限的人可以使用 gpasswd命令设置一个组密码(比如 gpasswd sales)。此后,任何用户通过输入 newgrp sales进入一个 Shell,当出现提示时输入组密码,从而临时使用 sales 作为其主组;可在完成任务后退出该Shell从而回到之前的终端shell。
通常0~999的组ID(GID)分配给特殊的管理组,root组与GID 0相关联。普通组从1000开始,也有部分Linux为管理组保留了0~500的GID。UID与GID可以平行存在。传统的组和用户管理有相当的局限性,后面又扩展为LDAP、NIS、Winbind服务验证用户身份,当然也有ACL权限管理体系。
Description
useradd is a low-level utility for adding users to a system. In general, the more friendly adduser should be used instead.
Your operating system may come with a slightly different version of useradd; check your documentation before using it to create new accounts. This documentation refers to some options frequently used on Debian-based variants of Linux, but is representative of useradd's general use.
When invoked without the -D option, the useradd command creates a new user account using the values specified on the command line plus the default values from the system. Depending on command line options, the useradd command will update system files and may also create the new user's home directory and copy initial files.
By default, a group will also be created for the new user (see the -g, -N, -U options, and the USERGROUPS_ENAB variable, below).
useradd -D
useradd -D [options]
Options
Changing New User Default Values
When invoked with only the -D option, useradd will display the current default values. When invoked with -D plus other options, useradd will update the default values for the specified options.
Valid default-changing options are:
Notes
The system administrator is responsible for placing the default user files in the /etc/skel/ directory (or any other skeleton directory specified in /etc/default/useradd or on the command line).
Caveats
You may not add a user to a NIS or LDAP group. This must be performed on the corresponding server.Similarly, if the username already exists in an external user database such as NIS or LDAP, useradd will deny the user account creation request.
It is usually recommended to only use usernames that begin with a lower case letter or an underscore, followed by lower case letters, digits, underscores, or dashes. They can end with a dollar sign. The regular expression which describes a valid username is:
[a-z_][a-z0-9_-]*[$]?
The only constraints are that usernames must neither start with a dash ('-') nor plus ('+') nor tilde ('~') nor contain a colon (':'), a comma (','), or a whitespace (space: ' ', end of line: '\n', tab: '\t', etc.). Note that using a slash ('/') may break the default algorithm for the definition of the user's home directory.
Usernames may only be up to 32 characters long.
Configuration
The following configuration variables in /etc/login.defs change the behavior of this tool:
The MAIL_DIR and MAIL_FILE variables are used by useradd, usermod, and userdel to create, move, or delete the user's mail spool.
Files
Exit Status
useradd exits with the following status, depending on what occurred:
Note: For these commands to work you must have root privileges.
useradd -D
Displays the defaults for new users. Output resembles the following:
GROUP=1001
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
useradd newperson
Creates newperson as a new user. Once the new user has been added, you would need to use the passwd command to assign a password to the account.
Once a user has been created, you can modify any of the user settings, such as the user's home directory, using the usermod command.
语 法
useradd [-mMnr][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数>][-g <群组>][-G <群组>][-s <shell>][-u <uid>][用户帐号]
或
useradd -D [-b][-e <有效期限>][-f <缓冲天数>][-g <群组>][-G <群组>][-s <shell>]
useradd [-d home] [-s shell] [-c comment] [-m [-k template]] [-f inactive] [-e expire ] [-p passwd] [-r] name
useradd –D [选项]
创建一个新用户或更新默认新用户信息。useradd和adduser命令相同,adduser是useradd的软连接。如果使用时不带-D选项,useradd命令使用命令行上指定的值和系统的默认值创建一个新的用户。根据命令行选项,useradd命令也会更新系统文件和创建新用户的主目录并复制初始文件。默认上也会为用户创建组(查看-g,-N,-U和USERGROUPS_ENAB)。
参数:
-c<备注> 加上备注文字。备注文字会保存在passwd的备注栏位中。
-d<登入目录> 指定用户登入时的启始目录。
-D 变更预设值。
-e<有效期限> 指定帐号的有效期限。
-f<缓冲天数> 指定在密码过期后多少天即关闭该帐号。
-g<群组> 指定用户所属的群组。
-G<群组> 指定用户所属的附加群组。
-m 自动建立用户的登入目录。
-M 不要自动建立用户的登入目录。
-n 取消建立以用户名称为名的群组。
-r 建立系统帐号。
-s<shell> 指定用户登入后所使用的shell,默认值为/bin/bash。
-u<uid> 指定用户ID。该值在系统中必须是唯一的。0~499默认是保留给系统用户账号使用的,所以该值必须大于499。
用户的分类
超级用户:root 拥有对系统的最高管理权限 UID=0
普通用户:系统用户 UID1~999(CentOS7版本)、1~499(CentOS6版本)
本地用户UID:1000+ 、500+
虚拟用户:伪用户,一般不会用来登录系统,它主要用于维持摸个访问的正常运行,如ftp,apache等。
注意:UID即每个用户的身份标识,虽然可以修改/etc/passwd(命令设置的UID不允许重复),但尽量保持唯一性。
常用选项
-d,--home-dir HOME_DIR 创建新用户时,用HOME_DIR作为用户主目录。默认情况是将用户名附加到BASE_DIR(默认/home/)并将其用作主目录名称。
-m,--create-home 其实这是一个临时开关,表示如果不存在用户主目录,则生成用户主目录(/home/[用户名]),骨架目录中的文件和目录(可以使用-k选项指定)将会被复制到主目录。默认上,如果没有指定此选项,并且/etc/login.defs(CREATE_HOME)的系统设置没有启用,则不会创建主目录。创建用户主目录必须存在且具有适当的SELinux环境和权限。否则无法创建或访问用户的主目录。
-M,--no-create-home 和-m选项一样是一个临时开关,即使/etc/login.defs(CREATE_HOME)的系统设置是yes,也不创建用户的主目录。
-g,--gid GROUP 用户初始登录组的组名或号码。组名必须已存在,组号码必须指代已存在的组。如果没有指定,useradd的行为将会依赖于/etc/login.defs文件中的USERGROUPS_ENAB参数。如果此参数设置为yes(或在命令行上指定了-U/--user-group),将会为用户创建一个组,组名和登录名相同。如果选项设置为no(或在命令行上指定-N/--no-user-group),useradd会把新用户的主组设置为/etc/default/useradd中GROUP变量指定的值,再或者默认100。
-G,--groups GROUP1[,GROUP2,…[GROUPN]] 用户还属于的附加组列表。每个组都用逗号隔开,中间没有空格。这里的组受到-g选项给定的组同样的限制。默认上,用户只属于初始组。(一个用户只允许有一个主组,可以有多个附加组)
-s,--shell SHELL 用户登录shell名。默认为留空,让系统根据/etc/default/useradd中的SHELL变量选择默认的登录shell,默认为空字符串。有时需要禁止某些用户执行登录动作,例如用来执行系统服务的用户,这时将shell设置为/sbin/nologin就可以禁止用户登录。
-b,--base-dir BASE_DIR 如果未指定-d HOME_DIR,系统使用默认基目录(BASE目录)。此时BASE_DIR与账户名称联系起来定义主目录(HOME目录)。这时的BASE_DIR必须存在,否则无法创建主目录。如果选项没有指定,useradd将使用/etc/default/useradd中的HOMR变量,过默认的/home。
-c,--comment COMMENT 通常是关于登录的简短描述,当前用于用户全名,可以是任何字符串。
-D,--defaults 显示或更改默认的useradd配置。
-e,--expiredate EXPIRE_DATE 用户账户将被禁用的日期,以YYYY-MM-DD格式指定。如果没有指定,useradd将使用/etc/default/useradd中的EXPIRE变量指定的默认过期时间,或者一个空字符串(永不过期)。
-f,--inactive INACTIVE 密码过期后,账户被彻底禁用前的天数。0表示立即禁用,-1表示禁用这个功能。如果未指定,useradd将使用/etc/default/useradd中的INACTIVE指定的默认禁用周期,或默认为-1。
-k,--skel SKEL_DIR 基本信息模板目录,包含使用useradd创建用户时,要复制到用户主目录中的文件和目录。这个选项只有在指定-m(或--create-home)选项时才有效。如果此项没有设置,该目录使用/etc/default/useradd中的SKELeton的变量或默认为/etc/skel。如果可以,也复制ACL和扩展属性。
-K,--key KEY=VALUE 覆盖/etc/login.defs默认值(UID_MIN,UID_MAX,UMASK,PASS_MAX_DAYS及其他)。例如:-K PASS_MAX_DAYS=-1可以用于创建一个密码不会过期的系统账户,即使系统账户没有密码。你还可以指定多个-K选项,如:-K UID_MIN=100 –K UID_MAX=499。
-l,--no-log-init 不要将用户添加到登录和登录失败数据库。默认上,最近登录和登录失败中用户的条目会被重置,以避免重新使用先前删除的用户的条目。
-N,--no-user-group 不要创建同名组,而是将用户添加到-g选项指定的组,或根据/etc/default/useradd中的GROUP变量。如果没有指定-g,-N和-U选项,默认行为由/etc/login.defs中的USERGROUPS_ENAB变量指定。
-o,--non-unique 允许使用重复的UID创建用户账户。此选项只有和-u选项组合使用才有效。
-p,--password PASSWORD 加密了的密码,就像crypt(3)的返回值。默认为禁用密码。注意:不推荐使用这个选项,应为密码(或加密过的密码)会被用户通过列出这个过程而看到。你应该确保密码符合系统的密码政策
-r,--system 创建一个系统账户。将在/etc/shadow中创建没有陈旧信息的系统用户,并且在选择/etc/login.defs中定义的SYS_UID_MIN到SYS_UID_MAX范围中的数字标识,而不是UID_MIN到UID_MAX(以及它们的GID对应的创建组)。注意:useradd不会为这种用户或篡改建主目录,无论/etc/login.defs(CREATE_HOME)中的默认设置是怎样。如果香味要创建的系统账户创建主目录,需要指定-m选项。
-R,--root CHROOT_DIR 在CHROOT_DIR目录中应用更改,并使用CHROOT_DIR目录中的配置文件。
-u,--uid UID 用户ID的数字值。此值必须为唯一的,除非使用了-o选项。此值必须非负,默认使用大于等于UID_MIN,且大于任何其他用户ID最小值。
-U,--user-group 创建一个和用户同名的组,并将用户添加到组中。如果没有指定-g,-N和-UID_MIN选项,默认行为有/etc/login.defs中的USERGROUPS_ENAB变量指定。
-Z,--selinux-user SEUSER 用户登录的SELinux用户。默认为留空,这会造成系统选择默认的SELinux用户。
注意1:只带-D选项使用时,useradd将显示当前的默认值。-D和其他选项配合使用时,useradd将为指定的选项更新默认值。有效的更新默认值选项有-b,-e,-f,-g,-s。
注意2:系统管理源负责将默认的用户文件放在/etc/skel/目录中(或者命令行上、/etc/default/useradd中指定的任何其他目录)。
注意3:你可能不能向NISsan组或LDAP组添加用户。这只能在相应服务器上进行。相似的,如果用户名已经存在于外部用户数据库中,比如NIS或LDAP,useradd将拒绝创建用户账户的请求。
注意4:用户名不能超过32个字节长。
配置文件
在/etc/login.defs中有如下配置变量,可以用来更改此工具的行为:
CREATE_HOME(boolean) 指示是否应该为新用户默认创建主目录。此设置并不应用到系统用户,并且可以使用命令行覆盖。
GID_MAX(number),GID_MIN(number) useradd,groupadd或newusers创建的常规组的组ID范围。GID_MIN默认值1000(CentOS6为500),GID_MAX默认值60000。
MAIL_DIR(string)邮箱目录。修改或删除用户账户时需要处理邮箱,如果没有指定,将使用编译时指定的默认值。
MAIL_FILE(string) 定义用户邮箱文件的位置(相对于主目录)。
注意:MAIL_DIR和MAIL_FILE变量由useradd,usermod和userdel用于创建、移动或删除用户邮箱。如果MAIL_CHECK_ENAB设置为yes,它们也被用于定义MAIL环境变量。
MAX_MEMBERS_PER_GROUP(number) 每个组条目的最大成员数。达到最大值时,在/etc/group开始一个新条目(行)(使用同样的名称,同样的密码,同样的GID)。默认值是0,意味着组中的成员数没有限制。此功能(分割组)允许限制组文件中的行长度。这对于确保NIS组的行比长于1024字符。如果要强制这个限制,可以使用25。
注意:分割组可能不受所有工具的支持(甚至在Shadow工具集中)。如果没有必要你不应该使用这个变量。
PASS_MAX_DAYS(number) 一个密码可以使用的最大天数。如果密码比这个旧,将会强迫更改密码。如果不指定,就假定为-1,这会禁用此限制。
PASS_MIN_DAYS(number) 两次更改密码时间最小间隔。将会拒绝任何早于此的更改密码的尝试。如果不指定就假定为-1,将会禁用此限制。
PASS_WARN_AGE(number) 密码过期之前鬼畜警告的天数。0表示在过期当天警告,负值表示不警告。如果没有指定,不会给出警告。
SYS_GID_MAX(number),SYS_GID_MIN(number) useradd、groupadd或newusers创建的系统组的组ID范围。SYS_GID_MIN默认101(CentOS6为201),SYS_GID_MAX默认GID_MIN-1。
SYS_UID_MAX(number),SYS_UID_MIN(number) useradd或newusers创建的系统用户的用户ID的范围。SYS_UID_MIN默认101(CentOS6为201),SYS_UID_MAX默认UID_MIN-1。
UID_MAX(number),UID_MIN(number) useradd或newusers创建的普通用户的用户ID的范围。UID_MIN默认1000(CentOS6为500),UID_MAX默认60000。
UMASK(number) 文件模式创建掩码初始化为此值。如果没有指定,掩码初始化为022。Useradd和newusers使用此掩码设置它们创建的用户主目录的模式。也被login用于指定用户的初始umask。注意,此掩码可以被用户的GECOS行覆盖(当设置了QUOTAS_ENAB时),也可以被带K指示符的limits(5)定义的限制值覆盖。
USERGROUPS_ENAB(boolean) 如果uid和gid相同,用户名和主用户名也相同,使非root组的组掩码位和属主位相同(如:022->002,077->007)。如果设置为yes,组中也没有成员了,userdel将移除此用户组,useradd创建用户时,也会创建一个同名的默认组。
用户信息存放文件
/etc/passwd 用户基本信息
/etc/shadow用户安全信息(包含密码)
/etc/group 用户组信息
/etc/gshadow 用户组安全信息
/etc/default/useradd 用户创建的默认值
/etc/skel/ 包含默认文件的目录
/etc/login.defs Shadow密码套件配置
退出返回码
0 成功
1 无法更新命令文件
2 无效的命令语法
3 给了选项一个无效的参数
4 UID已经使用(且没有-o)
6 指定的组不存在
9 用户名已被使用
10 无法更新组文件
12 无法创建主目录
14 无法更新SELinux用户映射
示例
useradd freeoa
最简单的调用,会自动创建freeoa组,并成为freeoa用户的默认主组,同时默认登录shell为bash。
useradd freeoa -u 1024
建立用户且指定UID
useradd -d /home/freeoa freeoa
添加用户并指定主目录
useradd -g freeoa t1 //指定用户主组
# id t1
uid=1046(t1) gid=1001(freeoa) 组=1001(freeoa)
useradd -G freeoa,root t2 //指定用户附加组
# id t2
添加用户并加入指定用户组(主组和附加组)
useradd user with group with same uid and gid
# groupadd -r -g 1234567 nexus \
&& useradd -r -u 1234567 -g 1234567 -m -c "nexus role account" -d /sonatype-work -s /bin/false nexus
用户帐号删除命令userdel的使用可参考此处。
userdel [选项] LOGIN
userdel 用于删除用户账户和相关文件
-r,--remove 删除主目录和邮件池(/var/mail)
强制退出已经登录用户
查看当前登录用户的命令:w
这里知道了登录用户的tty是ps1,执行强制退出命令pkill:
命令原型: pkill -kill -t [TTY]
# pkill -kill -t ps1
执行之后再执行w指令可以看到用户已经退出。
用户的创建与修改
useradd可用选项包括:
-c"comment here"--提供了对新用户账户的描述。一般来说为用户的全名。可以使用用户账户名(-c Jake)替换 comment。使用双引号输入多个单词(例如,-c"Jake Jackson")。
-d home_dir--设置账户所使用的主目录。默认情况下使用登录名命名该目录,并将其放置到ome中。请使用目录名替换home_dir(例如,-d /mnt/home/jake)。
-D--该选项并不创建一个新的账户,而是将所提供的信息保存为任何新创建账户的默认设置。
-e expire_date--以YYYY-MM-DD格式分配账户的有效期限。可以使用一个日期替换expire_date(例如,如果想要使某一账户在2019年5月5日到期,可以使用-e 2019-05-05)。
-f-1--设置密码过期之后多少天账户被永久禁用。默认值-1禁用了该选项。而如果将其设置为0,则表示在密码过期之后马上禁用账户。请使用一个数字替换-1(即负1)。
-g group--设置新用户所在的主组(必须已经存在于/etc/group 文件中)。请使用组名替换group(例如,-gwheel)。如果没有使用该选项,则会创建一个新组(使用用户名作为组名),并将其作为用户的主组。
-G grouplist-将新用户添加到以逗号分隔的增补组列表中(例如,-Gwheel,sales,techlunch)(如果在后面的 usermod 命令中使用了-G,请确保使用的是-aG,而不是-G。否则,现有的增补组将被删除,而所提供的组将成为唯一可分配的组)。
-k skel_dir--设置包含了初始配置文件和登录脚本(这些配置文件和脚本将被复制到新用户的主目录)的框架目录。该参数可以与-m选项一起使用。请使用目录名替换 skel di(如果没有使用该选项,则使用/etc/skel目录)。
-m--自动创建用户的主目录,并将框架目录(/etc/skel)中的文件复制到该主目录中(对于RHEL来说,该选项时默认行为,所以不是必需的,但对于 Ubuntu 来说却不是默认行为)。
-M--当使用该选项时,即使默认行为被设置为创建主目录,也不会创建新用户的主目录。
-n--禁用创建一个与新用户名和用户I 相匹配的新组的默认行为。Fedora 和 RHEL 系统可以使用该选项。而其他Limnux系统通常将新用户分配给名为users的组。
-o--使用-u uid 创建一个与另一个用户账户具有相同 UID 的用户账户(如果想要让两个不同的用户名拥有对相同文件和目录组的权限,使用该选项是非常有效率的)。
-p passwd--为当前添加的账户输入一个密码,且必须是一个加密后的密码。如果此时不添加加密密码,日后还可以使用 passwd user 命令为用户添加密码(如果想要生成一个 MD5加密密码,请输入opensslpasswd)。
-s shell--指定账户所使用的命令Shell。请使用命令Shell替换shell(例如,-s /bin/csh)。
-u user_id--为账户指定用户ID号(例如,-u 793)。如果没有使用-u选项,默认行为是自动分配下一个可用的数字。请使用ID号替换user_id。
在创建账户的过程中,useradd 命令完成了以下活动:
读取/etc/login.defs 和 /etc/default/useradd 文件,获取创建账户时所使用的默认值。
检查命令行参数,找出哪些默认值被重写。
根据默认值和命令行参数,在/etc/passwd和/etc/shadow 文件中创建新用户条目。
在/etc/group文件中创建新的组条目(Fedora 使用新用户名创建组)。
在home目录中根据用户名创建一个主目录。
将/etc/skel目录中的所有文件复制到新的主目录中。其中通常包括登录和应用程序启动脚本。
如果想要查看其他的默认设置,可以参阅/etc/default/useradd 文件;此外还可以通过输入带有-D选项的 useradd 命令查看默认设置,如下所示:
# useradd-D
GROUP-100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE MAIL SPOOL=yes
还可以使用-D选项来更改默认值。当运行带有该选项的命令时,useradd不实际创建一个新用户账户;而是在/etc/default/useradd中将任何额外提供的选项保存为新默认值。并不是所有的useradd选项可以与-D选项一起使用。可以使用的只有5个选项,如下所示:
-b default_home--设置所创建的主目录所在的默认目录。用目录名替换default_home(例如,-b /freeoa)。通常默认目录为/home。
-e defautl_expire_date--设置禁用用户账户的默认的截止日期。应该使用格式为YYYY-MM-DD的日期替换 dafult_expire_date值(例如,-e 2022-10-17)。
-f default_inactive--设置密码过期后多少天账户被禁用。用一个表示天数的数字替换default_inactive(例如,-f7)。
-g default_group--设置新用户所在的默认组。通常useradd 命令创建与用户具有相同名称和 ID号的新组。用组名替换 default_group(例如,-g freeoa)。
-s default_shell--设置新用户的默认 Shell,通常为/bin/bash。用默认 Shell 的完整路径替换default_shell(例如,-s /usr/bin/ksh)。
如果想要设置任何默认值,可以首先提供-D选项,然后添加想要设置的默认值。例如,为将默认主目录设置为/home/everyone,将默认Shell 设置为/bin/tcsh,请输入以下命令:
#useradd -D -b/home/everyone -s /bin/tcsh
除了设置用户默认值,管理员还可以创建被复制到每个用户主目录的默认文件。这些文件可以包括登录脚本和 Shell 配置文件(比如.bashrc)。其他对于使用用户账户非常有用的命令包括usermod(用来修改现有账户的设置)和userdel(用来删除现有用户账户)。
usermod相关选项
-c username--更改与用户账户相关联的描述。请使用用户账户名替换username(-cjake)。如果输入多个单词,则使用双引号(例如,-c "Jake Jackson")。
-d home_dir--更改账户的主目录。默认情况下,主目录名称与登录名相同,且被放置在/home中。用目录名替换 home_dir例如,-d /mnt/homes/jake)。
-e expire_date --为账户分配一个 YYYY-MM-DD 格式的新过期日期。用一个日期替换expire date(对于2017年10月15日,使用-e 2017-10-15)。
-f-1--更改密码过期之后多少天永久禁用账户。默认值-1表示禁用该选项。而如何设置为0则表示密码过期后马上禁用账户。用一个数字替换-1。
-g group--更改用户所在的主组(/etc/group文件中所列出)。用组名替换group(例如,-gwheel)。
-G grouplist--将用户的附属组(secondary groups)设置为以逗号分隔的组列表。除了用户的私有组之外,如果用户还属于另一个组中,则还必须添加-a选项(-Ga)。如果没有添加,用户将只会属于新的组,而失去任何其他组的成员资格。
-l login_name更改账户的登录名。
-L--通过在/etc/shadow文件中的加密密码前添加一个感叹号,可以锁定账户。虽然账户被锁定,但仍然允许用户保持密码不变(使用-U选项解除账户锁定)。
-m--只有当使用-d选项时该选项才可用,并将用户主目录中的内容复制到新目录中。
-o--只能与-u uid 一起使用,才能取消 UID 必须唯一的限制。
-s shell--为账户指定一个不同的命令Shell。用命令Shell 替换 shell(例如,-s bash)。
-u user_id--更改账户的用户D号。用ID号替换 user_id(例如,-u 1081)。
-U--解除用户账户的锁定(通过删除加密密码开头的感叹号)。
一些示例:
#usermod -Ga sales,marketing, freeoa
为用户 freeoa 添加了增补组。-a选项(-Ga)确保将该增补组添加到用户chris 的所有现有组。如果没有使用-a,则 chris 的现有增补组将被清除,而分配给该用户的新组列表将只包括新的增补组。
只有拥有root权限的人才可以将用户分配到用户组。
每一个用户都被分配给一个主组。在RHEL中主组默认为与用户同名的一个新组。所以如果将用户命名为sara,那么分配给该用户的组也为sara。主组由/etc/passwd 文件中每一条目的第三个字段的数字来表示。例如,组ID1007如下所示:
sara:x:1002:1007:Sara Green:/home/sara:/bin/tcsh
该组在/etc/group 文件中条目的入口点为:
sara:x:1007:
用户sara可以属于多个增补组,或不属于任何增补组。用户可以使用newgrp指令来为不同的用户组创建文件,使该文件的主组文件为该用户组。还可以通过使用 newgrp 命令允许用户临时成为某一组的成员,而不是实际成为该组成员为此,拥有root 权限的人可以使用 gpasswd命令设置一个组密码(比如 gpasswd sales)。此后,任何用户通过输入 newgrp sales进入一个 Shell,当出现提示时输入组密码,从而临时使用 sales 作为其主组;可在完成任务后退出该Shell从而回到之前的终端shell。
通常0~999的组ID(GID)分配给特殊的管理组,root组与GID 0相关联。普通组从1000开始,也有部分Linux为管理组保留了0~500的GID。UID与GID可以平行存在。传统的组和用户管理有相当的局限性,后面又扩展为LDAP、NIS、Winbind服务验证用户身份,当然也有ACL权限管理体系。
About useradd
useradd creates a new user or sets the default information for new users.Description
useradd is a low-level utility for adding users to a system. In general, the more friendly adduser should be used instead.
Your operating system may come with a slightly different version of useradd; check your documentation before using it to create new accounts. This documentation refers to some options frequently used on Debian-based variants of Linux, but is representative of useradd's general use.
When invoked without the -D option, the useradd command creates a new user account using the values specified on the command line plus the default values from the system. Depending on command line options, the useradd command will update system files and may also create the new user's home directory and copy initial files.
By default, a group will also be created for the new user (see the -g, -N, -U options, and the USERGROUPS_ENAB variable, below).
useradd syntax
useradd [options] LOGINuseradd -D
useradd -D [options]
Options
| -c, --comment COMMENT | COMMENT can be any text string. It is generally a short description of the login, and is currently used as the field for the user's full name. |
| -d, --home HOME_DIR | The new user will be created using HOME_DIR as the value for the user's login directory. The default is to append the LOGIN name to BASE_DIR and use that as the login directory name. The directory HOME_DIR does not have to exist but will not be created if it is missing. |
| -D, --defaults | Set new default values. See the section on Changing New User Default Values, below. |
| -e, --expiredate EXPIRE_DATE | The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD. If not specified, useradd will use the default expiry date specified by the EXPIRE variable in /etc/default/useradd, or an empty string (no expiry) by default. |
| -f, --inactive INACTIVE | The number of days after a password expires until the account is permanently disabled. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature. If not specified, useradd will use the default inactivity period specified by the INACTIVE variable in /etc/default/useradd, or -1 by default. |
| -g, --gid GROUP | The group name or number of the user's initial login group. The group name must exist. A group number must refer to an already existing group. If not specified, the behavior of useradd will depend on the USERGROUPS_ENAB variable in /etc/login.defs. If this variable is set to yes (or -U/--user-group is specified on the command line), a group will be created for the user, with the same name as her loginname. If the variable is set to no (or -N/--no-user-group is specified on the command line), useradd will set the primary group of the new user to the value specified by the GROUP variable in /etc/default/useradd, or 100 by default. |
| -G, --groups GROUP1[,GROUP2,...[,GROUPN]]] | A list of groups containing the user as a member. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. The default is for the user to belong only to the initial group. |
| -h, --help | Display a help message, and exit. |
| -k, --skel SKEL_DIR | SKEL_DIR is the skeleton directory, which contains files and directories to be copied in the user's home directory, when the home directory is created by useradd. This option is only valid if the -m (or --create-home) option is specified. If this option is not set, the skeleton directory is defined by the SKEL variable in /etc/default/useradd or, by default, /etc/skel. If possible, the ACL and extended attributes are copied. |
| -K, --key KEY=VALUE | Overrides /etc/login.defs defaults (UID_MIN, UID_MAX, UMASK, PASS_MAX_DAYS and others). Example: -K PASS_MAX_DAYS=-1 can be used when creating system account to turn off password aging, even though system account has no password at all. Multiple -K options can be specified, for example: -K UID_MIN=100 -K UID_MAX=499 |
| -l, --no-log-init | Do not add the user to the lastlog and faillog databases. By default, the user's entries in the lastlog and faillog databases are resetted to avoid reusing the entry from a previously deleted user. For the compatibility with previous versions of useradd, the -O option is also supported for this purpose. |
| -m, --create-home | Create the user's home directory if it does not exist. The files and directories contained in the skeleton directory (which can be defined with the -k option) will be copied to the home directory. By default, if this option is not specified and CREATE_HOME is not enabled, no home directories are created. |
| -M | Do no create the user's home directory, even if the system wide setting from /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, but add the user to the group specified by the -g option or by the GROUP variable in /etc/default/useradd. The default behavior (if the -g, -N, and -U options are not specified) is defined by the USERGROUPS_ENAB variable in /etc/login.defs. |
| -o, --non-unique | Allow the creation of a user account with a duplicate (non-unique) UID. This option is only valid in combination with the -u option. |
| -p, --password PASSWORD | The encrypted password, as returned by crypt. The default is to disable the password. Note: This option is not recommended because the password (or encrypted password) will be visible by users listing the processes (for example, with the ps command). You should make sure the password respects the system's password policy. |
| -r, --system | Create a system account.System users will be created with no aging information in /etc/shadow, and their numeric identifiers are chosen in the SYS_UID_MIN-SYS_UID_MAX range, defined in /etc/login.defs, instead of UID_MIN-UID_MAX (and their GID counterparts for the creation of groups). Note that useradd will not create a home directory for such an user, regardless of the default setting in /etc/login.defs (CREATE_HOME). You have to specify the -m options if you want a home directory for a system account to be created. |
| -R, --root CHROOT_DIR | Apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory. |
| -s, --shell SHELL | The name of the user's login shell. The default is to leave this field blank, which causes the system to select the default login shell specified by the SHELL variable in /etc/default/useradd, or an empty string by default. |
| -u, --uid UID | The numerical value of the user's ID. This value must be unique, unless the -o option is used. The value must be non-negative. The default is to use the smallest ID value greater than or equal to UID_MIN and greater than every other user. See also the -r option and the UID_MAX description. |
| -U, --user-group | Create a group with the same name as the user, and add the user to this group. The default behavior (if the -g, -N, and -U options are not specified) is defined by the USERGROUPS_ENAB variable in /etc/login.defs. |
| -Z, --selinux-user SEUSER | The SELinux user for the user's login. The default is to leave this field blank, which causes the system to select the default SELinux user. |
Changing New User Default Values
When invoked with only the -D option, useradd will display the current default values. When invoked with -D plus other options, useradd will update the default values for the specified options.
Valid default-changing options are:
| -b, --base-dir BASE_DIR | The path prefix for a new user's home directory. The user's name will be affixed to the end of BASE_DIR to form the new user's home directory name, if the -d option is not used when creating a new account. This option sets the HOME variable in /etc/default/useradd. |
| -e, --expiredate EXPIRE_DATE | The date on which the user account is disabled. This option sets the EXPIRE variable in /etc/default/useradd. |
| -f, --inactive INACTIVE | The number of days after a password has expired before the account will be disabled. This option sets the INACTIVE variable in /etc/default/useradd. |
| -g, --gid GROUP | The group name or ID for a new user's initial group (when the -N/--no-user-group is used or when the USERGROUPS_ENAB variable is set to no in /etc/login.defs). The named group must exist, and a numerical group ID must have an existing entry. This option sets the GROUP variable in /etc/default/useradd. |
| -s, --shell SHELL | The name of a new user's login shell. This option sets the SHELL variable in /etc/default/useradd. |
Notes
The system administrator is responsible for placing the default user files in the /etc/skel/ directory (or any other skeleton directory specified in /etc/default/useradd or on the command line).
Caveats
You may not add a user to a NIS or LDAP group. This must be performed on the corresponding server.Similarly, if the username already exists in an external user database such as NIS or LDAP, useradd will deny the user account creation request.
It is usually recommended to only use usernames that begin with a lower case letter or an underscore, followed by lower case letters, digits, underscores, or dashes. They can end with a dollar sign. The regular expression which describes a valid username is:
[a-z_][a-z0-9_-]*[$]?
The only constraints are that usernames must neither start with a dash ('-') nor plus ('+') nor tilde ('~') nor contain a colon (':'), a comma (','), or a whitespace (space: ' ', end of line: '\n', tab: '\t', etc.). Note that using a slash ('/') may break the default algorithm for the definition of the user's home directory.
Usernames may only be up to 32 characters long.
Configuration
The following configuration variables in /etc/login.defs change the behavior of this tool:
| name | type | description |
|---|---|---|
| CREATE_HOME | boolean | Indicate if a home directory should be created by default for new users. This setting does not apply to system users, and can be overridden on the command line. |
| GID_MAX, GID_MIN | number | Range of group IDs used for the creation of regular groups by useradd, groupadd, or newusers. The default value for GID_MIN is 1000; the default for GID_MAX is 60000. |
| MAIL_DIR | string | The mail spool directory. This is needed to manipulate the mailbox when its corresponding user account is modified or deleted. If not specified, a compile-time default is used. |
| MAIL_FILE | string | Defines the location of the users mail spool files relatively to their home directory. |
The MAIL_DIR and MAIL_FILE variables are used by useradd, usermod, and userdel to create, move, or delete the user's mail spool.
| MAX_MEMBERS_PER_GROUP | number | Maximum members per group entry. When the maximum is reached, a new group entry (line) is started in /etc/group (with the same name, same password, and same group ID).The default value is 0, meaning that there are no limits in the number of members in a group. This feature (split group) can help to limit the length of lines in the group file. This is useful to make sure that lines for NIS groups are not larger than 1024 characters.If you need to enforce such limit, you can use 25. Note: split groups may not be supported by all tools, even advanced tools like the Shadow toolsuite. You should not use this variable unless you really need it. |
| PASS_MAX_DAYS | number | The maximum number of days a password may be used. If the password is older than this, a password change will be forced. If not specified, -1 will be assumed (which disables the restriction). |
| PASS_MIN_DAYS | number | The minimum number of days allowed between password changes. Any password changes attempted sooner than this will be rejected. If not specified, -1 will be assumed (which disables the restriction). |
| PASS_WARN_AGE | number | The number of days warning given before a password expires. A zero means warning is given only upon the day of expiration, a negative value means no warning is given. If not specified, no warning will be provided. |
| SYS_GID_MAX, SYS_GID_MIN | number | Range of group IDs used for the creation of system groups by useradd, groupadd, or newusers. The default value for SYS_GID_MIN is 101; the default value for SYS_GID_MAX is GID_MIN minus 1. |
| SYS_UID_MAX, SYS_UID_MIN | number | Range of user IDs used for the creation of system users by useradd or newusers. The default value for SYS_UID_MIN is 101; the default value of SYS_UID_MAX is UID_MIN minus 1. |
| UID_MAX, UID_MIN | number | Range of user IDs used for the creation of regular users by useradd or newusers. The default value for UID_MIN (resp. UID_MAX) is 1000 (resp. 60000). |
| UMASK | number | The file mode creation mask is initialized to this value. If not specified, the mask will be initialized to 022. useradd and newusers use this mask to set the mode of the home directory they create. It is also used by pam_umask as the default umask value. |
| USERGROUPS_ENAB | boolean | If set to yes, userdel will remove the user's group if it contains no more members, and useradd will create by default a group with the name of the user. |
Files
| /etc/passwd | User account information. |
| /etc/shadow | Secure user account information. |
| /etc/group | Group account information. |
| /etc/gshadow | Secure group account information. |
| /etc/default/useradd | Default values for account creation. |
| /etc/skel/ | Directory containing default files. |
| /etc/login.defs | Shadow password suite configuration. |
Exit Status
useradd exits with the following status, depending on what occurred:
| 0 | Everything was completed successfully. |
| 1 | Couldn't update the password file. |
| 2 | The syntax of the command was invalid. |
| 3 | One or more options were given an invalid argument. |
| 4 | User ID is already in use, and -o was not specified. |
| 6 | Specified group doesn't exist. |
| 9 | Username already in use. |
| 10 | Couldn't update the group file. |
| 12 | Couldn't create the home directory. |
| 14 | Couldn't update SE Linux user mapping. |
useradd examples
Note: For these commands to work you must have root privileges.
useradd -D
Displays the defaults for new users. Output resembles the following:
GROUP=1001
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
useradd newperson
Creates newperson as a new user. Once the new user has been added, you would need to use the passwd command to assign a password to the account.
Once a user has been created, you can modify any of the user settings, such as the user's home directory, using the usermod command.