Linux下useradd命令与adduser命令的区别(adduser更适合初级使用者,useradd比较适合有些高阶经验的使用者)

Posted Dontla

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下useradd命令与adduser命令的区别(adduser更适合初级使用者,useradd比较适合有些高阶经验的使用者)相关的知识,希望对你有一定的参考价值。

文章目录

Linux下useradd命令与adduser命令的区别

Linux下创建用户时会用到useradd和adduser这两个命令,他们的区别如下:

1.使用useradd时,如果后面不添加任何参数选项,例如:#sudo useradd test创建出来的用户将是默认“三无”用户:一无Home Directory,二无密码,三无系统Shell。

2.使用adduser时,创建用户的过程更像是一种人机对话,系统会提示你输入各种信息,然后会根据这些信息帮你创建新用户。

  • adduser会提示设置密码,而useradd不会。
  • adduser会创建用户目录,比如/home/freebird freebird是用户,useradd不会
  • dduser会创建用户目录,比如/home/freebird freebird是用户,useradd不会
  • adduser会询问全名,房间号码,电话号码等用户信息,useradd不会

在Linux中,adduser更适合初级使用者,因为不用去记那些繁琐的参数选项,只要跟着系统的提示一步一步进行下去就行,缺点就是整个创建过程比较复杂而漫长;

而useradd比较适合有些高阶经验的使用者,往往一行命令加参数就能解决很多问题,所以创建起来十分方便。

参考文章:Linux下useradd与adduser的区别

man useradd

USERADD(8)                                                                 系统管理命令                                                                 USERADD(8)

名称
       useradd - 创建一个新用户或更新默认新用户信息

大纲
       useradd [选项] 登录

       useradd -D

       useradd -D [选项]

描述
       useradd is a low level utility for adding users. On Debian, administrators should usually use adduser(8) instead.

       如果使用时不带 -D 选项,useradd 命令使用命令行上指定的值和系统的默认值创建一个新用户。根据命令行选项,useradd
       命令也会更新系统文件和创建新用户的主目录并复制初始文件。

       默认上,也会为用户创建组 (察看 -g, -N, -U,和 USERGROUPS_ENAB)。

选项
       useradd 可以使用的选项有:

       -b, --base-dir BASE_DIR
           如果没有使用 -d HOME_DIR,则使用默认的基目录。BASE_DIR 加上账户名就是主目录。如果没有使用 -m 选项,BASE_DIR 必须已经存在。

           如果选项没有指定,useradd 将使用 /etc/default/useradd 中的 HOME 变量,或者默认的 /home。

       -c, --comment COMMENT
           任何字符串。通常是关于登录的简短描述,当前用于用户全名。

       -d, --home-dir HOME_DIR
           将创建新用户,并使用 HOME_DIR 作为用户登陆目录的值。默认值是将 LOGIN 名附加到 BASE_DIR 后面,并使用这作为登陆目录。目录不一定必须已经存在
           HOME_DIR,但是会在需要时创建。

       -D, --defaults
           看下边,“更改默认值”子节。

       -e, --expiredate EXPIRE_DATE
           用户账户将被禁用的日期。日期以 YYYY-MM-DD 格式指定。

           如果没有指定,useradd 将使用 /etc/default/useradd 中 EXPIRE 变量指定的默认过期日期,或者一个空字符串(不过期)-f, --inactive INACTIVE
           密码过期后,账户被彻底禁用之前的天数。0 表示立即禁用,-1 表示禁用这个功能。

           如果未指定,useradd 将使用 /etc/default/useradd 中的 INACTIVE 指定的默认禁用周期,或者默认为 -1-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 选项给定的组同样的限制。默认上,用户只属于初始组。

       -h, --help
           现实帮助信息并退出。

       -k, --skel SKEL_DIR
           骨架目录,包含使用 useradd 创建用户时,要复制到用户主目录中的文件和目录。

           这个选项只有在指定 -m (--create-home) 选项时才有效。

           如果此项没有设置,骨架目录使用 /etc/default/useradd 中的 SKEL 的变量或默认为 /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
           不要将用户添加到最近登录和登录失败数据库。

           默认上,最近登录和登录失败中用户的条目会被重置,以避免重新使用先前删除的用户的条目。

           For the compatibility with previous Debian's useradd, the -O option is also supported.

       -m, --create-home
           如果不存在,则创建用户主目录。骨架目录中的文件和目录(可以使用 -k 选项指定),将会复制到主目录。

           默认上,如果没有指定此选项并且 CREATE_HOME 没有启用,不会创建主目录。

       -M
           不创建用户主目录,即使系统在 /etc/login.defs 中的设置 (CREATE_HOME) 为 yes。

       -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
           创建一个系统账户。

           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).

           注意:useradd 不会为这种用户创建主目录,无论 /etc/login.defs (CREATE_HOME) 中是的默认设置是怎样。如果想为要创建的系统账户创建主目录,需要指定 -m 选项。

       -R, --root CHROOT_DIR
           Apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory.

       -s, --shell SHELL
           用户的登录 shell 名。默认为留空,让系统根据 /etc/default/useradd 中的 SHELL 变量选择默认的登录 shell,默认为空字符串。

       -u, --uid UID
           用户 ID 的数字值。此值必须为唯一的,除非使用了 -o 选项。此值必须非负,默认使用大于等于 UID_MIN,且大于任何其他用户 ID 最小值。

           请参考 -r 选项和 UID_MAX 的描述。

       -U, --user-group
           创建一个和用户同名的组,并将用户添加到组中。

           如果没有指定 -g, -N 和 -U 选项,默认行为由 /etc/login.defs 中的 USERGROUPS_ENAB 变量指定。

       -Z, --selinux-user SEUSER
           用户登陆的 SELinux 用户。默认为留空,这会造成系统选择默认的 SELinux 用户。

   更改默认值
       只带 -D 选项使用时,useradd 将显示当前的默认值。-D 和其它选项配合使用时,useradd 将为指定的选项更新默认值。有效的“更改默认值”选项有:

       -b, --base-dir BASE_DIR
           新用户主目录的路径前缀。如果创建新账户时,没有使用 -d 选项,用户的名称将会缀在 BASE_DIR 的后边形成新用户的主目录名。

           这个选择在 /etc/default/useradd 中设置 HOME 选项。

       -e, --expiredate EXPIRE_DATE
           禁用此用户账户的日期。

           此选项在 /etc/default/useradd 中设置 EXPIRE 变量。

       -f, --inactive INACTIVE
           密码过期到账户被禁用之前的天数。

           这个选项在 /etc/default/useradd 中设置 INACTIVE 变量。

       -g, --gid GROUP
           新用户初始组的组名或 ID (使用了 -N/--no-user-group 或者 /etc/login.defs 中的变量 USERGROUPS_ENAB 设置为 no 时)。给出的组必须存在,并且数字组 ID
           必须有一个已经存在的项。

           这个选项在 /etc/default/useradd 中设置 GROUP 变量。

       -s, --shell SHELL
           新用户的登录 shell 名。

           这个选项在 /etc/default/useradd 设置 SHELL 变量。

