How do you use an identity file with rsync?

cwd picture cwd · Apr 3, 2011 · Viewed 96.2k times · Source

How do you use an identity file with rsync?

This is the syntax I think I should be using with rsync to use an identity file to connect:

rsync -avz -e 'ssh -p1234  -i ~/.ssh/1234-identity'  \
"/local/dir/" [email protected]:"/remote/dir/"

But it's giving me an error:

Warning: Identity file ~/.ssh/1234-identity not accessible: No such file or directory.

The file is fine, permissions are set correctly, it works when doing ssh - just not with rsync - at least in my syntax. What am I doing wrong? Is it trying to look for the identity file on the remote machine? If so, how do I specify that I want to use an identity file on my local machine?

Answer

BillThor picture BillThor · Apr 3, 2011

You may want to use ssh-agent and ssh-add to load the key into memory. ssh will try identities from ssh-agent automatically if it can find them. Commands would be

eval $(ssh-agent) # Create agent and environment variables
ssh-add ~/.ssh/1234-identity

ssh-agent is a user daemon which holds unencrypted ssh keys in memory. ssh finds it based on environment variables which ssh-agent outputs when run. Using eval to evaluate this output creates the environment variables. ssh-add is the command which manages the keys memory. The agent can be locked using ssh-add. A default lifetime for a key can be specified when ssh-agent is started, and or specified for a key when it is added.

You might also want to setup a ~/.ssh/config file to supply the port and key definition. (See `man ssh_config for more options.)

host 22.33.44.55
    IdentityFile ~/.ssh/1234-identity
    Port 1234

Single quoting the ssh command will prevent shell expansion which is needed for ~ or $HOME. You could use the full or relative path to the key in single quotes.