Jenkins主机密钥验证失败。

204
我遇到了一个与jenkins相关的问题,当设置“git”时,会出现以下错误:
Failed to connect to repository : Command "git ls-remote -h https://person@bitbucket.org/person/projectmarket.git HEAD" returned status code 128:
stdout:
stderr: fatal: Authentication failed

我已经使用ssh进行了测试:

git@bitbucket.org:person/projectmarket.git

这是错误:
Failed to connect to repository : Command "git ls-remote -h git@bitbucket.org:person/projectmarket.git HEAD" returned status code 128:
stdout:
stderr: Host key verification failed.
fatal: The remote end hung up unexpectedly

我也用“SSH密钥”完成了这些步骤。

在Jenkins下登录

sudo su jenkins

将您的 Github 密钥复制到 Jenkins 的 .ssh 文件夹中

cp ~/.ssh/id_rsa_github* /var/lib/jenkins/.ssh/

重命名键

mv id_rsa_github id_rsa
mv id_rsa_github.pub id_rsa.pub

但是在jenkins中,git仓库依然无法工作。

感谢您的帮助!


没有 Jenkins 用户存在。 - IceFire
20个回答

230

切换到 jenkins 用户并手动运行该命令:

git ls-remote -h git@bitbucket.org:person/projectmarket.git HEAD

当您首次通过SSH连接到新主机时,您将收到标准的SSH警告:
The authenticity of host 'bitbucket.org (207.223.240.181)' can't be established.
RSA key fingerprint is 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40.
Are you sure you want to continue connecting (yes/no)?

输入 yes 并按回车键。主机密钥将被添加到 ~/.ssh/known_hosts 文件中,这样在 Jenkins 中就不会再出现此错误了。

6
权限拒绝(公钥)。 致命错误:远程终端意外断开连接。 - A. M. Mérida
6
好的,但这是一个完全不同的错误。现在你需要将你的公钥添加到 bitbucket.org 上的代码库中。 - ctc
4
请参考此处的第6步骤:https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git - ctc
3
您需要使用运行Jenkins的用户来运行它。在大多数系统上,通常将其作为单独的用户运行(例如,“jenkins”用户)。因此,您需要切换到该用户以确保将bitbucket.org的地址添加到~/.ssh/known_hosts文件中。 - ctc
5
你知道可以使用 ssh-keyscan 来实现这个吗?例如:ssh-keyscan -t rsa bitbucket.org >> ~/.ssh/known_hosts - slf
显示剩余19条评论

68
Jenkins是一个服务账户,它的设计并不包含shell。普遍认为,服务账户不应该能够进行交互式登录。
要解决“Jenkins主机密钥验证失败”的问题,请按照以下步骤操作。我已经在Jenkins中使用了mercurial。
  1. 在终端上执行以下命令
$ sudo su -s /bin/bash jenkins

提供密码
使用以下命令生成公私钥:
  1. 使用以下命令生成公私钥:
ssh-keygen

