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?

  • 1
    Does the directory /root/.ssh exist already? It usually doesn't. So you'd need to create it and chmod it correctly. ssh-keyscan won't create the directory for you iirc. Jun 19, 2015 at 5:20

1 Answer 1


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .