Linux

Linux + 如何從 bash 腳本執行 ssh-keygen 互動

  • July 18, 2017

請建議如何在不回答“ ssh-keygen -t rsa ”的問題的情況下生成文件id_rsa.pub

我的目標是從postgress USER的 bash 腳本執行 ssh-keygen ,它必須執行互動,或者可能以不同的語法執行 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):

嘗試ssh-keygen -t rsa -f id_rsa -P ''

-f標誌將指定文件位置,該-P標誌將指定密碼(在這種情況下為空)。

您可以使用 -N 參數指定密碼

   ssh-keygen -t rsa -N "passphrase"

引用自:https://serverfault.com/questions/862775