Reputation: 1068
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
Upvotes: 7
Views: 2861
Reputation: 518
Add the following to your .ssh/config
Host github_server
HostName github.com
User git
IdentityFile /path/to/your/private_key
Edit your .git/config and update the remote repo URL from
url = [email protected]:git_repo_name.git
to
url = git@github_server:git_repo_name.git
That should help you login to the server using the given key. Replace the path to the key in the above to the full actual path on your machine. Replace the repo name with the actual repo name. Do note that the user 'mysite' has access to the key file. You can test that using fopen from PHP and confirm.
Upvotes: 2
Reputation: 21
You should first try to debug using the actual 'mysite' account.
sudo -u mysite
cd ~/public_html/github
git pull
From the error log, it seems to be a remote problem, not local. Meaning SSH can actually access your private key.
I suspect github is receiving your own personal private key (via ssh-agent) and not 'mysite' public key. You can validate this by running ssh-add -l
within your php code, or with sudo -u mysite; ssh-add -l
and comparing with what's registered in github interface.
Github has covered this problem extensively: https://help.github.com/articles/error-permission-denied-publickey
Upvotes: 2
Reputation: 1323803
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.
Upvotes: 3