Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Github Pull script error 'permission denied (publickey)'

I've set up a PHP script to perform a GitHub pull:

This is contained in my Github folder /home/mysite/public_html/github

github_pull.php

<?php
echo `git pull 2>&1`;
?>

My server does already have the SSH public key, as if I perform git pull from Terminal:

ssh [email protected]
cd public_html/github
git pull

This works successfully (however I do have to enter the password for the rsa key first) Update: password is no longer needed (see comments)

However, when I run github_pull.php I get the following error: Permission denied (publickey). fatal: The remote end hung up unexpectedly

The SSH key is contained at /home/mysite/.ssh/id_rsa

When I run

<?php echo `whoami`;

It outputs mysite

like image 821
Tom Avatar asked Oct 16 '13 23:10

Tom


1 Answers

As commented, try first an https url:

 ssh [email protected]
 cd public_html/github
 git remote set-url origin https://github.com/username/reponame
 git pull

This is far easier than tinkering with ssh keys, especially when they are passphrase protected.


If you must use ssh keys, then you must know the default location of the key is:

~/.ssh/id_rsa(.pub)

If the user executing the script is 'mysite', then it will look for ~mysite/.ssh/id_rsa.
And you need to make sure the ssh-agent is running as mysite user. Which is why it is easier at first to try it with a private key not passphrase-protected.

If your ssh key were to be somewhere else, then you would need a:

~mysite/.ssh/config

In that config file, as illustrated here, you can specify the location and name of the key you want to use.

like image 171
VonC Avatar answered Sep 30 '22 16:09

VonC