How to use rsync via ssh with rsa key passphrases?

15,077

Solution 1

If you're using rsync with a regular user, ssh-agent may be what you are looking for.

If ran into this problem while using rsync in a cron job. Cron jobs are run by the user cron in a restricted environement (some env var like SSH_AUTH_SOCK are missing, so ssh-agent does not work).

A walkthrough is to use keychain. Keychain keeps ssh-agent environement variables in a file.
So, first you install keychain.
Then, you add these lines in your .bashrc, .zshrc or .profile file (tweak it to match your ssh keys):

/usr/bin/keychain $HOME/.ssh/id_rsa
source $HOME/.keychain/${HOSTNAME}-sh

Additionnaly, to make cron jobs work, you can add this line at the beggining of your cron scripts:

source $HOME/.keychain/${HOSTNAME}-sh


More infos in this article : http://eli.thegreenplace.net/2013/10/08/some-notes-on-logging-and-ssh-access-from-cron-jobs/

Solution 2

rsync -Pavp --rsh="ssh -i YOUR_KEY" DEST USER@REMOTE:SOURCE
Share:
15,077

Related videos on Youtube

Pascal
Author by

Pascal

Updated on September 18, 2022

Comments

  • Pascal
    Pascal almost 2 years

    How can I use rsync with passphrases? All googled "solutions" i found use passphrase-less keys which seems no real option.

    i am runnning 2 ubuntu servers 12.04.2 LTS.

  • amc
    amc almost 11 years
    to provide a complete answer, please summarize the steps rather than simply providing a link.
  • FrankM
    FrankM almost 11 years
    I don't think I could explain it any better than the link provided. But if there is anything unclear I can explain it.
  • Pascal
    Pascal almost 11 years
    i appreacite your attempt frankm, however you completely missed my question. the link is a nice resource - but not for my question. i am looking for a secure solution using passphrases.
  • Pascal
    Pascal almost 11 years
    @amc allow me to "clarify" my question by quoting it: "How can I use rsync with passPHRASES?"
  • amc
    amc almost 11 years
    @Pascal that doesn't help. Lose the attitude please -- people on this site are volunteering their time and expertise to help. Since there are two ways to use pass phrases I am seeking clarification to find out which method you had in mind.
  • Pascal
    Pascal almost 11 years
    'help' is a good thing, with reading too. my question is crystal-clear IMHO. therefore I point everyone to my original question. this helps contributors with off-topic responses concerning pass_words_ to improve the quality of responses. had I added any redundant information to my question, not only would the quality of the question have decreased, it would also have sent a false feedback to those who didn't pick up the unambiguous (IMHO) term ( pass___phrases___ which is featured both in the title and text! if my question is unclear, just point out why instead of being oversensitive.
  • Pascal
    Pascal almost 11 years
    " I am seeking clarification to find out which method you had in mind" none. i am not aware of any methods. my suggestion to you would be naming those 2 methods (unless they deal with pass_words_) - it's puzzling me that you didn't.
  • FrankM
    FrankM almost 10 years
    I guess all you want is the '-e' parameter for rsync to use a ssh connection, which automaticly asks for your passphrase... rsync -avze ssh /home/user [email protected]:/backups This would copy /home/user from your local computer to your remote computers /backups directory
  • Fabby
    Fabby over 8 years
    I'm sorry, but the edit came in quite late, so 2 other users voted to delete as well, so it took me some time to have it undeleted (but now you've got 2 updates upvote as well)
  • Jack Wasey
    Jack Wasey about 7 years
    This also enables ssh -oBatchMode=yes ... so a script will fail instead of prompting and waiting for ever
  • Guilherme Corrêa Peralta
    Guilherme Corrêa Peralta almost 7 years
    This also works using a private key, in my case I had to replace {"password:"} to {"'name_if_my_private_key_file':"} because that's what the script has to look for after running the rsync command.