3

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.

dessert
  • 41,116
Pascal
  • 127
  • 2
    are you asking about using passwords when connecting to ssh or are you wanting to use RSA keys with passphrases? the steps for setting up either are likely similar to the solutions you derided in your post. please edit your question to provide more detail so we can provide the answer to the specific question you have. – amc Aug 29 '13 at 15:13

4 Answers4

5

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/

Abd
  • 51
  • 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) – Fabby Nov 25 '15 at 13:20
3
rsync -Pavp --rsh="ssh -i YOUR_KEY" DEST USER@REMOTE:SOURCE
muru
  • 207,970
Vinz
  • 31
  • This also enables ssh -oBatchMode=yes ... so a script will fail instead of prompting and waiting for ever – Jack Wasey May 08 '17 at 14:51
  • The destination and source are reversed ! Works and ask for passphrase interactively in the console. The -p is useless though. – Zatigem Mar 14 '25 at 14:32
0

I found the perfect answer to this on https://stackoverflow.com/questions/3299951/how-to-pass-password-for-rsync-ssh-command

It worked perfect for me. It is a security risk as kainjow says, but it solves the problem.

Answer by kainjow:

If you can't use a public/private keys, you can use expect:

#!/usr/bin/expect
spawn rsync SRC DEST
expect "password:"
send "PASS\n"
expect eof
if [catch wait] {
    puts "rsync failed"
    exit 1
}
exit 0

You will need to replace SRC and DEST with your normal rsync source and destination parameters, and replace PASS with your password. Just make sure this file is stored securely!

  • 1
    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. – Guilherme Corrêa Peralta Aug 03 '17 at 14:41
-1

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 remoteuser@example.com:/backups This would copy /home/user from your local computer to your remote computers /backups directory

FrankM
  • 91
  • 3
    to provide a complete answer, please summarize the steps rather than simply providing a link. – amc Aug 29 '13 at 15:12
  • I don't think I could explain it any better than the link provided. But if there is anything unclear I can explain it. – FrankM Aug 29 '13 at 15:14
  • so summarize (copy/paste if you have to) the steps provided - just be sure to cite your source. the reason it's considered best practice to do so is so that, say a year from now, when someone looks at this Q/A, if the link doesn't work they can still get a solution – amc Aug 29 '13 at 15:16
  • 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 Aug 30 '13 at 07:04
  • 1
    @amc allow me to "clarify" my question by quoting it: "How can I use rsync with passPHRASES?" – Pascal Aug 30 '13 at 07:06
  • @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. – amc Aug 30 '13 at 14:08
  • '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 Sep 03 '13 at 09:09
  • " 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. – Pascal Sep 03 '13 at 09:13
  • 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 remoteuser@example.com:/backups This would copy /home/user from your local computer to your remote computers /backups directory

    – FrankM Jun 29 '14 at 01:28