注意:
       系统管理员负责将默认的用户文件放在 /etc/skel/ 目录中(或者命令行上、/etc/default/useradd 中指定的任何其它目录)。

CAVEATS
       您可能不能想 NIS 组或 LDAP 组添加用户。这只能在相应服务器上进行。

       相似地,如果用户名已经存在于外部用户数据库中,比如 NIS 或 LDAP,useradd 将拒绝创建用户账户的请求。

       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. In regular expression terms: [a-z_][a-z0-9_-]*[$]?

       On Debian, 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', tabulation: '\\t', etc.). Note that using a slash ('/') may break the default algorithm for the
       definition of the user's home directory.

       用户名不能超过 32 个字符长。

配置文件
       在 /etc/login.defs 中有如下配置变量,可以用来更改此工具的行为:

       CREATE_HOME (boolean)
           指示是否应该为新用户默认创建主目录。

           此设置并不应用到系统用户,并且可以使用命令行覆盖。

       GID_MAX (number), GID_MIN (number)
           useradd,groupadd 或 newusers 创建的常规组的组 ID 的范围。

           GID_MIN 和 GID_MAX 的默认值分别是 100060000MAIL_DIR (string)
           邮箱目录。修改或删除用户账户时需要处理邮箱,如果没有指定,将使用编译时指定的默认值。

       MAIL_FILE (string)
           定义用户邮箱文件的位置(相对于主目录)。

       MAIL_DIR and MAIL_FILE 变量由 useradd,usermod 和 userdel 用于创建、移动或删除用户邮箱。

       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 表示只有只在过期的当天警告,负值表示不警告。如果没有指定,不会给警告。

       SUB_GID_MIN (number), SUB_GID_MAX (number), SUB_GID_COUNT (number)
           If /etc/subuid exists, the commands useradd and newusers (unless the user already have subordinate group IDs) allocate SUB_GID_COUNT unused group IDs
           from the range SUB_GID_MIN to SUB_GID_MAX for each new user.

           The default values for SUB_GID_MIN, SUB_GID_MAX, SUB_GID_COUNT are respectively 100000, 600100000 and 10000.

       SUB_UID_MIN (number), SUB_UID_MAX (number), SUB_UID_COUNT (number)
           If /etc/subuid exists, the commands useradd and newusers (unless the user already have subordinate user IDs) allocate SUB_UID_COUNT unused user IDs
           from the range SUB_UID_MIN to SUB_UID_MAX for each new user.

           The default values for SUB_UID_MIN, SUB_UID_MAX, SUB_UID_COUNT are respectively 100000, 600100000 and 10000.

       SYS_GID_MAX (number), SYS_GID_MIN (number)
           useradd、groupadd 或 newusers 创建的系统组的组 ID 的范围。

           SYS_GID_MIN 和 SYS_GID_MAX 的默认值分别是 101 和 GID_MIN-1SYS_UID_MAX (number), SYS_UID_MIN (number)
           useradd 或 newusers 创建的系统用户的用户 ID 的范围。

           SYS_UID_MIN 和 SYS_UID_MAX 的默认值分别是 101 和 UID_MIN-1UID_MAX (number), UID_MIN (number)
           useradd 或 newusers 创建的普通用户的用户 ID 的范围。

           UID_MIN 和 UID_MAX 的默认值分别是 100060000UMASK (number)
           文件模式创建掩码初始化为此值。如果没有指定,掩码初始化为 022。

           useradd 和 newusers 使用此掩码设置它们创建的用户主目录的模式。

           也被 pam_umask 用作默认 umask 值。

       USERGROUPS_ENAB (boolean)
           如果设置为 yes,如果组中没有成员了,userdel 将移除此用户组,useradd 创建用户时,也会创建一个同名的默认组。

