Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a very simple cron job written in shell script, which scp a file from another server to local. e.g,

scp $REMOTE:/$FILE ./

Of course, the server need a private key to login, which I have config file saving HostName, IdentityFile in (~/.ssh/id_rsa, a mod with 600).

However, it has "permission denied" issue when the cron running....

X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/xxx>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=xxxx>
Message-Id: <20120118235702.33A7B16C03B5@xxx-xxxx-deskbox>
Date: Wed, 18 Jan 2012 18:57:02 -0500 (EST)

Permission denied (publickey).

I checked everywhere, it says the crontab will be running as the owner/user's environment.

Anyone have a solution for this? Thanks!

share|improve this question
This is not a Cron error, it is an SSH error. See… – voretaq7 Jan 19 '12 at 0:27
The server won't accept your key. Check that you can log in manually. You can add key to command line scp -i ~/.my_key_somewhere ... – jkj Jan 19 '12 at 9:45
Thanks jkj. Yes, manually run the script is a success. I test it before I put in the crontab, thought it will be same.... I tried with -i in my script: scp -i /home/xxxx/.ssh/id_rsa xxxx@$REMOTE:$FILE $FOLDER/ still no luck. Denied. – Guoqiang Huang Jan 19 '12 at 16:48

Problem solved. There was a passphrase on my id_rsa key. Hence manually running won't require it as the phrase is in the session; while crontab need the passphrase. Delete the passphrase

ssh-keygen -p [-P old_passphrase] 

and enter empty for new passphrase; or check this page

Hope my answer can help some programmers several hours of debugging..

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.