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.
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 .
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.
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!
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.
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.
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.
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 havePubkeyAuthentication yes
in each remote server'ssshd_config
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With