AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 643411
Accepted
martin_0004
martin_0004
Asked: 2015-07-02 20:05:47 +0800 CST2015-07-02 20:05:47 +0800 CST 2015-07-02 20:05:47 +0800 CST

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

  • 772

我正在尝试从 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

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

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

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

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

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

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

command-line
  • 5 5 个回答
  • 81455 Views

5 个回答

  • Voted
  1. Best Answer
    Maythux
    2015-07-02T21:28:01+08:002015-07-02T21:28:01+08:00

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

    • adduser 和 useradd 有什么区别?
    • https://unix.stackexchange.com/questions/121071/what-does-adduser-do-that-useradd-doesnt

    现在回到你的命令:

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

    sudo useradd -m -c "Samwise the Brave" sam  -s /bin/bash 
    

    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.
    

    所以你想念用来-s添加你的登录外壳和-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
    

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

    • http://www.sanfoundry.com/newusers-command-usage-examples/
    • https://opensourcewin.wordpress.com/2013/03/04/how-to-create-multiple-users-in-linux-newusers-command/
    • 31
  2. Sai
    2016-09-29T09:25:23+08:002016-09-29T09:25:23+08:00

    创建用户

    创建用户的主目录

    定义登录shell

    useradd -m -d /home/username username -s /bin/bash
    

    删除用户

    删除器用户的主目录

    userdel -r username
    
    • 3
  3. Michael Bailey
    2015-07-02T21:21:11+08:002015-07-02T21:21:11+08:00

    虽然您缺少标志并且其他答案不一定是错误的,但如果您希望将来更全面,请考虑运行adduser 。它是 useradd 的一个更漂亮的版本。也就是说,它会默认创建一个主目录,这与 useradd 不同。另请注意,当它要求大量内容时,它会将内联存储在 /etc/passwd 文件中,您不必填写任何内容。

    • 1
  4. martin_0004
    2015-07-04T17:01:33+08:002015-07-04T17:01:33+08:00

    感谢大家。根据您的回答,我已经能够使用以下命令行解决问题。

    sudo useradd -c "Samwise the Brave" -m -s /bin/bash sam
    echo -e "hello-1234\nhello-1234" | passwd sam
    

    密码已设置passwd为已加密(否则“hello-1234”在 /etc/passwd 中将显示为未加密)。

    • 1
  5. Codeguy007
    2015-07-02T21:08:07+08:002015-07-02T21:08:07+08:00

    您在 useradd 命令中缺少标志。'-m' 创建用户目录,'-s /bin/bash' 添加 bash shell。默认是不创建用户目录并分配默认 shell。当我测试它时,我的系统做了同样的事情所以它看起来像 ubuntu 14.04 使用空白作为默认 shell。您无法登录,因为您没有 shell。

    在终端中为现有用户创建默认主目录(问题 335961,第 3 个答案)

    从 useradd 的 Ubuntu 联机帮助页

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

    如何将所有用户的默认 shell 更改为 bash?问题 335961

    • 0

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve