33

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?

Asped
  • 3,083
  • 4
  • 31
  • 52
Tampa
  • 75,446
  • 119
  • 278
  • 425

4 Answers4

62

Why not just use:

htpasswd -b -c ~/temp/password admin test101
GavinCattell
  • 3,863
  • 20
  • 22
  • 7
    Although it's old: Because all other users on the same system may be able see "test101" in /proc//cmdline for a short moment, if they are watching. – phi1010 Mar 04 '19 at 21:25
  • 2
    This is the case if your system has more than 1 users, for creating initial example to setup a server and generate htpasswd, this will have no problem. – MaXi32 Dec 25 '20 at 23:03
22

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
elim
  • 1,404
  • 1
  • 16
  • 17
12

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
Daniel Roethlisberger
  • 6,958
  • 2
  • 41
  • 59
9

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.

ender.center
  • 91
  • 1
  • 3