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?
/root/.ssh
exist already? It usually doesn't. So you'd need to create it andchmod
it correctly.ssh-keyscan
won't create the directory for you iirc.