mostafiz mostafiz - 5 months ago 13
Bash Question

Shell script creates linux user account but password goes wrong

I've written a shell script to create user accounts. The script reads user account name and password from a text file and create account with those info. When I execute the script it successfully creates the accounts, but when I try to log in to those accounts I can't log in due to

Invalid password, please try again
error.

Here is the script I used to create the user accounts:

file_name="t.txt"

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


EDIT-1:
t.txt
file contains the user account information: space separated username and password per line. Here is the snippet of the file:

user1 abcXYZ
user2 DEFxyz
user3 ijkLMN


EDIT-2:
When I follow the method recommended by Steve Kline it shows me following result: (still the created accounts can't be logged in with the given password)

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


Still the created accounts can't be logged in with the given password

Answer

So your current method is adding plaintext passwords to /etc/shadow.

Using your script as is... Created this. Mind you, I added a few digits at the end of your the password to match the standard 8 characters to suppress the "are you sure" prompts.

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

The users files

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

The 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

Using both syntaxes, produced these results in /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:::

So, therefor the method I posted works. Update: Ubuntu working method below. This the stdin is confirmed for Rhel based systems.

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

If your heart is absolutely set on using that method, I would suggest installing mkpasswd and using this syntax.

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

Update for Ubuntu Passwd stdin

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