Write a script to create multiple users with predefined passwords

So, I would like to create a script that will create users from user.txt executing

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

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

This is a 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 that it does not fill out passwords. And one more thing, I want the script to populate passwords twice.

Any suggestions?

+4
source share
5 answers

You must specify a password on stdin. Replace:

passwd $iuser

with:

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

or as suggested by mklement0:

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

<<< -. , <<<, , <<<. , passwd.

(script . , - , , .)

+3

John1024 . - .

- (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.txt , \t.
  • stdin (<(...)).
  • read .
  • $\n ANSI C, () .
+2
   #! /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

100 .
(userm1-userm100).
home directory /root/Desktop/ (userm1-user100)
(userm1-userm100)

0

:

useradd -m -s /bin/false $iuser  

:

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

:

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

Python script, , :

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

Read the vichhaiy blog. Create multiple users on Linux using the Python script for more information on muliUser.pyand user_password.txt.

0
source

All Articles