Search code examples
bashrsync

Why is rsync prompting for password in bash script, but runs fine when executed outside of script?


I have a shared SSH keys with another server so I can login without a password. Executing:

rsync -avze ssh --blocking-io --delete --rsh='ssh -p2020' [email protected]:/foo/* /bar/;

Runs fine. No password prompt. But that command in a bash file:

#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
rsync -avze ssh --blocking-io --delete --rsh='ssh -p2020' [email protected]:/foo/* /bar/;

Run like so:

sudo ./copyfiles.sh

Gives me this:

[email protected]'s password:

Any ideas why?


Solution

  • You seem to be running the script as root (sudo) but run the command line as some other user (this is just a guess). Most likely, the public key of the root user is not present on the destination machine.