Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pushing to GitHub using a cron job -- Permission denied (publickey)

Tags:

git

ssh

cron

ubuntu

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.

like image 598
John Manak Avatar asked Oct 08 '14 17:10

John Manak


People also ask

How do I fix SSH permission denied Publickey?

If you want to use a password to access the SSH server, a solution for fixing the Permission denied error is to enable password login in the sshd_config file. In the file, find the PasswordAuthentication line and make sure it ends with yes . Find the ChallengeResponseAuthentication option and disable it by adding no .

How do I fix Git GitHub Permission denied Publickey fatal could not read from remote repository?

The “Permission denied (publickey). fatal: Could not read from remote repository” error is caused by an issue with the way in which you authenticate with a Git repository. To solve this error, make sure your key is being used on your Git account. If it is not, add your key to Git.

How to fix permission denied error on GitHub?

Get your public key by opening id_rsa.pub in any text editor. Copy its contents and paste it into Github Settings > SSH and GPG Keys. Now you have a SSH key configured on Github. This should solve your Permission Denied error and enable you to push code into your repo! Bonus!

What does ‘permission denied (publickey) ’ mean?

I frequently face the ‘Permission Denied (publickey)’ when I start pushing some code into my new repos. This means that, you do not have a SSH key on your local machine associated with your account. Either the key was removed or you haven’t made any.

How do I clone a GitHub repository with SSH key?

If you have GitHub Desktop installed, you can use it to clone repositories and not deal with SSH keys. If you are using another terminal prompt, such as Git for Windows, turn on ssh-agent: Verify that you have a private key generated and loaded into SSH.

What are some examples of Git permissions that should not be used?

There could be several reasons why, and the most common examples are explained below. Should the sudo command or elevated privileges be used with Git? You should not be using the sudo command or elevated privileges, such as administrator permissions, with Git.


1 Answers

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.

like image 139
VonC Avatar answered Sep 20 '22 13:09

VonC