You should pass the -p
option to useradd
instead of trying to change the password after the fact.
From man useradd
-p, --password PASSWORD The encrypted password, as returned by crypt(3). The default is to disable the account.
Using -p you will need to have the password encrypted beforehand. Also, see this Stack Overflow question which provides similar answers and a script to generate the hashed password dynamically.