简体   繁体   English

Jenkins 主机密钥验证失败

[英]Jenkins Host key verification failed

I have a problem with jenkins , setting "git", shows the following error:我有一个问题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

I have tested with ssh :我已经用ssh测试过:

git@bitbucket.org:person/projectmarket.git

This is error:这是错误:

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

I've also done these steps with "SSH key".我还使用“SSH 密钥”完成了这些步骤。

Login under Jenkins Jenkins下登录

sudo su jenkins

Copy your github key to Jenkins.ssh folder将您的 github 密钥复制到 Jenkins.ssh 文件夹

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

Rename the keys重命名键

mv id_rsa_github id_rsa
mv id_rsa_github.pub id_rsa.pub

but still not working git repository in jenkins .但仍然无法使用 git中的存储库jenkins

thanks by help..感谢帮助..

Change to the jenkins user and run the command manually:更改为jenkins用户并手动运行命令:

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

You will get the standard SSH warning when first connecting to a new host via SSH:首次通过 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)?

Type yes and press Enter.输入yes并按 Enter。 The host key for bitbucket.org will now be added to the ~/.ssh/known_hosts file and you won't get this error in Jenkins anymore. bitbucket.org的主机密钥现在将添加到~/.ssh/known_hosts文件中,您将不再在 Jenkins 中收到此错误。

Jenkins is a service account, it doesn't have a shell by design. Jenkins 是一个服务帐户,它在设计上没有外壳。 It is generally accepted that service accounts.人们普遍接受服务帐户。 shouldn't be able to log in interactively.应该无法以交互方式登录。

To resolve "Jenkins Host key verification failed", do the following steps.要解决“Jenkins 主机密钥验证失败”,请执行以下步骤。 I have used mercurial with jenkins.我已经将 mercurial 与 jenkins 一起使用。

1)Execute following commands on terminal 1)在终端执行以下命令

             $ sudo su -s /bin/bash jenkins

provide password提供密码

2)Generate public private key using the following command: 2)使用以下命令生成公钥私钥:

              ssh-keygen

you can see output as ::你可以看到输出为 ::

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: 

3)Press Enter --> Do not give any passphrase--> press enter 3)按回车-->不要输入任何密码-->按回车

             Key has been generated

4) go to --> cat /var/lib/jenkins/.ssh/id_rsa.pub 4)去--> cat /var/lib/jenkins/.ssh/id_rsa.pub

5) Copy key from id_rsa.pub 5) 从 id_rsa.pub 复制密钥

6)Exit from bash 6)退出bash

7) ssh@yourrepository 7) ssh@yourrepository

8) vi .ssh/authorized_keys 8) vi .ssh/authorized_keys

9) Paste the key 9) 粘贴密钥

10) exit 10) 退出

11)Manually login to mercurial server 11)手动登录mercurial服务器

Note: Pls do manually login otherwise jenkins will again give error "host verification failed"注意:请手动登录,否则 jenkins 会再次报错“主机验证失败”

12)once manually done, Now go to Jenkins and give build 12)一旦手动完成,现在去Jenkins并进行构建

Enjoy!!!享受!!!

Good Luck祝你好运

Or you can use:或者你可以使用:

ssh -oStrictHostKeyChecking=no host

This will be insecure (man in the middle attacks) but easiest solution.这将是不安全的(中间人攻击)但最简单的解决方案。

The better way to do that is to generate correct mappings between host and ip address, so ssh will not complain:更好的方法是在主机和 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

Excerpt fromgist .摘自要点

Had same problem, i fix it like that :有同样的问题,我是这样解决的:

reset permission on id_rsa* only for current user no group no other仅对当前用户重置 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

And clear ~/.ssh/know_hosts并清除 ~/.ssh/know_hosts

Now Connect as jenkins现在以詹金斯的身份连接

sudo su jenkins

Try the jenkins commands试试詹金斯命令

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

If no problem appears, now jenkins will be able to connect the repo (for me ^^ at least)如果没有出现问题,现在 jenkins 将能够连接 repo(至少对我来说 ^^)

using https://bitbucket.org/YYYY/XX.git使用https://bitbucket.org/YYYY/XX.git

