使用多个命令选项创建用户

Man*_*har 5 command-line user-management

是否可以使用特定的默认主目录创建用户并使用单个命令将用户添加到特定的主要组?

gun*_*ert 8

是的,您可以通过使用更友好的前端adduser而不是useradd.

sudo adduser --home /path/to/desired/homedirectory --ingroup specialgroup  newuser
Run Code Online (Sandbox Code Playgroud)


Vid*_*uth 5

您可以使用useradd. 我添加了这一行来证明这是可能的。一般来说passwd,在手动执行之后运行更安全,也是最好的方法。

sudo useradd -U -m -G <group> -p <password> <user-name>
Run Code Online (Sandbox Code Playgroud)

来自man useradd

   -G, --groups GROUP1[,GROUP2,...[,GROUPN]]]
       A list of supplementary groups which the user is also a member of.
       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.

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

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

   -p, --password PASSWORD
       The encrypted password, as returned by crypt(3). 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.

       You should make sure the password respects the system's password
       policy.
Run Code Online (Sandbox Code Playgroud)

所以你最好分两步做,这样系统就不会记录密码。

sudo useradd -U -m -G <group> <user-name>
sudo passwd <user-name>
Run Code Online (Sandbox Code Playgroud)

你甚至可以把它变成一个单行动作:

sudo useradd -U -m -G <group> <user-name> && sudo passwd <user-name>
Run Code Online (Sandbox Code Playgroud)