简体   繁体   English

PHP Github拉出脚本错误'权限被拒绝(publickey)'

[英]PHP Github Pull script error 'permission denied (publickey)'

I've set up a PHP script to perform a GitHub pull: 我已经设置了一个PHP脚本来执行GitHub拉取:

This is contained in my Github folder /home/mysite/public_html/github 这包含在我的Github文件夹/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公钥,就好像我从终端执行git pull

ssh username@host.com
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) 这成功(但我必须先输入rsa 密码的密码 更新:不再需要密码(请参阅注释)

However, when I run github_pull.php I get the following error: Permission denied (publickey). 但是,当我运行github_pull.php我收到以下错误:Permission denied(publickey)。 fatal: The remote end hung up unexpectedly 致命:远程端意外挂断

The SSH key is contained at /home/mysite/.ssh/id_rsa SSH密钥包含在/home/mysite/.ssh/id_rsa

When I run 我跑的时候

<?php echo `whoami`;

It outputs mysite 它输出了mysite

As commented, try first an https url: 如评论所述,首先尝试使用https网址:

 ssh username@host.com
 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. 这比修改ssh密钥容易得多,特别是当它们受密码保护时。


If you must use ssh keys, then you must know the default location of the key is: 如果必须使用ssh密钥,则必须知道密钥的默认位置是:

~/.ssh/id_rsa(.pub)

If the user executing the script is ' mysite ', then it will look for ~mysite/.ssh/id_rsa . 如果执行脚本的用户是' mysite ',那么它将查找~mysite/.ssh/id_rsa
And you need to make sure the ssh-agent is running as mysite user. 并且您需要确保ssh-agent作为mysite用户运行。 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: 如果您的ssh密钥位于其他地方,那么您需要:

~mysite/.ssh/config

In that config file, as illustrated here , you can specify the location and name of the key you want to use. 在该配置文件中,如此处所示 ,您可以指定要使用的密钥的位置和名称。

You should first try to debug using the actual 'mysite' account. 您应该首先尝试使用实际的“mysite”帐户进行调试。

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. 含义SSH实际上可以访问您的私钥。

I suspect github is receiving your own personal private key (via ssh-agent) and not 'mysite' public key. 我怀疑github正在接收你自己的私人密钥(通过ssh-agent)而不是'mysite'公钥。 You can validate this by running ssh-add -l within your php code, or with sudo -u mysite; ssh-add -l 您可以通过在PHP代码中运行ssh-add -l或使用sudo -u mysite; ssh-add -l来验证这一点sudo -u mysite; ssh-add -l sudo -u mysite; ssh-add -l and comparing with what's registered in github interface. sudo -u mysite; ssh-add -l并与github接口中注册的内容进行比较。

Github has covered this problem extensively: https://help.github.com/articles/error-permission-denied-publickey Github广泛报道了这个问题: https//help.github.com/articles/error-permission-denied-publickey

Add the following to your .ssh/config 将以下内容添加到.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 编辑.git / config并更新远程repo URL

url = git@github.com: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. 将repo名称替换为实际的repo名称。 Do note that the user 'mysite' has access to the key file. 请注意,用户'mysite'可以访问密钥文件。 You can test that using fopen from PHP and confirm. 您可以使用PHP中的fopen进行测试并确认。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM