Ssh – Adding address to known_hosts command line

known-hostssftpssh

I am having trouble adding an IP to known_hosts from command line. I have a mysql backup script that is supposed to upload files to an other server using sftp. The script works fine, but you have to run sftp command manually once to add the ip to known_hosts or the script will fail.

The script should be portable so it could be droped to a server and it will do everything automatically. Not requiring user to connect using sftp manually the first time.

I tried to add the ip using:

ssh-keygen -R [receiver IP]
ssh-keyscan -H [receiver IP] >> ~/.ssh/known_hosts

But I am getting following error:

ssh-keygen: /root/.ssh/known_hosts: No such file or directory

Should I just add touch to create known_hosts file or what is the best thing to do?

Best Answer

Since it must be portable to any server, I think the best choice for you is to append StrictHostKeyChecking=no to your ssh command like this:

ssh -o StrictHostKeyChecking=no [backupuser]@[backupserver]

You could also add this option globally in your sshd config file like this:

StrictHostKeyChecking No