I wanted to run a simple automated backup script that would copy local files to a remote server. I used ‘scp’ to copy those files. Now, I have setup scp as needed to enable public key authentication with the server so that I would not be asked for a password. However, the authentication would work only if I — personally — ran the backup script and not when cron ran it with my user id. I was perplexed.
After quite a bit of digging, I figured it out: I used the Seahorse program to create my public–private key pair. Now, Seahorse automates the entire key setup process, even on the remote machine. It then asks Gnome to cache (using gnome-ssh-askpass probably) the private-key unlock password used by the user and make it available to Gnome applications when necessary. However, cron is not a gnome application, so when it ran the backup script using my user id, it still needed that password. Since there was no one there to supply that private-key unlock password, my backup script would fail.
The solution: use ssh-agent. You can make the unlock password available to cron by doing the following:
$ exec /usr/bin/ssh-agent $SHELL $ ssh-add
for more details on why and how this works, go here. Now in System->Preferences->Startup Applications (or Sessions in Ubuntu), create an entry for the following command:
This should do the trick!