137

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/" remoteUser@22.33.44.55:"/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?

1
  • Seems to work OK now with rsync 3.1.2 ...
    – rogerdpack
    Feb 12, 2022 at 6:07

6 Answers 6

97

Use either $HOME

rsync -avz -e "ssh -p1234  -i \"$HOME/.ssh/1234-identity\"" dir remoteUser@server:

or full path to the key:

rsync -avz -e "ssh -p1234  -i /home/username/.ssh/1234-identity" dir user@server:

Tested with rsync 3.0.9 on Ubuntu

6
  • 3
    Yes this works fine, no need to use ssh-agent or other config options.
    – laurent
    Dec 6, 2013 at 10:55
  • 2
    +1: this is a globbing issue, just like you couldn't do "ls '~'"
    – Alex
    Jan 17, 2014 at 23:53
  • $HOME will not work inside simple quotes (no variable expansion), so you must either use double quotes or the absolute path.
    – anol
    Aug 14, 2014 at 16:44
  • in standard Bash should work "$HOME" or $HOME without any difference (also "${HOME}" is possible, if you want to make it complicated)
    – Tombart
    Aug 17, 2014 at 15:44
  • I've changed the single quotes to doubles. Aug 26, 2014 at 1:59
92

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.

4
  • 2
    Double quotes and using $HOME solved my problem. Can you elaborate on what the first two commands are doing? I was already familiar with setting up a config file - the only problem is when I have multiple accounts on one server. I don't expect that it would let me specify multiple identity files for the same host.
    – cwd
    Apr 3, 2011 at 4:05
  • +1 This fixed my issue with drone.io :) Thanks a lot. Jan 21, 2014 at 6:37
  • This is friggin' awesome. FTR, you can also specify the default username to connect as, e.g. User ubuntu when configuring for an EC2 Ubuntu instance :) Thanks! May 11, 2015 at 10:44
  • If you're using csh style shell (such as fish) then do eval (ssh-agent -c)
    – Dave
    Dec 29, 2016 at 13:14
36

You have to specify the absolute path to your identity key file. This probably some sort of quirck in rsync. (it can't be perfect after all)

I ran into this issue just a few days ago :-)

1
  • You can use ~ but you can't embed it within single quotes, because then it doesn't get substituted correctly to /home/username/ you must use double quotes to have correct that work correctly, see @ilcavero 's solution
    – X Tian
    Mar 20, 2018 at 18:53
25

This works for me

rsync -avz --rsh="ssh -p1234  -i ~/.ssh/1234-identity"  \
"/local/dir/" remoteUser@22.33.44.55:"/remote/dir/"
2
  • 2
    For some reason I had to specify full path to the identity file, i.e. /home/user/.ssh/1234-identity. Then it worked. Possibly because it's then a different shell, as suggested in Darhuuk 's answer. May 17, 2015 at 18:08
  • @osa you saved me there! I was having an issue with Drupal and rSYNC and the issue was the path...weird. Feb 1, 2016 at 21:22
11

use key file with rsync:

rsync -rave "ssh -i /home/test/pkey_new.pem" /var/www/test/ ubuntu@231.210.24.48:/var/www/test
6

Are you executing the command in bash or sh? This might make a difference. Try replacing ~ with $HOME. Try double-quoting the string for the -e option.

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.