adduser (将创建一个新用户)

瑞兹 发表于 2020-12-30 09:14
浏览次数:
在手机上阅读

在像 Unix 一样操作系统上,adduser命令将创建一个新用户,而 addgroup 命令将创建一个新组。

查看英文版

目录

1 adduser 运行系统环境

2 adduser 描述

3 adduser 语法

4 adduser 例子

adduser 运行系统环境

Unix&Linux

adduser 描述

如何使用adduser和addgroup

adduseraddgroupadduser和组到系统根据命令行选项和配置信息在/etc/adduser.conf。它们通过选择符合策略的 UID 和 GID 值、创建具有骨骼配置的主目录、运行自定义脚本和其他功能,对低级工具(如useradd、groupadd和usermod 程序)更友好的前端。

有五种主要方法可以使用adduser 和addgroup可以在五种模式之一中运行:

添加普通用户

如果使用一个非选项参数调用,并且没有--system-- group则 adduser将添加一个普通用户。

adduser将从配置文件中为普通用户指定的范围中选择第一个可用的 UID。UID 可以 b 覆盖-uid选项。

配置文件中指定的范围可能用--firstuid 和 --lastuid 选项覆盖。

默认情况下,GNU/Linux 系统中的每个用户都得到一个同名的相应组。用户组允许通过在新组中放置适当的用户、在目录中设置设置组 ID 位以及确保所有用户使用umask 002来轻松维护组可写目录。如果通过将 USERGROUPS 设置为"否"关闭此选项,则所有用户的 GID 都设置为USERS_GID。还可以使用--gid --ingroup选项从命令行覆盖用户的主要组,以便分别按 id 或 名称设置组。此外,用户可以通过在adduser.conf中设置 ADD_EXTRA_GROUPS 到1或在命令行上传递--add_extra_groups 添加到一个或多个组中。

adduser将创建一个主目录主题,主题为 DHOME、GrouphomeS 和 Letterhomes。可以使用--home选项从命令行重写主目录,也可以使用--shell选项覆盖主目录。如果 USERGROUPS是,将设置主目录的集组 ID以便用户主目录中创建的任何文件都将具有正确的组。

adduser将文件从 SKEL 复制到主目录,并提示手指(gecos) 信息和密码。也可以使用 --gecos 选项设置 gecos。使用--禁用登录选项,将创建该帐户,但将被禁用,直到设置密码。--禁用密码选项不会设置密码,但仍然可以登录(例如使用SSH RSA密钥)。若要为新用户设置加密主目录,请添加--加密主选项

如果文件/usr/本地/sbin/adduser.local存在,则将在用户帐户设置为执行任何本地设置后执行。传递给adduser.local 的参数是:"username uid gid home -directory"。

环境变量 VERBOSE 根据以下规则设置:

  • 如果--quiet被指定 
  • 如果未指定 --quiet --debug 被指定  
  • 如果-debug 被指定 

