sshrsync

Prevent rsync from trying to ask for a password


How do I prevent rsync from trying to ask for a password for the remote server login?

Note: I am not asking how to set up public key authenticated SSH. I know how to set up public key authenticated SSH. What I am asking is how to prevent rsync from trying to ask for a password if public key authentication fails, like what scp's -B flag does. I am using rsync in a script here, so if it tries to ask for a password, my script will hang, waiting for input that will never come. I want the rsync command to instead fail, so my script can detect the failure and exit gracefully.


Solution

  • Just pass options to the underlying ssh command used by rsync:

    rsync -e 'ssh -oBatchMode=yes [other ssh options]' [rest of rsync command]
    

    From the rsync manual:

    -e, --rsh=COMMAND           specify the remote shell to use
    

    From the ssh manual:

    BatchMode
    
    If set to “yes”, passphrase/password querying will be disabled.
    This option is useful in scripts and other batch jobs where no
    user is present to supply the password.  The argument must be
    “yes” or “no”.  The default is “no”.
    

    This emulates the batch mode behavior of scp -B.