passwordless ssh ubuntu in script

时间:2015-06-30 13:27:30

标签: shell ubuntu ssh

I have a passwordless ssh setup for ubuntu. It works fine, I can give ssh commands from the command line. But if I have a script that contains an ssh command, it still asks for my password. Example of command in script:

ssh ubuntu@localhost 'mkdir -p mydir'

Any ideas how to solve this problem?

Thanks, Serban

2 个答案:

答案 0 :(得分:1)

I think what you mean is:

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?

If so, the answer is that the server does not your have your private key, so the entry in the authorized_keys file is insufficient.

You can test this by seeing whether when logged into the server ssh ubuntu@localhost asks for a password.

Either:

  1. copy your private key to the server (in general a bad idea); or
  2. generate a new private/public keypair on the server (with ssh-keygen), and put the public element of that keypair into authorized_keys.

答案 1 :(得分:0)

In the server:

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

where id_rsa is your public key in the client from which you want to access the server.