使用 ssh 时如何避免输入公钥密码

Har*_*ett 2 linux ssh sshpass

如果我想使用密码,我可以这样做:

`sshpass -f <(printf '%s\n' your_password) ssh user@hostname`
Run Code Online (Sandbox Code Playgroud)

但 sshpass 似乎不适用于公钥。我尝试了以下命令但失败了:

`sshpass -f <(printf '%s\n' your_passphrase) ssh -o PreferredAuthentications=publickey user@hostname`
Run Code Online (Sandbox Code Playgroud)

有什么好的办法吗?

小智 7

您可以使用该sshpass选项-P来检测密码提示:

sshpass -Ppassphrase -f <(printf '%s\n' your_passphrase) ssh -o PreferredAuthentications=publickey user@hostname
Run Code Online (Sandbox Code Playgroud)

  • `-Ppassphrase` 中缺少空格(应该是 `-P passphrase`),而且不需要使用 `printf`,尤其是 `sshpass` 的选项 `-f`(用于传递包含密码),但使用“-p”(您可以直接插入密码)。当然,如果您直接在命令行中插入,则此方法是不安全的,在这种情况下,我建议将密码保存到文件中并使用“-f”提供文件名。因此,更简单的命令可以是“sshpass -P passphrase -p your_passphrase ssh -o PreferredAuthentications=publickey user@hostname” (6认同)