6

I have created an SSH key (following the official tutorial), added it to GitHub and created a Bash script that commits and pushes a single file to my repository on Github. When I run this script from the command line, everything works fine and the updates are pushed. However, when I set up a job using crontab -e, the push generates the following error:

Permission denied (publickey).
fatal: The remote end hung up unexpectedly

I have edited the user's crontab (crontab -e), i.e. I'm NOT using sudo crontab -e. I'm running Ubuntu 12.04.

John Manak
  • 13,328
  • 29
  • 78
  • 119
  • check the environment cron executes in. Often times things like your profile aren't loaded but your bashrc is, so if the setting for your SSH is in one only you can run into problems. – Andrew C Oct 08 '14 at 18:07
  • Does this SSH key require a passphrase? – Kenster Oct 08 '14 at 18:09
  • Yes; however, I have added it to the ssh-agent using ssh-add (as described in the tutorial). – John Manak Oct 08 '14 at 18:23

1 Answers1

8

if it isn't a user issue (where you run the job as root, missing the right $HOME/.ssh folder), it can be a passphrase issue:

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.

The passphrase bit is detailed in "Not able to ssh in to remote machine using shell script in Crontab":

You can make ssh connections within a cron session. What you need is to setup a public key authentication to have passwordless access.
For this to work, you need to have PubkeyAuthentication yes in each remote server's sshd_config.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It was indeed a passphrase issue. I solved it by setting up a public key authentication without a passphrase – John Manak Nov 20 '14 at 11:15