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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10164201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:01:53  来源:igfitidea点击:

How to automate generation of htpasswd from command line without typing password?

bash.htpasswd

提问by Tampa

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 htpasswdfrom command line without typing password?

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

回答by GavinCattell

Why not just use:

为什么不使用:

htpasswd -b -c ~/temp/password admin test101

回答by Daniel Roethlisberger

The -bswitch should do the trick, but the password will still be visible to other users on the system via the process list (psetc):

-b开关应该做的伎俩,但密码仍然会通过进程列表系统上的其他用户(可见光ps等):

htpasswd -b -c ~/temp/password admin test101

回答by elim

If you don't have htpasswdinstalled (for example when using nginx) you can generate a password with openssl.

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

printf "USER:$(openssl passwd -crypt PASSWORD)\n" >> .htpasswd

回答by ender.center

From the man page of htpasswd we get this:

从 htpasswd 的手册页我们得到:

-i Read the password from stdin without verification (for script usage).

-i 从标准输入中读取密码而无需验证(用于脚本使用)。

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.

然后删除 pass_plain 文件。

Also make sure the pass_plain file is not readable by anyone else, also if its just there for a few seconds.

还要确保 pass_plain 文件不被其他人读取,即使它只是在那里几秒钟。