you shoud delete username@你应该删除用户名@

As for the workaround (eg Windows slave), define the following environment variable in global properties:至于解决方法(例如 Windows slave),请在全局属性中定义以下环境变量:

GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"

Jenkins,全局属性,环境变量,GIT_SSH_COMMAND

Note: If you don't see the option, you probably need EnvInject plugin for it.注意:如果你没有看到这个选项,你可能需要它的EnvInject插件

  • Make sure we are not editing any of the default sshd_config properties to skip the error确保我们没有编辑任何默认的 sshd_config 属性来跳过错误

  • Host Verification Failed - Definitely a missing entry of hostname in known_hosts file主机验证失败 - known_hosts文件中肯定缺少主机名条目

  • Login to the server where the process is failing and do the following:登录到进程失败的服务器并执行以下操作:

    1. Sudo to the user running the process对运行该进程的用户执行 Sudo

    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 .现在从运行进程的服务器,执行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注意:不要更改用户.ssh目录中文件的默认权限,您最终会遇到不同的问题

Copy host keys from both bitbucket and github:从 bitbucket 和 github 复制主机密钥:

ssh root@deployserver 'echo "$(ssh-keyscan -t rsa,dsa bitbucket.org)" >> /root/.ssh/known_hosts'
ssh root@deployserver 'echo "$(ssh-keyscan -t rsa,dsa github.com)" >> /root/.ssh/known_hosts'

I ran into this issue and it turned out the problem was that the jenkins service wasn't being run as the jenkins user.我遇到了这个问题,结果发现问题是 jenkins 服务没有以 jenkins 用户身份运行。 So running the commands as the jenkins user worked just fine.因此,以 jenkins 用户身份运行命令工作得很好。

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

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

SSH SSH

If you are trying it with SSH, then the Host key Verification error can come due to several reasons.Follow these steps to overcome all the reasons.如果您使用 SSH 进行尝试,则主机密钥验证错误可能由于多种原因而出现。请按照以下步骤解决所有原因。

  1. Set the Environment variable as HOME and provide the address as the root directory of .ssh folder.将环境变量设置为HOME并提供地址作为.ssh文件夹的根目录。 eg:- If your .ssh is kept inside Name folder.例如:- 如果您的 .ssh 保存在Name文件夹中。 C:/Users/Name. C:/用户/名称。
  2. Now make sure that the public SSH key is being provided in the repository link also.现在确保在存储库链接中也提供了公共 SSH 密钥。 Either it is github or bitbucket or any other.它是 github 或 bitbucket 或任何其他。
  3. Open git bash.打开git bash。 And try cloning the project from the repository.并尝试从存储库克隆项目。 This will help in adding your repository URL in the known_host file, which is being auto created in the .ssh folder.这将有助于在 known_host 文件中添加您的存储库 URL,该文件正在 .ssh 文件夹中自动创建。
  4. Now open jenkins and create a new job.现在打开 jenkins 并创建一个新工作。 Then click on configure.然后点击配置。
  5. provide the cloning URL in Source code management under Git.在Git下的源代码管理中提供克隆URL。 The URL should be start with git@github.com/......... or ssh://proje........ URL 应该以 git@github.com/......... 或 ssh://proje.. 开头
  6. Under the Credential you need to add the username and password of your repository form which you are cloning the project.在 Credential 下,您需要添加要克隆项目的存储库表单的用户名和密码。 Select that credential.选择该凭据。
  7. And now apply and save the configuration.现在应用并保存配置。
  8. Bingo!答对了! Start building the project.开始构建项目。 I hope now you will not get any Host Key verification error!我希望现在您不会收到任何主机密钥验证错误!
  1. login as jenkins using: "sudo su -s /bin/bash jenkins"使用 jenkins 登录:“sudo su -s /bin/bash jenkins”
  2. git clone the desired repo which causes the key error git clone 导致关键错误的所需存储库
  3. it will ask you to add the key by showing Yes/No (enter yes or y)它会要求您通过显示是/否来添加密钥(输入是或 y)

that's it!而已!

you can now re-run the jenkins job.您现在可以重新运行 jenkins 作业。

I hope you this will fix your issue.我希望你这会解决你的问题。

