使用SSH从GitHub部署到Linux服务器时出现“Permission denied (publickey)”错误。PHP脚本。

9
我正在尝试在我的共享Linux服务器上使用部署PHP脚本,利用github.com的Web-hooks功能拉取私有仓库,第一次使用SSH。基本上,每次我提交到Github时,Web-hook会向我的php文件发送一个POST请求,我想使用它来发出简单的git pull命令,从而使我的服务器和Github保持同步,并意味着我有一个良好的工作流程,将帮助我停止在生产服务器上编辑东西。我使用终端进行ssh连接。我使用ssh进入我的服务器并创建了我的私有repo的克隆,这起作用了。因此,我可以ssh到我的服务器并发出git pull,它完美地工作。但是,当然,我想自动化这个过程。如果我能让这个工作,我想对其他几个仓库做同样的事情。
我已经按照github指南创建了密钥,并将它们添加到我的github.com账户的ssh keys部分,我可以在我的主机上的/.ssh/文件夹中看到这些密钥。私钥具有chmod 600权限,公钥为644。我已多次删除这些密钥,重新创建新的密钥,并在github上多次删除/重新添加公钥。

我目前拥有这些密钥:

  1024 03:c4:16:45:40:77:a4:94:a4:... /home/username/.ssh/id_dsa (DSA)
  2048 b3:62:87:e0:4b:39:aa:06:97... /home/username/.ssh/id_github (RSA)