文件
       /etc/passwd
           用户账户信息。

       /etc/shadow
           安全用户账户信息。

       /etc/group
           组账户信息。

       /etc/gshadow
           安全组账户信息。

       /etc/default/useradd
           账户创建的默认值。

       /etc/skel/
           包含默认文件的目录。

       /etc/subgid
           Per user subordinate group IDs.

       /etc/subuid
           Per user subordinate user IDs.

       /etc/login.defs
           Shadow 密码套件配置。

退出值
       useradd 可以返回如下值:

       0
           成功

       1
           无法更新密码文件

       2
           无效的命令语法

       3
           给了选项一个无效的参数

       4
           UID 已经使用 (且没有 -o)

       6
           指定的组不存在

       9
           用户名已被使用

       10
           无法更新组文件

       12
           无法创建主目录

       14
           can't update SELinux user mapping

参见
       chfn(1), chsh(1), passwd(1), crypt(3), groupadd(8), groupdel(8), groupmod(8), login.defs(5), newusers(8), subgid(5), subuid(5), userdel(8), usermod(8).

shadow-utils 4.2                                                            2019-08-30                                                                  USERADD(8)
 Manual page useradd(8) line 281/322 (END) (press h for help or q to quit)

man adduser

ADDUSER(8)                                                            System Manager's Manual                                                           ADDUSER(8)

NAME
       adduser, addgroup - add a user or group to the system

SYNOPSIS
       adduser  [options] [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID] [--firstuid ID] [--lastuid ID] [--ingroup GROUP | --gid ID] [--disabled-pass‐
       word] [--disabled-login] [--gecos GECOS] [--add_extra_groups] [--encrypt-home] user

       adduser --system [options] [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID] [--group | --ingroup GROUP | --gid ID] [--disabled-password]  [--dis‐
       abled-login] [--gecos GECOS] user

       addgroup [options] [--gid ID] group

       addgroup --system [options] [--gid ID] group

       adduser [options] user group

   COMMON OPTIONS
       [--quiet] [--debug] [--force-badname] [--help|-h] [--version] [--conf FILE]

DESCRIPTION
       adduser  and  addgroup  add  users and groups to the system according to command line options and configuration information in /etc/adduser.conf.  They are
       friendlier front ends to the low level tools like useradd, groupadd and usermod programs, by default choosing Debian policy conformant UID and GID  values,
       creating a home directory with skeletal configuration, running a custom script, and other features.  adduser and addgroup can be run in one of five modes:

   Add a normal user
       If called with one non-option argument and without the --system or --group options, adduser will add a normal user.

       adduser  will choose the first available UID from the range specified for normal users in the configuration file.  The UID can be overridden with the --uid
       option.

       The range specified in the configuration file may be overridden with the --firstuid and --lastuid options.

       By default, each user in Debian GNU/Linux is given a corresponding group with the same name.  Usergroups allow group  writable  directories  to  be  easily
       maintained  by  placing  the  appropriate users in the new group, setting the set-group-ID bit in the directory, and ensuring that all users use a umask of
       002.  If this option is turned off by setting USERGROUPS to no, all users' GIDs are set to USERS_GID.  Users' primary groups can also  be  overridden  from
       the  command line with the --gid or --ingroup options to set the group by id or name, respectively.  Also, users can be added to one or more groups defined
       in adduser.conf either by setting ADD_EXTRA_GROUPS to 1 in adduser.conf, or by passing --add_extra_groups on the commandline.

       adduser will create a home directory subject to DHOME, GROUPHOMES, and LETTERHOMES.  The home directory can be overridden from the command  line  with  the
       --home  option,  and  the shell with the --shell option. The home directory's set-group-ID bit is set if USERGROUPS is yes so that any files created in the
       user's home directory will have the correct group.

       adduser will copy files from SKEL into the home directory and prompt for finger (gecos) information and a password.  The gecos may also  be  set  with  the
       --gecos option.  With the --disabled-login option, the account will be created but will be disabled until a password is set. The --disabled-password option
       will not set a password, but login is still possible (for example with SSH RSA keys).  To set up an encrypted home directory for  the  new  user,  add  the
       --encrypt-home option.  For more information, refer to the -b option of ecryptfs-setup-private(1).

       If the file /usr/local/sbin/adduser.local exists, it will be executed after the user account has been set up in order to do any local setup.  The arguments
       passed to adduser.local are:
       username uid gid home-directory
       The environment variable VERBOSE is set according to the following rule:

       0 if --quiet is specified

       1 if neither --quiet nor --debug is specified

       2 if --debug is specified

              (The same applies to the variable DEBUG, but DEBUG is deprecated and will be removed in a later version of adduser.)

   Add a system user
       If called with one non-option argument and the --system option, adduser will add a system user. If a user with the same name already exists in  the  system
       uid range (or, if the uid is specified, if a user with that uid already exists), adduser will exit with a warning. This warning can be suppressed by adding
       "--quiet".

       adduser will choose the first available UID from the range specified for system users in the configuration file (FIRST_SYSTEM_UID and LAST_SYSTEM_UID).  If
       you want to have a specific UID, you can specify it using the --uid option.

       By  default,  system users are placed in the nogroup group.  To place the new system user in an already existing group, use the --gid or --ingroup options.
       To place the new system user in a new group with the same ID, use the --group option.

       A home directory is created by the same rules as for normal users.  The new system user will have the shell /bin/false (unless overridden with the  --shell
       option), and have logins disabled.  Skeletal configuration files are not copied.

   Add a user group
       If adduser is called with the --group option and without the --system option, or addgroup is called respectively, a user group will be added.

       A  GID will be chosen from the range specified for system GIDS in the configuration file (FIRST_GID, LAST_GID). To override that mechanism you can give the
       GID using the --gid option.

       The group is created with no users.

   Add a system group
       If addgroup is called with the --system option, a system group will be added.

       A GID will be chosen from the range specified for system GIDS in the configuration file (FIRST_SYSTEM_GID, LAST_SYSTEM_GID). To override that mechanism you
       can give the GID using the --gid option.

       The group is created with no users.

   Add an existing user to an existing group
       If called with two non-option arguments, adduser will add an existing user to an existing group.

OPTIONS
       --conf FILE
              Use FILE instead of /etc/adduser.conf.

       --disabled-login
              Do not run passwd to set the password.  The user won't be able to use her account until the password is set.

       --disabled-password
              Like --disabled-login, but logins are still possible (for example using SSH RSA keys) but not using password authentication.

       --force-badname
              By  default, user and group names are checked against the configurable regular expression NAME_REGEX (or NAME_REGEX_SYSTEM if --system is specified)
              specified in the configuration file. This option forces adduser and addgroup to apply only a weak check for validity of the name.

       --gecos GECOS
              Set the gecos field for the new entry generated.  adduser will not ask for finger information if this option is given.

       --gid ID
              When creating a group, this option forces the new groupid to be the given number.  When creating a user, this option  will  put  the  user  in  that
              group.

       --group
              When combined with --system, a group with the same name and ID as the system user is created.  If not combined with --system, a group with the given
              name is created.  This is the default action if the program is invoked as addgroup.

       --help Display brief instructions.

       --home DIR
              Use DIR as the user's home directory, rather than the default specified by the configuration file.  If the directory does not exist, it  is  created
              and skeleton files are copied.

       --shell SHELL
              Use SHELL as the user's login shell, rather than the default specified by the configuration file.

       --ingroup GROUP
              Add  the  new user to GROUP instead of a usergroup or the default group defined by USERS_GID in the configuration file.  This affects the users pri‐
              mary group.  To add additional groups, see the add_extra_groups option

       --no-create-home
              Do not create the home directory, even if it doesn't exist.

       --quiet
              Suppress informational messages, only show warnings and errors.

       --debug
              Be verbose, most useful if you want to nail down a problem with adduser.

       --system
              Create a system user or grouplinux用户管理 -- 创建用户(adduser和useradd)和删除用户(userdel)

Linux命令: useradd/adduser 新增用户

Ubuntu中useradd和adduser的区别

Ubuntu下useradd与adduser区别

useradd与adduser的区别

useradd与adduser的区别