脚本中的无密码 ssh ubuntu

passwordless ssh ubuntu in script

我有 ubuntu 的无密码 ssh 设置。它工作正常,我可以从命令行发出 ssh 命令。但是如果我有一个包含 ssh 命令的脚本,它仍然会询问我的密码。 脚本中的命令示例:

ssh ubuntu@localhost 'mkdir -p mydir'

有什么解决这个问题的想法吗?

谢谢, 塞尔维亚语

在服务器中:

cd /home/ubuntu/.ssh/
cat id_rsa.pub >> authorized_keys

其中 id_rsa 是您要访问服务器的客户端中的 public 密钥。

我想你的意思是:

I can ssh to my server and run this script without a password as my own public key is in the server's authorized_keys file, but when I run the script and it ssh's to itself, it asks for a password. Why?

如果是这样,答案是服务器没有您的私钥,因此 authorized_keys 文件中的条目不足。

您可以通过查看 登录服务器时 ssh ubuntu@localhost 是否要求输入密码来进行测试。

或者:

  1. 将您的私钥复制到服务器(通常是个坏主意);或
  2. 在服务器上生成一个新的private/public密钥对(带ssh-keygen),并将那个密钥对的public元素放入authorized_keys.