简体   繁体   English

自动git pull到服务器:主机密钥验证失败

[英]Automatic git pull to server: Host key verification failed

I have a server with a gitlab clone. 我有一台具有gitlab克隆的服务器。 I have set up everything, and it works, but now I want to do a git pull automatically. 我已经完成了所有设置,并且可以正常运行,但是现在我想自动执行git pull。 I already added a ssh key based on this: 我已经基于此添加了ssh密钥:

https://help.github.com/articles/generating-ssh-keys/ https://help.github.com/articles/generating-ssh-keys/

If I run the command git pull through putty, it works. 如果我运行git pull through putty命令,它将起作用。

Now I want to create a php file which does an automatic git pull. 现在,我想创建一个自动进行git pull的php文件。 I do that like this: 我这样做是这样的:

<?php
echo shell_exec( 'git pull 2>&1' );
?>

That gives the following error: 这给出了以下错误:

Host key verification failed. 主机密钥验证失败。 fatal: The remote end hung up unexpectedly 致命:远端意外挂断

I have searched for a solution, and most things I found were problems with the ssh key. 我一直在寻找解决方案,而我发现的大多数事情都与ssh密钥有关。 It does work when I run the command from the root user though. 但是,当我从root用户运行命令时,它确实起作用。 I changed the owner of my php file to root. 我将php文件的所有者更改为root。

I am completely new to this, so I might miss something obvious. 我对此是全新的,所以我可能会错过一些显而易见的事情。 However, it is working when I execute git pull from the command line, and not when I call the php script and I have no idea how to fix this. 但是,它在我从命令行执行git pull时起作用,而不是在我调用php脚本时起作用,并且我不知道如何解决此问题。

While similarly named, this error has to do with the "Host Key" instead of the key used for public key authentication. 尽管名称相似,但此错误与“主机密钥”有关,而不是与用于公共密钥身份验证的密钥有关。 This error most likely means that the host key (of the remote git server) has changed since it was stored in the known_hosts file. 此错误很可能意味着自远程密钥服务器的主机密钥存储在known_hosts文件以来,该密钥已经更改。

Check the key stored in the the .ssh/known_hosts file of the user under which php runs (httpd? apache?). 检查存储在运行php的用户的.ssh/known_hosts文件中的密钥(httpd?apache?)。 To fix the error, remove the line which corresponds with the hostname or ip of the remote git server. 要解决该错误,请删除与远程git服务器的主机名或ip相对应的行。 (From a security standpoint, you should verify the new "Host Key" before accepting it.) (从安全角度出发,在接受新的“主机密钥”之前,应先对其进行验证。)

This blog post may help, the main points it suggests are: 这篇博客文章可能会有所帮助,它提出的要点是:

Creating the php script like this: 创建这样的php脚本:

<?php
...
   echo shell_exec("/full/path/to/bin/git pull 2&gt;&amp;1");
...

Note: You can find the path to your (Linux) Git installation by executing which git 注意:您可以通过执行以下which git来找到(Linux)Git安装的路径

Then doing an initial Git pull with the apache user to make sure the remote is added to the user's known_hosts file like this: 然后与apache用户进行初始Git拉动,以确保将遥控器添加到用户的known_hosts文件中,如下所示:

sudo -u www git pull

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

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