In most Linux distributions, an instance of ssh-agent
is started when you log in. This is a background process that holds onto decrypted copies of your SSH keys; the ssh
command will attempt to go through ssh-agent
to gain access to the keys in order to do public-key authentication. The point of ssh-agent
is that you should only need to enter your password for an SSH key once during a login session. (On some distros you don't even need to enter this - your keys are automatically decrypted when you log in.)
I'm guessing you're trying to run your script from cron, or from some other server process (like a web server or CGI script)? If so, it won't have the necessary environment variables set up for the ssh
command to talk to ssh-agent
- so it will prompt for a password (and thus fail if it's not being run within a terminal).
You can get around this by storing an unencrypted copy of your SSH key (so you won't need ssh-agent
to decrypt it), but as this is a major security no-no this is generally only done for SSH keys that allow access to locked-down, minimally-privileged accounts.