Linux + 如何从 bash 脚本交互运行 ssh-keygen

Linux + 如何从 bash 脚本交互运行 ssh-keygen

请指教如何生成文件id_rsa.pub不回答“ssh-keygen -t rsa

我的目标是从 bash 脚本运行 ssh-keygenpostgress 用户,并且它必须以交互方式运行,或者可能使用不同的语法运行id_rsa.pub?

示例-这种方法不具有交互性:

 su - postgres -c "ssh-keygen -t rsa"
 Generating public/private rsa key pair.
 Enter file in which to save the key (/var/lib/pgsql/.ssh/id_rsa):
 Enter passphrase (empty for no passphrase):
 Enter same passphrase again:
 Your identification has been saved in /var/lib/pgsql/.ssh/id_rsa.
 Your public key has been saved in /var/lib/pgsql/.ssh/id_rsa.pub.
 The key fingerprint is:
 a5:c8:fb:ff:fa:3b:55:59:9c:90:4e:28:35:f0:46:7c 
 The key's randomart image is:
 +--[ RSA 2048]----+
 |         .++..o..|
 |         .oo.E .o|
 |          oo+   o|
 |     . . o.  . ..|
 |      o S      . |
 |       .      .  |
 |      .      .   |
 |       .    .    |
 |        ..o++o   |
 +-----------------+



 # ls
 authorized_keys  id_rsa  id_rsa.pub  known_hosts

我已经尝试通过 EOF 循环执行以下操作来回答 ssh-keygen 交互,但没有成功

# su - postgres -c "ssh-keygen -t rsa
>
>
> <<<EOF"
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/pgsql/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):

答案1

尝试ssh-keygen -t rsa -f id_rsa -P ''

标志-f将指定文件位置,-P标志将指定密码(在本例中为空)。

答案2

您可以使用 -N 参数指定密码

    ssh-keygen -t rsa -N "passphrase"

答案3

这是从 postgresql 用户创建公钥的有效答案

   su - postgres -c " cat /dev/zero | ssh-keygen -q -N \"\" "

相关内容