Script to add users will add users but they cannot login

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:

while IFS=: read -r f1 f2 f3 f4
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: 

Records are separated by a newline.

Asked By: Elite


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")