我有一个 /.ssh/config 文件,其中包含以下内容(希望 Github 根据 这个 Stack Overflow 回答 使用 id_github 密钥:
 # Default GitHub
Host github_server
HostName github.com
user git
ForwardAgent yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_github

我已经更新了我的.git/config文件,将命令更改为git@github_server:username/repo

我使用以下命令将密钥复制到authorized_keys中

 cat id_github.pub >> authorized_keys

但是当我提交时,我收到一封电子邮件,上面显示“权限被拒绝(publickey)”(我的php脚本会发送成功/失败的电子邮件,它是基于此repo的:)
这使用了一个简单的git pullshell_exec函数中,我添加了2>&1以进行调试。
我修改了我的命令(尝试为用户添加密钥)。
  `ssh-add -l git pull 2>&1` 

并收到了消息

   `Could not open a connection to your authentication agent.`

所以我尝试使用以下命令启动ssh-agent:

  `eval $(ssh-agent) ssh-add ssh-add -l git pull` 

它返回一个代理进程的PID。
  Agent pid 944568
  ssh-add: No such file or directory
  -l: No such file or directory
  git: No such file or directory
  pull: No such file or directory

不好,所以我只尝试了:

  `eval $(ssh-agent) ssh-add git pull` 

并且返回了:

  Agent pid 949815
  git: No such file or directory
  pull: No such file or directory

并且为了完成仅此:

  `eval $(ssh-agent) ssh-add -l git pull` 

并且返回了:

 Agent pid 952014
 The agent has no identities.

此外,在某个时候,尝试任何事情,
**编辑:后来发现是这样的: [^] **
 eval $(ssh-agent) ssh-add ~/.ssh/id_github git pull 2>&1

我在命令中添加了其他内容,得到了这个结果:
 Agent pid 940365
 Enter passphrase for /home/username/.ssh/id_github: 
 ssh-add: No such file or directory
 -l: No such file or directory
 git: No such file or directory
 pull: No such file or directory

这表明我需要为用户添加密码短语,然后使用密钥即可正常工作,但如何在php shell_exec命令中执行此操作而不会存在安全问题?

对我来说,这也意味着我的密钥未正确设置给该用户,但下面的输出表明已经设置正确。

更多信息:

运行ps aux | grep ssh会显示很多ssh-agents,我不知道这是否是一个问题

 940006  0.0  0.0  57708   784 ?        Ss   23:22   0:00 ssh-agent  
 940365  0.0  0.0  57708   768 ?        Ss   23:24   0:00 ssh-agent  
 944222  0.0  0.0  57708   784 ?        Ss   23:42   0:00 ssh-agent  
 944568  0.0  0.0  57708   772 ?        Ss   23:44   0:00 ssh-agent  
 944854  0.0  0.0  57708   772 ?        Ss   23:45   0:00 ssh-agent  
 945103  0.0  0.0  57708   772 ?        Ss   23:47   0:00 ssh-agent  
 945188  0.0  0.0  57708   784 ?        Ss   23:47   0:00 ssh-agent

根据这个SO回答在ssh上运行以下命令

# ssh -i ~/.ssh/id_github -vT git@github.com

我得到了这个...

 OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
 debug1: Reading configuration data /home/username/.ssh/config
 debug1: Reading configuration data /etc/ssh/ssh_config
 debug1: Applying options for *
 debug1: Connecting to github.com [192.30.252.129] port 22.
 debug1: Connection established.
 debug1: identity file /home/username/.ssh/id_github type 1
 debug1: identity file /home/username/.ssh/id_github-cert type -1
 debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-    5ubuntu1+github5
 debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1+github5 pat OpenSSH*
 debug1: Enabling compatibility mode for protocol 2.0
 debug1: Local version string SSH-2.0-OpenSSH_5.3
 debug1: SSH2_MSG_KEXINIT sent
 debug1: SSH2_MSG_KEXINIT received
 debug1: kex: server->client aes128-ctr hmac-md5 none
 debug1: kex: client->server aes128-ctr hmac-md5 none
 debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
 debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
 debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
 debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
 debug1: Host 'github.com' is known and matches the RSA host key.
 debug1: Found key in /home/username/.ssh/known_hosts:2
 debug1: ssh_rsa_verify: signature correct
 ...
 debug1: Authentications that can continue: publickey
 debug1: Next authentication method: publickey
 debug1: Offering public key: /home/username/.ssh/id_github
 debug1: Server accepts key: pkalg ssh-rsa blen 277
 debug1: Authentication succeeded (publickey).
 debug1: channel 0: new [client-session]
 debug1: Requesting no-more-sessions@openssh.com
 ...
 debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
 debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0
 Hi username! You've successfully authenticated, but GitHub does not provide shell access.
 debug1: channel 0: free: client-session, nchannels 1
 Transferred: sent 2440, received 2920 bytes, in 0.2 seconds
 Bytes per second: sent 12993.2, received 15549.2
 debug1: Exit status 1

我已经删除了一些我认为不相关的内容,看起来密钥存在并被接受 - 这是最重要的部分,对吧?
那么我错过了什么?
附言:我肯定已经将密钥添加到了 Github。
谢谢
^编辑
在终端中运行以下命令:eval $(ssh-agent) ssh-add ~/.ssh/id_github 返回:
 Agent pid 958924
 Enter passphrase for /home/username/.ssh/id_github: (i added passphrase)
 Identity added: /home/username/.ssh/id_github (/home/username/.ssh/id_github) 

更新:

我删除了所有的密钥,使用默认的id_rsa文件名重新生成了一个密钥,密码留空,现在它可以工作了!我收到了电子邮件,执行了git pull命令并部署了一个测试文件。太棒了。

根据github关于密码短语的指导,我进去编辑了我的密钥并添加了一个密码短语,但是问题又回来了:Permission denied (publickey)。


你想要自动化什么?你想从哪里运行你的自动化? - pampasman
每次我提交到Github时,Webhook会向我的PHP文件发送一个POST请求,我想利用此功能来发出简单的git pull命令,从而使我的服务器和Github保持同步。据我所知,这并不是一个罕见的概念。有很多谷歌搜索结果,但我已经尝试过了,但还没有找到有效的解决方案。 - user894932
2个回答

2

Github还允许使用部署密钥,它们与普通的ssh密钥相同,但通常不带有密码。正如我之前提到的问题中所述,我可以在没有密码的情况下进行部署。

这似乎运行良好,因此将其作为答案接受。


0

请在您的配置文件中尝试以下内容:

User git

而不是

user git

尝试后仍然出现以下错误:在尝试简单的 git pull 时,返回 Permission denied (publickey). fatal: The remote end hung up unexpectedly - user894932
你尝试过使用~/.ssh/id_rsa作为名称而不是~/.ssh/id_github吗?确保~/.ssh/id_rsa具有600权限。 - pampasman
尝试使用keygen创建另一个密钥,将id_rsa添加到Github。再次尝试,但仍然出现相同的问题。 - user894932

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接