(这同样适用于变量DEBUG,但DEBUG已弃用,将在Adduser的较新版本中删除。

添加系统用户

如果使用一个非选项参数和--系统选项调用,则 adduser将添加一个系统用户。如果系统 uid 范围内已存在同名用户(或者,如果指定了 uid,则如果已存在该 uid 的用户),则adduser将退出并发出警告。可以通过添加"-quiet"来抑制此警告。

adduser将从配置文件(和 FIRST_SYSTEM_UID)中为系统用户指定的LAST_SYSTEM_UID。 如果要具有特定的 UID,可以使用--uid 选项指定它

默认情况下,系统用户被放置在"nogroup"组中。若要将新系统用户放在已存在的组中,请使用--gid--ingroup。若要将具有相同 ID 的新系统用户放在新组中,请使用--group选项。

主目录由与普通用户相同的规则创建。新系统用户将具有 shell /bin/false(除非使用--shell选项覆盖),并且已禁用登录名。不会复制骨骼配置文件。

adduser组

如果使用--group选项调用adduser,并且没有--system选项,或者调用addgroup,则将adduser组。

将从配置文件中为系统 GIDS 指定的范围(FIRST_GID,LAST_GID)。 若要重写该机制,可以使用--gid 选项为 GID。

创建组时没有用户。

添加系统组

如果使用--系统选项调用addgroup,则将添加系统组。

将从配置文件中为系统 GIDS 指定的范围(FIRST_SYSTEM_GIDLAST_SYSTEM_GID)中选择 GID。若要重写该机制,可以使用--gid 选项为 GID。

创建组时没有用户。

将现有用户添加到现有组

如果使用两个非选项参数运行 adduser,则它将现有用户添加到组 组,如果存在。

How to use adduser and addgroup

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 choosing policy-conformant UID and GID values, creating a home directory with skeletal configuration, running a custom script, and other features.

There are five major ways to use 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 b 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 a GNU/Linux system is given a corresponding group with the same name. User groups 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 command line.

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.

If the file /usr/local/sbin/adduser.local exists, it will be executed after the user account has been set up 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 rules:

  • if --quiet is specified 
  • if neither --quiet nor --debug is specified 
  • 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, a user group will be added.

A GID will be chosen from the range specified for system GIDS in the configuration file (FIRST_GIDLAST_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_GIDLAST_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 you run adduser with two non-option arguments, it will add existing user user to group group, if it exists.

查看英文版

查看中文版

adduser 语法

adduser [--system] [--home DIR] [--shell SHELL] [--no-create-home]
        [--uid ID] [--firstuid ID] [--lastuid ID]
        [--ingroup GROUP | --gid ID] [--disabled-password]
        [--disabled-login] [--gecos GECOS] [--add_extra_groups]
        [--encrypt-home] [--quiet] [--debug] [--force-badname]
        [--help|-h] [--version] [--conf FILE] user 
addgroup [--system] [--quiet] [--debug] [--force-badname] [--help|-h]
         [--version] [--conf FILE] [--gid ID] group 
adduser [options] user group 

选项

- - conf file

使用文件而不是/etc/adduser.conf

-- disabled-login

不要运行passwd来设置密码。在设置密码之前,用户将无法使用她的帐户。

--禁用密码

--disabled-login但登录仍然是可能的(例如使用 SSH RSA 密钥),但不使用密码身份验证。

- - disabled-password

默认情况下,根据配置文件中指定的可配置正则表达式 NAME_REGEX(NAME_REGEX_SYSTEM -system)检查用户和组名称。此选项强制添加user 和 add 组仅应用对名称有效性的弱检查。

- -force-badname

为生成的新条目设置 gecos 字段。如果提供了此选项,adduser 不会询问手指信息。

- - gid ID

创建组时,此选项强制新组 id 为给定数字。创建用户时,此选项将用户放在该组中。

- - group

--system 结合使用时,将创建与系统用户具有相同名称和 ID 的组。如果不与--system 组合使用 ,将创建一个具有给定名称的组。如果程序被调用为添加组,则这是默认操作。

- - help

显示帮助消息,然后退出。

- - home DIR

使用DIR作为用户主目录,而不是配置文件指定的默认值。如果该目录不存在,则创建该目录并复制骨架文件。

- - shell SHELL

使用SHELL作为用户的登录外壳,而不是配置文件指定的默认值。

-- ingroup GROUP

将新用户添加到GROUP,而不是配置文件中由用户USERS_GID的默认组。这会影响用户主组。若要添加其他组,请参阅add_extra_groups选项。

--no-create-home

不要创建主目录,即使它不存在。

- - quiet

禁止显示信息性消息,仅显示警告和错误。

- - debug

详细,这是最有用的,如果你想确定一个问题与adduser

- - system

创建系统用户或组。

- - uid ID

强制新用户 ID 为给定编号。如果已使用用户 ID,adduser 将失败。

- - firstuid ID

覆盖从 uid 选择的范围中的第一个 uid(FIRST_UID配置文件中指定的操作)。

- -lastuid ID

覆盖从 (从) 选择 uid 的范围内的最后一个 uid (LAST_UID )。

- add_extra_groups

将新用户添加到配置文件中定义的额外组。

- - version

显示版本和版权信息。

配置

文件/etc/adduser.conf包含程序的默认值,即adduseraddgroup、deluser 和 delgroup。每行在窗体 "option = value " 中保留单个值对。值周围允许双引号或单引号,equalss 符号周围的空格也允许。注释行在第一列中必须有一个哈希符号("\")。

有效的配置选项包括:

DSHELL

要用于所有新用户的登录外壳。默认值为/bin/bash

DHOME

应在其中创建新主目录的目录。默认值为/home

GROUPHOMES

如果设置为"是",则主目录将创建为"/home/ [groupname] /user"。默认值为

LETTERHOMES

如果设置为"yes",则创建主目录将插入一个额外的目录,该目录是登录名的第一个字母。例如: /home/u/user。默认值为no

SKEL

应从中复制骨骼用户配置文件的目录。默认值为/etc/skel

FIRST_SYSTEM_UID, LAST_SYSTEM_UID

指定可从中动态分配系统 UID 的包含范围。默认值为100 - 999。请注意,系统软件(如由 base-passwd 包分配的用户)可能假定小于 100 的 UID 未分配。

FIRST_UID, LAST_UID

指定可从中动态分配普通用户的 UID 的包含范围。默认值为1000 - 29999

FIRST_SYSTEM_GID, LAST_SYSTEM_GID

指定可从中动态分配系统 GID 的包含范围 GID。默认值为100 - 999

FIRST_GID, LAST_GID

指定可从中动态分配普通组的 GID 的包含范围。默认值为1000 - 29999

USERGROUPS

如果设置为"yes",则每个创建的用户都将获得自己的组以使用。如果"否",则每个创建的用户将被放置在 GID 为其USERS_GID(见下文)。默认值为"yes"。

USERS_GID

如果USERGROUPS  USERS_GID则是给所有新创建用户的 GID。默认值为100

DIR_MODE

如果设置为有效值(例如0755755),则创建的目录将具有指定为 umask 的指定权限。否则,0755将用作默认值。

SETGID_HOME

如果设置为"是",则具有自己组(USERGROUPS=yes)的用户主目录将设置 setgid 位。这是 3.13之前添加用户版本的默认设置。不幸的是,它有一些不好的副作用,所以我们不再这样做,每个默认值。如果你想要它,你仍然可以在这里激活它。

QUOTAUSER

如果设置为非空值,则新用户将从该用户复制配额。默认值为空。

NAME_REGEX

根据此正则表达式检查用户和组名称。如果名称与此 regexp 不匹配,则除非设置--force-badname,否则将拒绝在 adduser 中创建用户和组。使用--force-badname,仅执行弱检查。默认值是最保守的\a-z\-a-z0-9\*$。指定系统时,NAME_REGEX_SYSTEM使用系统。

NAME_REGEX_SYSTEM

根据此正则表达式检查系统用户的名称。如果提供 --system且名称与此 regexp 不匹配,则除非设置了--force-badname,否则将拒绝在 adduser 中创建用户。使用--force-badname,仅执行弱检查。默认值为默认字母,NAME_REGEX允许大写字母。

SKEL_IGNORE_REGEX

/etc/skel/ 中的文件将针对此正则表达式进行检查,如果它们匹配,则不会复制到新创建主目录。默认情况下,这设置为未合并的配置文件(dpkg-(old |new|dist))

ADD_EXTRA_GROUPS

将其设置为0以外的内容(默认值)将导致添加者将新创建的非系统用户添加到由 EXTRA_GROUPS 定义的组列表中。

EXTRA_GROUPS

这是将添加到新非系统用户的组列表。默认情况下,此列表是"拨号 cdrom 软盘音频视频插件用户游戏"。

退出值

0

用户存在指定。这有两个原因:用户是由添加用户创建的或者用户在调用添加用户之前已经在系统上出现。如果adduser返回 0,则第次调用 adduser时具有与以前相同的参数,也返回0

1

创建用户或组失败,因为它已与其他 UID/GID 一起存在,而不是指定。由于与配置的正则表达式不匹配,用户名或组名被拒绝。adduser已被信号中止。

或者,出于其他未记录的原因,可能会返回此代码,在这种情况下,这些原因将打印到控制台。然后,您可以考虑删除--quiet,使adduser更加详细。

文件

/etc/adduser.conf

addgroupadduser的默认配置文件

adduser [--system] [--home DIR] [--shell SHELL] [--no-create-home]
        [--uid ID] [--firstuid ID] [--lastuid ID]
        [--ingroup GROUP | --gid ID] [--disabled-password]
        [--disabled-login] [--gecos GECOS] [--add_extra_groups]
        [--encrypt-home] [--quiet] [--debug] [--force-badname]
        [--help|-h] [--version] [--conf FILE] user 
addgroup [--system] [--quiet] [--debug] [--force-badname] [--help|-h]
         [--version] [--conf FILE] [--gid ID] group 
adduser [options] user 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 a help message, and exit.

--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 user group or the default group defined by USERS_GID in the configuration file. This affects the users primary 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, which is most useful if you want to nail down a problem with adduser.

--system

Create a system user or group.

--uid ID

Force the new userid to be the given number. adduser will fail if the userid is already taken.

--firstuid ID

Override the first uid in the range that the uid is chosen from (overrides FIRST_UID specified in the configuration file).

--lastuid ID

Override the last uid in the range that the uid is chosen from ( LAST_UID ).

--add_extra_groups

Add new user to extra groups defined in the configuration file.

--version

Display version and Copyright information.

Configuration

The file /etc/adduser.conf contains defaults for the programs adduseraddgroupdeluser and delgroup. Each line holds a single value pair in the form "option = value". Double or single quotes are allowed around the value, as is whitespace around the equals sign. Comment lines must have a hash sign ("#") in the first column.

The valid configuration options are:

DSHELL

The login shell to be used for all new users. Defaults to /bin/bash.

DHOME

The directory in which new home directories should be created. Defaults to /home.

GROUPHOMES

If this is set to yes, the home directories will be created as "/home/[groupname]/user". Defaults to no.

LETTERHOMES

If this is set to yes, then the home directories created will have an extra directory inserted that is the first letter of the loginname. For example: /home/u/user. Defaults to no.

SKEL

The directory from which skeletal user configuration files should be copied. Defaults to /etc/skel.

FIRST_SYSTEM_UIDLAST_SYSTEM_UID

specify an inclusive range of UIDs from which system UIDs can be dynamically allocated. Default to 100 - 999. Please note that system software, such as the users allocated by the base-passwd package, may assume that UIDs less than 100 are unallocated.

FIRST_UIDLAST_UID

specify an inclusive range of UIDs from which normal user's UIDs can be dynamically allocated. Default to 1000 - 29999.

FIRST_SYSTEM_GIDLAST_SYSTEM_GID

specify an inclusive range of GIDs from which system GIDs can be dynamically allocated. Default to 100 - 999.

FIRST_GIDLAST_GID

specify an inclusive range of GIDs from which normal group's GIDs can be dynamically allocated. Default to 1000 - 29999.

USERGROUPS

If this is set to yes, then each created user will be given their own group to use. If this is no, then each created user will be placed in the group whose GID is USERS_GID (see below). The default is yes.

USERS_GID

If USERGROUPS is no, then USERS_GID is the GID given to all newly-created users. The default value is 100.

DIR_MODE

If set to a valid value (e.g., 0755 or 755), directories created will have the specified permissions as umask. Otherwise, 0755 is used as default.

SETGID_HOME

If this is set to yes, then home directories for users with their own group ( USERGROUPS=yes ) will have the setgid bit set. This was the default setting for adduser versions previous to 3.13. Unfortunately, it has some bad side effects, so we no longer do this per default. If you want it nevertheless you can still activate it here.

QUOTAUSER

If set to a nonempty value, new users will have quotas copied from that user. The default is empty.

NAME_REGEX

User and group names are checked against this regular expression. If the name doesn't match this regexp, user and group creation in adduser is refused unless --force-badname is set. With --force-badname set, only weak checks are performed. The default is the most conservative ^[a-z][-a-z0-9]*$. When --system is specified, NAME_REGEX_SYSTEM is used instead.

NAME_REGEX_SYSTEM

Names of system users are checked against this regular expression. If --system is supplied and the name doesn't match this regexp, user creation in adduser is refused unless --force-badname is set. With --force-badname set, only weak checks are performed. The default is as for the default NAME_REGEX but also allowing uppercase letters.

SKEL_IGNORE_REGEX

Files in /etc/skel/ are checked against this regex, and not copied to the newly created home directory if they match. This is by default set to the regular expression matching files left over from unmerged config files (dpkg-(old|new|dist)).

ADD_EXTRA_GROUPS

Setting this to something other than 0 (the default) will cause adduser to add newly created non-system users to the list of groups defined by EXTRA_GROUPS (below).

EXTRA_GROUPS

This is the list of groups that new non-system users will be added to. By default, this list is 'dialout cdrom floppy audio video plugdev users games'.

Exit values

0

The user exists as specified. This can have 2 causes: The user was created by adduser or the user was already present on the system before adduser was invoked. If adduser was returning 0, invoking adduser a second time with the same parameters as before also returns 0.

1

Creating the user or group failed because it was already present with other UID/GID than specified. The username or groupname was rejected because of a mismatch with the configured regular expressions. adduser has been aborted by a signal.

Or this code may be returned for other undocumented reasons, which in that case are printed to console. You may then consider to remove --quiet to make adduser more verbose.

Files

/etc/adduser.conf

Default configuration file for adduser and addgroup

查看英文版

查看中文版

adduser 例子

adduser mynewuser

创建新的用户帐户mynewuser

adduser将提示您为用户分配密码,并要求您提供可选的标识信息,如全名和电话号码

将为新用户创建一个主目录默认情况下,/home/mynewuser),并将创建一个特定于用户的组 mynewuser。骨架主目录的内容(默认情况下位于/etc/skel)将被复制到新的主目录中。

如果脚本/usr/local/sbin/adduser.local存在,它将执行参数用户名uid gid主目录,其中用户名mynewuser ,uidmynewuser 的用户 ID,gidmynewuser的组 ID,而 home 目录mynewuser的 home 目录。

adduser --home /home/mynewuserhome mynewuser

创建用户帐户mynewuser ,并在位置/home/mynewuserhome创建其主目录。

adduser mynewuser

Creates a new user account, mynewuser.

adduser will prompt you to assign a password for the user, and ask you for optional identifying information such as Full Name and Phone Number.

A home directory (by default, /home/mynewuser) will be created for the new user, and a user-specific group, mynewuser, will be created. The contents of the skeleton home directory, which by default is located at /etc/skel, will be copied into the new home directory.

If the script /usr/local/sbin/adduser.local exists, it will be executed with the arguments username uid gid home-directory, where username is mynewuseruid is mynewuser's user id, gid is mynewuser's group id, and home-directory is mynewuser's home directory.

adduser --home /home/mynewuserhome mynewuser

Creates the user account mynewuser, and creates their home directory at the location /home/mynewuserhome.

查看英文版

查看中文版

其他命令行

apt-cache | apt-get | ar | arch | arp | as | a2p | aspell | ac | at | awk | a2p | apropos | alias | agrep | addgroup |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条