ssh rsync – Specify Identity File (id_rsa) with rsync

rsyncssh

I need to make periodic backups of a directory on a remote server which is a virtual machine hosted by a research organisation. They mandate that access to VMs is through ssh keys, which is all good, except that I can't figure out how to point rsync to the ssh key for this server.

Rsync has no problem if the key file is ~/.ssh/id_rsa, but when it is something else I get Permission denied (publickey).

With ssh I can specify the identity file with -i, but rsync appears to have no such option.

I have also tried temporarily moving the key on the local machine to ~/.ssh/id_rsa, but that similarly does not work.

tl;dr

Can you specify an identity file with rsync?

Best Answer

You can specify the exact ssh command via the '-e' option:

rsync -Pav -e "ssh -i $HOME/.ssh/somekey" username@hostname:/from/dir/ /to/dir/

Many ssh users are unfamiliar with their ~/.ssh/config file. You can specify default settings per host via the config file.

Host hostname
    User username
    IdentityFile ~/.ssh/somekey

In the long run it is best to learn the ~/.ssh/config file.

Related Question