你可以看到输出为::
Generating public/private rsa key pair. 
Enter file in which to save the key (/var/lib/jenkins/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

按下回车键 --> 不要输入任何密码短语 --> 按下回车键
Key has been generated

4. 前往 --> cat /var/lib/jenkins/.ssh/id_rsa.pub 5. 从id_rsa.pub复制密钥 6. 退出bash 7. ssh@yourrepository 8. vi .ssh/authorized_keys 9. 粘贴密钥 10. 退出 11. 手动登录到mercurial服务器 12. 注意:请手动登录,否则jenkins会再次出现“主机验证失败”的错误 13. 手动完成后,现在转到Jenkins并进行构建 14. 祝您好运 15. 祝您成功

1
这里需要输入什么密码? - IceFire

33
或者你可以使用:
ssh -oStrictHostKeyChecking=no host
这样做会不安全(中间人攻击),但是是最简单的解决方案。
更好的方法是生成正确的主机和IP地址之间的映射,这样ssh就不会报错了。
#!/bin/bash

for domain in "github.com" "bitbucket.org"; do
    sed -i "/$domain/d" ~/.ssh/known_hosts
    line=$(ssh-keyscan $domain,`nslookup $domain | awk '/^Address: / { print $2 ; exit }'`)
    echo $line >> ~/.ssh/known_hosts
done

gist中摘录。
注意,上述操作也可以直接从终端完成。
domain=github.com
sed -i "/$domain/d" ~/.ssh/known_hosts
line=$(ssh-keyscan $domain,`nslookup $domain | awk '/^Address: / { print $2 ; exit }'`)
echo $line >> ~/.ssh/known_hosts

3
这会让你容易受到中间人攻击的威胁。 - ctc
1
你可以添加参数“-oStrictHostKeyChecking=no”并连接一次,这将把主机添加到known_hosts文件中(与在命令行上使用ssh并输入yes以将密钥添加到known_hosts文件相同),然后在此之后删除此选项。 - krupan
域名查找很棘手,因为主机可能在集群上运行。你一分钟前得到的机器可能在下一分钟是另一个盒子。 - ingyhere

24
在“管理Jenkins > 配置全局安全”下面有:

enter image description here

顺便说一句,没有验证肯定不是最好的选择。

14
好的,这是需要翻译的内容:“nice. Just for others sake, this setting can be found under 'Manage Jenkins' > 'Configure Global Security'。”我的翻译如下:“很好。为了其他人着想,此设置可以在“管理Jenkins”>“配置全局安全性”下找到。” - Tim Krins
这是唯一一个在安装了Windows Server和私有Github存储库的Jenkins中有效的答案。 - writerrajiv
这是唯一一个在安装了Windows Server和使用私有代码库的Github中起作用的答案。 - writerrajiv
我相信这是向Jenkins提供已知主机密钥的正确方法。它不需要通过ssh登录到主机,保持可移植性(如果您移动了jenkins实例),并保留了从Jenkins内部进行可见性的优点。- 当我设置作业时,我必须确保使用“ssh”而不是“https” URI。 - Ben Keene
1
在更新的版本中,“配置全局安全”已更名为“管理Jenkins > 安全”。此外,“接受第一次连接”是一个下拉选项,对我来说很有效。 - undefined

8

我也遇到了同样的问题,我是这样解决的:

仅为当前用户重置id_rsa*文件的权限,不包括任何组或其他用户。

chmod o-rwx ~/.ssh/id*
chmod G-rwx ~/.ssh/id*

ls -lart ~/.ssh/


-rw-------  1 jenkins nogroup  398 avril  3 09:34 id_rsa.pub
-rw-------  1 jenkins nogroup 1675 avril  3 09:34 id_rsa

清除 ~/.ssh/know_hosts
现在以 jenkins 身份连接
sudo su jenkins

尝试使用Jenkins命令

git ls-remote -h git@bitbucket.org:user/project.git HEAD

如果没有出现问题,现在 Jenkins 将能够连接仓库(至少对我而言)。

1
以上命令(chmod G-rwx ~/.ssh/id*)用于更改组权限失败了。下面的命令按预期工作。chmod g-rwx ~/.ssh/id* - samaitra

8
关于解决方法(例如Windows从机),请在全局属性中定义以下环境变量:
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"

Jenkins, Global properties, Environment variables, GIT_SSH_COMMAND

注意:如果您没有看到该选项,则可能需要使用 EnvInject 插件

4
  1. 使用以下命令以jenkins身份登录:sudo su -s /bin/bash jenkins
  2. git clone所需的存储库,这将导致密钥错误
  3. 它会要求您通过显示Yes/No来添加密钥(输入yes或y)

就是这样!

现在您可以重新运行Jenkins作业。


不,它只是要求输入密码,我尝试了Jenkins密码和Git密码,但都无法通过验证。 - Owl

3

3
  • Make sure we are not editing any of the default sshd_config properties to skip the error

  • Host Verification Failed - Definitely a missing entry of hostname in known_hosts file

  • Login to the server where the process is failing and do the following:

    1. Sudo to the user running the process

    2. ssh-copy-id destinationuser@destinationhostname

    3. It will prompt like this for the first time, say yes and it will also ask password for the first time:

      The authenticity of host 'sample.org (205.214.640.91)' can't be established.
      RSA key fingerprint is 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40.
      Are you sure you want to continue connecting (yes/no)? *yes*
      

      Password prompt ? give password

    4. Now from the server where process is running, do ssh destinationuser@destinationhostname. It should login without a password.

      Note: Do not change the default permissions of files in the user's .ssh directory, you will end up with different issues


在第三步中,可能会出现文件中已存在密钥的提示,但您应该继续执行这4个步骤。如果您可以从Jenkins用户登录而无需密码,则说明一切都设置好了。 - Rakibul Haq
Jenkins用户缺少.pub文件。 - Owl

3

最好的方法是在Jenkinsfile或任何需要的地方,使用“https” URL格式中的“git url”。

git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'


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