I need to have a script periodically rsync files from a remote host via ssh. The account which the script runs under does not have home directory on the remote host, so I'm not able to store my public key on the remote host. I'm happy to store my password in a file on my local host, but I can't seem to get rsync to use the password.

The --password-file option is only an option for connecting to an rsync daemon, and the password prompt does not read from stdin.


Have you looked at using expect?

  • 1
    +1. This is really the only solution. Python's Pexpect and Perl's Expect implementation are both easy to use, I suggest those routes instead of the pure Tcl version. – Sam Halicke Nov 19 '09 at 1:00

Look for the program sshpass in your distribution. Once you have it, you can do:

rsync –e 'sshpass -f passwordfile ssh -l user' host:path

or, putting everything in one file:

rsync –e 'sshpass -d 300 ssh -l user' host:path 300<<<'password'

You might want to look into changing the AuthorizedKeysFile option in the remote server's sshd_config, if that's an option, so you may have your public key set up there without a home directory. In that case, it would be something like /home/%u/.ssh/authorized_keys, to minimize potential conflicts with other users on the system.

  • Strongly second the AuthorizedKeysFile idea. Public key authentication is The Right Way to do automated logins. sshpass and expect are ways of deliberately circumventing the well-designed, well-tested security model used by ssh. – Insyte Nov 21 '09 at 16:47
  • Thanks for this. I'm working with a set of servers that don't allow keyfile authentication, yet I have to do multiple rsyncs. The password policy is "long passphrases". You saved me bucketfuls of typing. – Rich Mar 8 '11 at 5:48
  • Out of interest, what is logic behind them not allowing keyfiles? Surely not allowing them would be less secure than long passphrases, which people would need to write down somewhere? – Decado Mar 8 '11 at 7:14

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.