How to automate generation of htpasswd from command line without typing password?
Question
I am trying to automate creating a password from the command line. I have tried the below but it still keeps asking for the password.
echo "test101" | htpasswd -c ~/temp/password admin
How to generate automate htpasswd
from command line without typing password?
La solution
Why not just use:
htpasswd -b -c ~/temp/password admin test101
Autres conseils
The -b
switch should do the trick, but the password will still be visible to other users on the system via the process list (ps
etc):
htpasswd -b -c ~/temp/password admin test101
If you don't have htpasswd
installed (for example when using nginx) you can generate a password with openssl.
printf "USER:$(openssl passwd -crypt PASSWORD)\n" >> .htpasswd
From the man page of htpasswd we get this:
-i Read the password from stdin without verification (for script usage).
So just acording to your question something like this should work:
echo "test101" | htpasswd -c -i ~/temp/password admin
But the password will be visible in history and process list.
To automate creating a password from the command line i would write the plain password to a file and do something like that:
htpasswd -c -i ~/temp/password admin < ~/temp/pass_plain
Afterwards delete the pass_plain file.
Also make sure the pass_plain file is not readable by anyone else, also if its just there for a few seconds.