Shell script creates linux user account but password goes wrong

I wrote a shell script to create user accounts. The script reads the user account name and password from a text file and creates an account with this data. When I execute the script, it successfully creates accounts, but when I try to log in to these accounts, I cannot log in due to an error Invalid password, please try again.

Here is the script I used to create user accounts:

file_name="t.txt"

while read user pass
do
    useradd -p ${pass} ${user}
done < $file_name

EDIT-1: the t.txt file contains information about the user account: space, divided by user name and password per line. Here is a snippet of the file:

user1 abcXYZ
user2 DEFxyz
user3 ijkLMN

-2: , , : ( )

passwd: unrecognized option '--stdin'
Usage: passwd [options] [LOGIN]

Options:
  -a, --all                     report password status on all accounts
  <---------------------------------- SKIPPED ------------------------------->
  -x, --maxdays MAX_DAYS        set maximum number of days before password
                                change to MAX_DAYS

Adding user user1  with the password  abcXYZ123

+4
1

, /etc/shadow.

script ... . , , 8 , " ".

user1:abcXYZ123:16963:0:99999:7:::
user2:DEFxyz142:16963:0:99999:7:::
user3:ijkLM1564:16963:0:99999:7:::

[root@localhost ~]# cat t.txt s.txt
user1 abcXYZ123
user2 DEFxyz142
user3 ijkLM1564
user4 abcXYZ123
user5 DEFxyz142
user6 ijkLM1564

Script

#!/bin/bash -e
#Adding user1, user2, user3 using your method.
file_name="t.txt"
while read user pass
do
    #useradd ${user} -p ${pass}
    useradd -p ${pass} ${user}
    echo "Adding user "${user}"  with the password  "${pass}
done < $file_name

#Adding user4, user5, user6 using the recommended method.
file_name="s.txt"
while read user pass
do
    #useradd ${user} -p ${pass}
    useradd ${user}
    echo "${pass}" | passwd --stdin ${user}
    echo "Adding user "${user}"  with the password  "${pass}

done < $file_name

, /etc/shadow

user1:abcXYZ123:16963:0:99999:7:::
user2:DEFxyz142:16963:0:99999:7:::
user3:ijkLM1564:16963:0:99999:7:::
user4:$1$NpazYQAn$tlhfQLlP0CaFiUeNeK8HW.:16963:0:99999:7:::
user5:$1$4z8G4gvh$v0jzcV5xbhWixU1LG9mwW.:16963:0:99999:7:::
user6:$1$cBkcYJkJ$7A.j6E3gy/umUcVmY0tgt0:16963:0:99999:7:::

, , , . Ubuntu . stdin Rhel.

useradd ${user}
echo "${pass}" | passwd --stdin ${user}

, mkpasswd .

 useradd -p $(mkpasswd ${pass}) ${user}

Ubuntu Passwd stdin

echo ${user}:${pass} | /usr/sbin/chpasswd
+3

All Articles