bash 编写脚本以使用预定义的密码创建多个用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28919191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:29:52  来源:igfitidea点击:

Write script to create multiple users with pre-defined passwords

linuxbashshellterminalpasswords

提问by Baba Mok

So I would like to make a script that create users from users.txt running

所以我想制作一个脚本,从运行的 users.txt 创建用户

useradd -m -s /bin/false users_in_the_users.txt

and fill the password from passwords.txt twice (to confirm the passwords)

并在passwords.txt中填写两次密码(以确认密码)

This is the script

这是脚本

#!/bin/bash

# Assign file descriptors to users and passwords files
exec 3< users.txt
exec 4< passwords.txt
exec 5< passwords.txt

# Read user and password
while read iuser <&3 && read ipasswd <&4 ; do
  # Just print this for debugging
  printf "\tCreating user: %s with password: %s\n" $iuser $ipasswd
  # Create the user with adduser (you can add whichever option you like)
  useradd -m -s /bin/false $iuser
  # Assign the password to the user, passwd must read it from stdin
  passwd $iuser
done

The problem is, it does not fill the passwords. And 1 more thing, I want the script to fill the passwords twice.

问题是,它没有填写密码。还有一件事,我希望脚本填写两次密码。

Any suggestions?

有什么建议?

采纳答案by John1024

You have to supply the password on stdin. Replace:

您必须在标准输入上提供密码。代替:

passwd $iuser

with:

和:

passwd "$iuser" <<<"$ipasswd
$ipasswd"

or, as suggested by mklement0:

或者,按照 mklement0 的建议:

passwd "$iuser" <<<"$ipasswd"$'\n'"$ipasswd"

The incantation <<<creates a here-string. The string that follows the <<<is provided as standard in to the command which precedes the <<<. In this case we provide the two copies of the password that the passwdcommand wants.

咒语<<<创建了一个here-string<<<后面的字符串作为标准提供给<<<. 在这种情况下,我们提供passwd命令所需的密码的两个副本。

(The script reads these passwords from a plain text file. I will assume that your situation is some special case for which this is not as dangerous as it normally would be.)

(脚本从纯文本文件中读取这些密码。我假设您的情况是一些特殊情况,这不像通常那样危险。)

回答by mklement0

John1024's answeris the correct one- his warning about reading passwords from plain-text files bears repeating.

John1024 的答案是正确的- 他关于从纯文本文件读取密码的警告值得重复。

Let me show the solution in context, without the file-descriptor acrobatics(exec 3<, ...):

让我在上下文中展示解决方案,没有文件描述符杂技( exec 3<, ...):

#!/bin/bash

# NOTE: Be sure to run this script with `sudo`.

# Read user and password
while read iuser ipasswd; do

  # Just print this for debugging.
  printf "\tCreating user: %s with password: %s\n" $iuser $ipasswd

  # Create the user with adduser (you can add whichever option you like).
  useradd -m -s /bin/false $iuser

  # Assign the password to the user.
  # Password is passed via stdin, *twice* (for confirmation).
  passwd $iuser <<< "$ipasswd"$'\n'"$ipasswd"

done < <(paste users.txt passwords.txt)
  • paste users.txt passwords.txtreads corresponding linesfrom the two files and puts them on a singleline, separated with \t.
  • The result is piped to stdin via a process substitution(<(...)).
  • This allows readto read from a single source.
  • $\nis an ANSI C-quoted stringthat produces a (literal) newline.
  • paste users.txt passwords.txt读出相应行从两个文件,并将它们放在一单个线,具有分离\t
  • 结果通过进程替换( <(...))传送到标准输入。
  • 这允许read从单个源读取。
  • $\n是一个ANSI C 引用的字符串,它产生一个(文字)换行符。

回答by Manish Sakariya

   #! /bin/bash

   for i in {1..100}
    do
      `sudo mkdir -p /root/Desktop/userm$i`
      `sudo useradd -m -d /root/Desktop/userm$i -s /bin/bash userm$i`
       echo "userm$i:userm$i" | chpasswd

   done

this will create 100 users.
user name will be (userm1-userm100).
home directory will be /root/Desktop/(userm1-user100)
password will be (userm1-userm100)

这将创建 100 个用户。
用户名将是 (userm1-userm100)。
主目录将是 /root/Desktop/(userm1-user100)
密码将是 (userm1-userm100)

回答by kesavan vinesh

Instead of using this line:

而不是使用这一行:

useradd -m -s /bin/false $iuser  

Try this one:

试试这个:

useradd -m -s /bin/false -p $ipasswd $iuser 

You don't actually need this:

你实际上并不需要这个:

passwd $iuser <<< "$ipasswd"$'\n'"$ipasswd" 

回答by vserey

Here is how by using a Python script that you can just run as below:

以下是使用 Python 脚本的方法,您可以按如下方式运行:

root@ubuntu:/# python multiUser.py user_password.txt

Read vichhaiy's blog Create Multiple Users in Linux using Python scriptfor details on muliUser.pyand user_password.txt.

阅读 vichhaiy 的博客使用 Python 脚本在 Linux 中创建多个用户,了解有关muliUser.py和 的详细信息user_password.txt