25

我正在尝试从命令行自动创建密码。我已经尝试了以下方法,但它仍然不断要求输入密码。

echo "test101" | htpasswd -c ~/temp/password admin

如何在htpasswd不输入密码的情况下从命令行生成自动化?

4

4 回答 4

51

为什么不直接使用:

htpasswd -b -c ~/temp/password admin test101
于 2012-04-15T17:12:16.940 回答
14

如果您没有htpasswd安装(例如使用 nginx 时),您可以使用 openssl 生成密码。

printf "USER:$(openssl passwd -crypt PASSWORD)\n" >> .htpasswd
于 2018-10-05T09:58:11.483 回答
11

开关应该可以解决问题,-b但密码仍然可以通过进程列表(ps等)对系统上的其他用户可见:

htpasswd -b -c ~/temp/password admin test101
于 2012-04-15T17:13:08.080 回答
3

从 htpasswd 的手册页我们得到:

-i 从标准输入读取密码而不进行验证(用于脚本使用)。

因此,根据您的问题,这样的事情应该可以工作:

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.

于 2019-05-04T11:49:51.670 回答