Copy my ssh key to remote machine via ssh-copy-id in a script without prompt?

My usecase is that i need to copy my ssh key for authorized access onto a remote machine. I need to be able to call a script that copies over my ssh key to the remote machine without needing my input. Currently it prompts for a password and also a yes to the RSA fingerprint. I need it to be able to automatically add my hosts to each other via an IP range


Solution 1:

I believe you can use sshpass.

  • Ubuntu/Debian: apt-get install sshpass
  • Fedora/CentOS: yum install sshpass

For example:

sshpass -p "PASSWORD" ssh-copy-id -o StrictHostKeyChecking=no USERNAME@IP

Solution 2:

For ssh password prompt, try to use ansible/ansible-playbook -k/--ask-pass. It will call sshpass(FYI, maybe OSX don't ship with sshpass by default).

For host key checking, add

[defaults]
host_key_checking = False

in ~/.ansible.cfg or /etc/ansible/ansible.cfg.

Or export ANSIBLE_HOST_KEY_CHECKING=False just like this manual says.

For ssh keys deploy, use authorized_keys module in ansible playbook. It's easier for user to keep the scripts idempotency.