Ubuntu 14.04:从命令行创建的新用户缺​​少功能

mar*_*004 18 command-line useradd

我正在尝试从 bash 命令行在 Ubuntu 14.04 LTS 中创建一个新用户。我使用以下命令:

sudo useradd -c "Samwise the Brave" sam    
sudo passwd sam    
Enter new UNIX password: hello-1234    
Retype new UNIX password: hello-1234    
passwd: password updated successfully
Run Code Online (Sandbox Code Playgroud)

创建这个新用户后,我遇到了 3 个问题:

  1. 我无法使用用户 sam 登录 Ubuntu。每当我登录时,我都会被送回登录屏幕。

  2. 当我查看/etc/passwd文件时,我可以看到没有为用户 sam 定义默认 shell:

    cat /etc/passwd | grep sam    
    sam:x:1003:1003:Samwise the Brave:/home/sam:
    
    Run Code Online (Sandbox Code Playgroud)
  3. 未创建 Sam 的主文件夹,即/home/sam不存在。

关于什么可能导致所有这些问题的任何线索?

这里需要注意的是,当我使用 Unity Control Center 创建用户时,不会出现这些问题。但是我希望能够使用命令行,因为我要创建数十个用户。

May*_*hux 31

首先请注意,最好使用 adduser 而不是 useradd。

现在回到你的命令:

您应该按以下方式运行命令:

sudo useradd -m -c "Samwise the Brave" sam  -s /bin/bash 
Run Code Online (Sandbox Code Playgroud)

man useradd

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

 -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.
Run Code Online (Sandbox Code Playgroud)

所以你错过了-s用来添加你的登录 shell 和-m创建你的家。

如果要同时添加多个用户,最好使用命令newusers。它将简化您的任务。

man newusers

DESCRIPTION

   The newusers command reads a file of user name and clear-text password
   pairs and uses this information to update a group of existing users or
   to create new users. Each line is in the same format as the standard
   password file (see passwd(5)) with the exceptions explained below:

   pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell
Run Code Online (Sandbox Code Playgroud)

这里有一些关于newusers命令的教程: