【问题标题】:deploying from github to linux server using ssh but Permission denied (publickey). PHP script使用 ssh 从 github 部署到 linux 服务器,但权限被拒绝(公钥)。 PHP 脚本
【发布时间】:2014-02-10 04:36:11
【问题描述】:

我正在尝试在我的共享 linux 服务器上使用部署 php 脚本,使用他们的 web-hooks 功能从 github.com 拉入 private 存储库,使用 第一次使用ssh。

本质上: 每次我提交到 github 时,web-hook 都会向我的 php 文件发送一个 post 请求,我想用它来发出一个简单的 git pull 命令,从而保留我的服务器和 github同步并且意味着我有一个体面的工作流程,这将帮助我停止在生产服务器上编辑内容。

我正在使用终端进行 ssh 连接。

我使用 ssh 访问我的服务器并创建了我的私人仓库的克隆,这很有效。

因此,我可以通过 ssh 连接到我的服务器并发出 git pull 并且它运行良好。但我当然想自动化这个过程。如果我能让这个工作,我想为其他几个回购做同样的事情。

我已关注 github's guide 创建密钥并将它们添加到我在 github.com 上的帐户的 ssh keys section,我可以看到密钥存在于我主机上的 /.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 使用基于 this SO answer 的 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

但是当我提交时,我收到一封电子邮件,上面写着permissions denied (publickey)(我的 php 脚本发出电子邮件表示成功/失败并且是based on this repo

这在shell_exec 函数中使用了一个简单的git pull,我在末尾添加了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.

另外,在某个时候,尝试任何事情,

**edit:后来发现是这样的:[^] **

 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 代理。不知道是不是这个问题

 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

在 ssh 上基于 this SO answer 运行以下命令

# 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 guidance for passphrases,我进去编辑了我的密钥,添加了一个密码,然后我又回到了原点。 Permission denied (publickey).

【问题讨论】:

  • 您想要自动化什么?您想从哪里运行自动化?
  • 每次我提交到 github 时,web-hook 都会向我的 php 文件发送一个 post 请求,我想用它来发出一个简单的 git pull 命令。从而使我的服务器和 github 保持同步。 - 我的理解是这不是一个罕见的概念。很多谷歌结果,但我已经尝试过,但还没有找到有效的解决方案。

标签: php linux git github ssh


【解决方案1】:

Github 还允许部署密钥,这些密钥与普通的 ssh 密钥相同,但通常不附带密码。如我之前的问题所述,我无需密码即可部署。

这似乎工作正常,所以会接受它作为答案。

【讨论】:

    【解决方案2】:

    在您的配置文件中尝试以下操作:

    User git
    

    代替

    user git
    

    【讨论】:

    • 试过了。仍然得到:Permission denied (publickey). fatal: The remote end hung up unexpectedly 尝试简单时git pull
    • 您是否尝试过仅使用 ~/.ssh/id_rsa 而不是 ~/.ssh/id_github 作为名称。确保 ~/.ssh/id_rsa 有 600 个权限。
    • 又一次尝试使用 keygen 创建另一个密钥。 id_rsa,添加到 github。再试一次,同样的问题。
    猜你喜欢
    • 1970-01-01
    • 2021-03-04
    • 2018-11-06
    • 2011-11-25
    • 1970-01-01
    • 2019-11-24
    • 2014-11-08
    • 2019-03-11
    • 1970-01-01
    相关资源
    最近更新 更多