11

The first time ssh-copy-id it will ask

# ssh-copy-id -i .ssh/id_dsa.pub backup@example.com
The authenticity of host 'example.com (xxx.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is 39:fb:5e:70:30:33:2b:18:17:e9:4f:2f:91:b5:d2:21.
Are you sure you want to continue connecting (yes/no)? 

Is it possible to script this, so the script just will answer yes?

Sandra
  • 10,303
  • 38
  • 112
  • 165

4 Answers4

11

SSH has an option to automatically add any host keys to the trusted hosts file:

ssh-copy-id -i .ssh/id_dsa.pub -o StrictHostKeyChecking=no backup@example.com

As an alternative, you could do the following:

echo "yes \n" | ssh-copy-id -i .ssh/id_dsa.pub backup@example.com

Edit: since it appears these solutions don't work with ssh-copy-id, you could always create a ~/.ssh/config file with the following option in it:

StrictHostKeyChecking no

This should work with all SSH connections, regardless if they are invoked through a script or not.

brain99
  • 1,792
  • 12
  • 19
9

If your ssh-copy-id doesn't support the StrictHostKeyChecking option, you can write a script that does:

  1. Run ssh-keyscan against the target server to get the public key
  2. Append that to the known_hosts file
  3. Run ssh-copy-id
cjc
  • 24,916
  • 3
  • 51
  • 70
1

This was from a decade ago, but is still a relevant question and doesn't have a functional answer:

ssh-copy-id -i .ssh/id_dsa.pub backup@example.com <<< yes

You will still need to enter your password.

Just in case someone else stumbles across this old post.

Jeff R.
  • 11
  • 1
-1

Try this:

ssh-copy-id -i ~/.ssh/id_rsa.pub user@example.com -y

it does not add the host keys to the known_hosts file but lets you copy (or append) the public key to the authorized_keys that you want to, if that is the sole purpose here.

Marco
  • 1,709
  • 3
  • 17
  • 31