4

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

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

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

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?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Baba Mok
  • 61
  • 1
  • 1
  • 3

5 Answers5

3

You have to supply the password on stdin. Replace:

passwd $iuser

with:

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

or, as suggested by 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 passwd command wants.

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

mklement0
  • 382,024
  • 64
  • 607
  • 775
John1024
  • 109,961
  • 14
  • 137
  • 171
  • Thank you. I tried and I get this error Enter new UNIX password: Retype new UNIX password: Sorry, passwords do not match passwd: Authentication token manipulation error passwd: password unchanged – Baba Mok Mar 07 '15 at 20:25
  • 1
    @BabaMok: That suggests that you haven't entered the here-string correctly; perhaps try on a single line with `passwd "$iuser" <<<"$ipasswd"$'\n'"$ipasswd"`. Also, just to state what may be obvious to some: this must be run as root (with `sudo`). – mklement0 Mar 07 '15 at 20:31
  • @BabaMok When I run it I get: `Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully`. If you received the message "passwords do not match," it might be because a stray character, maybe a tab or space, was entered. – John1024 Mar 07 '15 at 20:34
  • 1
    Thank you so much both of you. It works! I changed from passwd $iuser to passwd "$iuser"<<<"$ipasswd"$'\n'"$ipasswd" and it works! Thank you again guys – Baba Mok Mar 07 '15 at 20:45
  • 1
    @mklement0 Thanks. I updated the answer with your suggestions. – John1024 Mar 07 '15 at 21:17
  • @John1024: I appreciate it; I hope it's OK that I incorporated your answer into mine, but I wanted to show how to make do without the file-descriptor redirection commands. – mklement0 Mar 07 '15 at 21:22
  • 1
    @mklement0 Just fine. And your answer got my +1. – John1024 Mar 07 '15 at 21:27
  • @mklement0 I am sorry because I am new in this website. I have clicked the check mark. Thank you you again for helping :) – Baba Mok Mar 09 '15 at 06:33
  • @BabaMok: You're welcome. I'm glad it worked for you. And: no worries about not accepting John's answer right away; we all were new to this site once. – mklement0 Mar 09 '15 at 12:48
2

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

Let me show the solution in context, without the file-descriptor acrobatics (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 reads corresponding lines from the two files and puts them on a single line, separated with \t.
  • The result is piped to stdin via a process substitution (<(...)).
  • This allows read to read from a single source.
  • $\n is an ANSI C-quoted string that produces a (literal) newline.
Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
0
   #! /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)

Manish Sakariya
  • 482
  • 3
  • 10
0

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" 
GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Kindly run the below script.

#!/bin/bash
#purpose: bash script to create multiple users with pre-defined passwords at once.
#Read_Me: The import file should be in two columns, first users name and second passwords.
#author: Bablish Jaiswal
#contact: linux.cnf@gmail.com

    read -p "Kindly import/type Users Name-password file with location:- " creation_info
    cat $creation_info |while read i p
    do
            ( useradd  $i   &&  echo -e "${p}\n${p}" |  passwd  $i )  > /dev/null 2>&1 && echo $user ${i} created and password is ${p} || echo ${i} failed 
    done
linux.cnf
  • 519
  • 6
  • 7