Try尝试

ssh-keygen -R hostname ssh-keygen -R 主机名

-R hostname Removes all keys belonging to hostname from a known_hosts file. -R hostname 从known_hosts 文件中删除属于hostname 的所有键。 This option is useful to delete hashed hosts此选项对于删除散列主机很有用

issue is with the /var/lib/jenkins/.ssh/known_hosts.问题在于 /var/lib/jenkins/.ssh/known_hosts。 It exists in the first case, but not in the second one.它存在于第一种情况,但不存在于第二种情况。 This means you are running either on different system or the second case is somehow jailed in chroot or by other means separated from the rest of the filesystem (this is a good idea for running random code from jenkins).这意味着您要么在不同的系统上运行,要么第二种情况以某种方式被关在 chroot 中或通过其他方式与文件系统的其余部分分开(这是从 jenkins 运行随机代码的好主意)。

Next steps are finding out how are the chroots for this user created and modify the known hosts inside this chroot.接下来的步骤是找出这个用户的 chroot 是如何创建的,并修改这个 chroot 中的已知主机。 Or just go other ways of ignoring known hosts, such as ssh-keyscan, StrictHostKeyChecking=no or so.或者只是采用其他方式忽略已知主机,例如 ssh-keyscan、StrictHostKeyChecking=no 等。

After ssh-keygen probably one only needs to copy the public key to remote host with:在 ssh-keygen 之后,可能只需要将公钥复制到远程主机:

ssh-copy-id -i ~/.ssh/mykey user@host

Use ssh-keyscan should be much more easier:使用 ssh-keyscan 应该更容易:

ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts

This command will put all required hosts to ~/.ssh/known_hosts .此命令会将所有必需的主机放入~/.ssh/known_hosts You will need to run this command inside your Jenkins machine.您需要在 Jenkins 机器中运行此命令。 You can also create a job and put that command into the "Execute shell" section of the Configure of that job and then execute the job.您还可以创建一个作业并将该命令放入该作业配置的“执行外壳”部分,然后执行该作业。

I think, that many people didnt recognize, at least available since jenkins 2.361:我认为,很多人没有认识到,至少从 jenkins 2.361 开始可用:

在此处输入图像描述

btw.顺便提一句。 No Verification is for sure not the best option.无验证肯定不是最佳选择。

There is a safe and (relative easy) way to accomplish this, which should also work if you have separate worker nodes/clouds (like docker/kube.netes).有一种安全且(相对简单)的方法可以实现此目的,如果您有单独的工作节点/云(如 docker/kube.netes),这种方法也应该有效。

Adding host keys to Jenkins configuration将主机密钥添加到 Jenkins 配置

  • First go to a console and execute ssh-keyscan your_git_server.url首先 go 到控制台并执行ssh-keyscan your_git_server.url
  • Copy the output of that command复制该命令的 output
  • Then navigate to https://YOUR_JENKINS_URL/manage/configureSecurity/然后导航到https://YOUR_JENKINS_URL/manage/configureSecurity/
  • Scroll down to Git Host Key Verification Configuration向下滚动到Git 主机密钥验证配置
  • Paste the output of the command into the window. it should look like this:将命令的 output 粘贴到 window 中。它应该如下所示:

示例输出

Both bitbucket and github have pages about their keys and servers. bitbucket和 github 都有关于他们的密钥和服务器的页面。 Read them and ensure that you are adding the proper keys and not some random keys阅读它们并确保您添加的是正确的密钥而不是一些随机密钥

Getting the ssh-keyscan via your Jenkins installation通过 Jenkins 安装获取 ssh-keyscan

If you for some reason do not have ssh-keyscan , you can go to the script console ( https://YOUR_JENKINS_URL/manage/script ) and paste in the following script:如果您出于某种原因没有ssh-keyscan ,您可以 go 到脚本控制台 ( https://YOUR_JENKINS_URL/manage/script ) 并粘贴以下脚本:

def sout = new StringBuilder(), serr = new StringBuilder()
def proc = 'ssh-keyscan bitbucket.org'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "copy this to jenkins>\n$sout"
//println "err> $serr"

脚本输出

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

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