以非交互方式运行 adduser

Lor*_*ein 275 adduser

我想使用该adduser命令通过 shell 脚本添加用户(密码已禁用)。

默认情况下,adduser会提示您输入各种值(例如,全名)。有没有办法通过命令行提交这些值?还是我需要useradd代替?

Zok*_*oke 349

使用--gecos选项跳过chfn交互部分。

adduser --disabled-password --gecos "" username
Run Code Online (Sandbox Code Playgroud)

这一切都在手册页中。不是最明显的公式。

--gecos GECOS
          Set  the  gecos field for the new entry generated.  adduser will
          not ask for finger information if this option is given.
Run Code Online (Sandbox Code Playgroud)

GECOS 字段是一个逗号分隔的列表,例如:Full name,Room number,Work phone,Home phone,尽管手册页提到了finger information 详细信息 - 维基百科

希望这对你有帮助。

  • 贝尔实验室的一些早期 Unix 系统使用 GECOS 机器进行打印假脱机和各种其他服务,因此添加此字段来携带有关用户 GECOS 身份的信息(来源:https://en.wikipedia.org/wiki/Gecos_field) (2认同)
  • @Ragtime,这只适用于系统帐户。在调用 `adduser`/`useradd` 之前,最好使用 `getent passwd <username>` 来测试用户是否存在。 (2认同)

Tho*_*ner 60

useradd can also add users and does not appear to have any form of prompting built in.

useradd -m -p <encryptedPassword> -s /bin/bash <user>
Run Code Online (Sandbox Code Playgroud)
  • -m, --create-home: Create user home directory
  • -p, --password: Specify user password; skip to have it disabled
  • -s, --shell: Default shell for logon user

    Blank will use default login shell specified by the SHELL variable in /etc/default/useradd

  • Substitute <user> with the login name
  • Substitute <encryptedPassword> with the encrypted password

Generating a hashed password:

There are a lot of crypt3 implementations that can generate a hashed password. The whole thing is your hashed password.

Sha-512 Based

结果输出格式:散列机制($6对于 sha-512),随机盐(第二个美元符号后的八个字节$ASDF1234),余数是有效载荷。

  • 密码 mkpasswd -m sha-512

    mkpasswdwhois包提供)

基于 DES:

结果输出格式:前 2 个字节是您的盐,其余是有效负载。整件事就是你的散列密码。

  • mkpasswd:(mkpasswdwhois包提供)
  • 打开SSL: openssl passwd -crypt
  • 珀尔: perl -e "print crypt('password');"
  • Python: python3 -c 'import crypt; print(crypt.crypt("password"))'

  • @ᴠɪɴᴄᴇɴᴛ `adduser` 与 `useradd` 不同,我知道令人困惑。 (2认同)

Bru*_*sky 7

你可以像这样结合@ThorSummoner @Zoke 所说的:

username=jovyan
password=jovyan

adduser --gecos "" --disabled-password $username
chpasswd <<<"$username:$password"
Run Code Online (Sandbox Code Playgroud)

我正在为我的 Jupyter docker-stack 做这个。它允许在 Dockerfile 中进行完整的无头设置。

  • +1 表示“chpasswd”。比手动使用“mkpasswd”来生成哈希值要容易得多,而且对脚本更友好。 (3认同)