2

I am trying to create a cron job to perform backup of a server over the internet using sftp. But before the actual backup is performed, I would like to verify that the server is connecting to the correct backup machine as its IP address may change periodically.

My idea is to put a file containing a checksum say abc123 in the sftp folder of the backup machine and let the server read and compare this checksum first before performing the backup transfer. I have already set up the ssh key to allow authentication without password. How do I go about performing this verification with sftp command?

  • I don't think sftp has a way to invoke such a process -- but you can certainly do it with ssh by calling a script or shell function remotely before doing the transfer, e.g. "ssh my-server-name '/bin/bash -c "if [ $(hostname) == my-server-name ]; then exit 0; else exit 1; fi"' ... the expression in single quotes will cause ssh to return an error if the hostname command on the backup machine doesn't match your expectation. – Stabledog Aug 2 '14 at 10:39
2

SSH already takes care of this. The first time you connect to a server from a client, the client records the public key of the server in ~/.ssh/known_hosts. If the key changes in a subsequent connection, ssh will complain that the server is being impersonated and refuse to connect. (This mechanism can be deactivated by configuration option, but… don't do it.)

  • Yes, you are right. I am looking at the wrong way of doing things. Thanks for pointing out the proper way :) – Question Overflow Aug 4 '14 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.