8

I have a script that will add users from a file. It will add the users just fine as far as I can tell, but when trying to login the password supplied by the script does not work. I'm not sure if this is normal or not, but the /etc/shadow file also shows the correct passwords in plain text. I thought the shadow file is only supposed to show a hash?

Here is the code I have for the script:

#/bin/bash
file="/Scripts/FormattedFile.txt"
while IFS=: read -r f1 f2 f3 f4
do
validID=$( echo "$f3" | cut -b 4,5,6,7,8,9 )
comment="${f1} ${f2}"
useradd -m -p "$f3" -u "$validID" -c "$comment" "$f1"
echo "Added user $f1."
done < "$file"

The result is a user with the correct username, ID, and home directory, but is unable to login. I know that the script is reading the file correctly and the fields are also correct as pulled from the file. Field 3 is a 6 digit number taken from a longer 9 digit string.

Each record is of the format:

firstname:lastname:H00123456:[email protected] 

Records are separated by a newline.

2
  • I explored using passwd but it doesn't seem to want to take a stream of input.
    – Elite
    Oct 30, 2019 at 0:27
  • 1
    For what it's worth, chpasswd will update one or many passwords, reading username:cleartextpassword pairs from stdin. Oct 30, 2019 at 18:36

1 Answer 1

17

According to useradd --help:

-p, --password PASSWORD       encrypted password of the new account

So what you need to do is to pass the encrypted password:

-p $(openssl passwd "the_password")
1
  • 4
    Also if your default shell is set to /bin/false or /bin/nologin, they won't be able to log in. You need to add -s /bin/bash.
    – werfu
    Oct 30, 2019 at 14:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .