Having a very strange problem. I've created a small bash script which runs a command on a remote host via ssh (using public key authentication).
When I run this script manually from the command line it works fine, but when placed in /etc/cron.hourly it fails with
Permission denied, please try again. error.
- I explicitly set the key in the script using
ssh -i /root/.ssh/id_rsa user@remote "command";
- the script is running as root (I added a
echo `id` > /tmp/whoami.logto double-check); and
- the ssh key is not password protected…
The system is Ubuntu 12.04 server, I don't have much access on the remote side to troubleshoot, but as I said, running ssh manually or the same bash script from the command-line works.
Any idea why this is happening or how to fix it??
turns out I was mistaken, and the ssh key was password protected (with keychain loading the ssh-agent), hence why it failed from a script but not when running from the bash session. Adding
. ~/.keychain/$HOSTNAME-sh to my script resolved the problem (thanks to @grawity who pointed me in the right direction and provided a comprehensive answer).