Ben*_*nny 8 php linux git ssh github
我试图在我的共享Linux服务器上使用部署php脚本,使用他们的web-hooks功能从github.com获取一个私人仓库,这是第一次使用ssh.
本质上:每次我提交github web-hook发送一个post请求到我的php文件,我想用它来发出一个简单的git pull命令,从而保持我的服务器和github同步并意味着我有一个体面的工作流程,将帮助我停止在生产服务器上编辑内容.
我正在使用终端进行ssh连接.
我使用ssh去我的服务器并创建我的私人仓库的克隆,这很有用.
因此,我可以ssh到我的服务器并发出一个git pull完美的工作.但当然我想自动化这个过程.如果我可以让这个工作,我想对其他几个回购做同样的事情.
我已经关注创建密钥并将其添加到github.com上我的帐户部分的github指南,我可以看到密钥存在于我的主机上的/.ssh/文件夹中.私钥有chmod 600,公钥是644.我删除了这些,重新创建了新密钥,并在github上删除/重新添加了几次公钥.ssh keys
我目前有这些键:
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)
Run Code Online (Sandbox Code Playgroud)
我有一个带有以下内容的/.ssh/config文件(希望github使用基于此SO答案的id_github密钥:
# Default GitHub
Host github_server
HostName github.com
user git
ForwardAgent yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_github
Run Code Online (Sandbox Code Playgroud)
我已更新我的.git/config以将命令更改为 git@github_server:username/repo
我已经使用了将密钥复制到authorized_keys
cat id_github.pub >> authorized_keys
Run Code Online (Sandbox Code Playgroud)
但是当我提交时,我收到一封电子邮件说permissions denied (publickey)(我的php脚本会发送成功/失败的电子邮件,并且基于此回购:
这使用了一个简单git pull的shell_exec函数,我已添加2>&1到最后进行调试.
我将命令更改为(尝试为用户添加密钥)
`ssh-add -l git pull 2>&1`
Run Code Online (Sandbox Code Playgroud)
得到了消息
`Could not open a connection to your authentication agent.`
Run Code Online (Sandbox Code Playgroud)
所以我接着尝试使用ssh-agent
`eval $(ssh-agent) ssh-add ssh-add -l git pull`
Run Code Online (Sandbox Code Playgroud)
它返回一个代理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
Run Code Online (Sandbox Code Playgroud)
没有好处,所以我尝试了:
`eval $(ssh-agent) ssh-add git pull`
Run Code Online (Sandbox Code Playgroud)
并返回:
Agent pid 949815
git: No such file or directory
pull: No such file or directory
Run Code Online (Sandbox Code Playgroud)
并完成这个:
`eval $(ssh-agent) ssh-add -l git pull`
Run Code Online (Sandbox Code Playgroud)
并返回:
Agent pid 952014
The agent has no identities.
Run Code Online (Sandbox Code Playgroud)
另外在某些时候,尝试任何事情,
**编辑:后来解决了这个:[^]**
eval $(ssh-agent) ssh-add ~/.ssh/id_github git pull 2>&1
Run Code Online (Sandbox Code Playgroud)
我在我的命令中添加了一些其他内容给了我:
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
Run Code Online (Sandbox Code Playgroud)
这表明我需要为用户添加密码,然后它可以使用密钥,但我怎么能从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
Run Code Online (Sandbox Code Playgroud)
在ssh上根据此SO答案运行以下命令
# 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
Run Code Online (Sandbox Code Playgroud)
我已经删除了一些我认为不相关的东西,它看起来像钥匙存在并被接受 - 这是重要的一点吗?
那我错过了什么?
ps我绝对有把钥匙添加到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)
Run Code Online (Sandbox Code Playgroud)
更新:
我删除了所有的密钥,使用默认的id_rsa文件名再次使用keygen启动,我将密码保留为空白,然后就可以了!收到电子邮件,git pull部署命令和测试文件.超.
看着github对密码的指导,我进去编辑了我的密钥,添加了一个密码,然后我回到了原点.Permission denied (publickey).