【问题标题】:SSH Host Key Verification Failed inside GitLab CIGitLab CI 中的 SSH 主机密钥验证失败
【发布时间】:2019-12-08 23:19:11
【问题描述】:

本地设置

我通过ssh-keygen 命令创建了一个公钥和私钥。

我决定先在本地设置私钥,然后在我的 repo 的 gitlab CI 上设置它。

我在服务器上设置了公钥(在这种情况下,是另一个 gitlab 存储库,但这可能会在未来发生变化并且不应该影响问题)。

我通过以下命令在本地成功地与服务器通信(在这种情况下,我通过git 使用 SSH,但将来可能会再次更改):

git clone git@gitlab.com:...../......git

GitLab CI 设置

然后我决定在 gitlab CI 上设置私钥和通信。

在我的仓库中,我导航到设置->持续集成->变量,并添加了以下环境变量:

  • SSH_DEPLOY_PRIVATE_KEY - 我曾经使用与本地相同的私钥
  • SSH_KNOWN_HOSTS
    • 我从本地计算机的 ~/.ssh/known_hosts 文件中获取了 gitlab.com 已知主机
    • gitlab.com,35.231.145.151 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFSMqzJeV9rUzU4kWitGjeR4PWSa29SPqJ1fVkhtj3Hw9xjLVXVYrU9QlYWrOLXBpQ6KWjbjTDTdDkoohFzgbEY=

然后我在 .gitlab-ci.yml 中设置 SSH:

script:
  - apt-get install openssh-client -y
  - eval $(ssh-agent -s)
  - echo "$SSH_DEPLOY_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
  - mkdir -p /.ssh && touch /.ssh/known_hosts
  - echo "$SSH_KNOWN_HOSTS" >> /.ssh/known_hosts
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh

这似乎工作正常,我收到以下消息:Identity added: (stdin) (runner@....)

然后我添加了相同的git clone 命令与服务器通信,但失败并出现以下错误:

Cloning into '......'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

在本地测试仍然有效。我使用上面相同的命令在本地设置 SSH(除了我使用pacman -S openssh 来安装)。

我该如何解决这个问题?

编辑

我知道我可以直接在 GitLab CI 中执行ssh-keyscan,理论上这应该可以解决问题,但据我所知,这很容易受到中间人攻击。我正在尝试寻找更安全的解决方案。

编辑 2

直接在 GitLab CI 中运行 ssh-keyscan 后,我收到相同的错误消息。

详细输出是一样的:

$ GIT_SSH_COMMAND="ssh -vvv" git clone git@gitlab.com:..../.....git deployed
Cloning into 'deployed'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

编辑 3

似乎已连接到互联网。另外apt-get install 将无法正常工作。

编辑 4

我不明白为什么这是一项如此艰巨的任务。我关注了this article,并且做的一切都正确。似乎还有很多其他类似的问题,也没有任何答案。这只是我们无法控制的 GitLab CI 问题吗?

我现在也认为这与 SSH 服务器是另一个 GitLab 存储库这一事实有关。也许 GitLab CI 会阻止同一网络内的 SSH 连接。不知道为什么,但这是一种可能性。也不知道没有 SSH 怎么连接。

编辑 5

使用GIT_SSH_COMMAND,详细输出显然无法正常工作,所以我尝试了没有 git 的 ssh 连接:

ssh -vvvv git@gitlab.com

日志输出:

OpenSSH_6.7p1 Debian-5+deb8u5, OpenSSL 1.0.1t  3 May 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
Pseudo-terminal will not be allocated because stdin is not a terminal.
debug2: ssh_connect: needpriv 0
debug1: Connecting to gitlab.com [35.231.145.151] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.7p1 Debian-5+deb8u5
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.8
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.8 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug3: load_hostkeys: loading entries for host "gitlab.com" from file "/root/.ssh/known_hosts"
debug3: load_hostkeys: loaded 0 keys
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com,ssh-rsa-cert-v00@openssh.com,ssh-dss-cert-v00@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-md5-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-md5-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: first_kex_follows 0 
debug2: kex_parse_kexinit: reserved 0 
debug2: kex_parse_kexinit: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1
debug2: kex_parse_kexinit: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
debug2: kex_parse_kexinit: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: kex_parse_kexinit: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: kex_parse_kexinit: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: kex_parse_kexinit: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: kex_parse_kexinit: none,zlib@openssh.com
debug2: kex_parse_kexinit: none,zlib@openssh.com
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: first_kex_follows 0 
debug2: kex_parse_kexinit: reserved 0 
debug2: mac_setup: setup umac-64-etm@openssh.com
debug1: kex: server->client aes128-ctr umac-64-etm@openssh.com none
debug2: mac_setup: setup umac-64-etm@openssh.com
debug1: kex: client->server aes128-ctr umac-64-etm@openssh.com none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA f1:d0:fb:46:73:7a:70:92:5a:ab:5d:ef:43:e2:1c:35
debug3: load_hostkeys: loading entries for host "gitlab.com" from file "/root/.ssh/known_hosts"
debug3: load_hostkeys: loaded 0 keys
debug3: load_hostkeys: loading entries for host "35.231.145.151" from file "/root/.ssh/known_hosts"
debug3: load_hostkeys: loaded 0 keys
debug1: read_passphrase: can't open /dev/tty: No such device or address
Host key verification failed.

倒数第二行表明它正在尝试使用/dev/tty 文件与终端通信。当然,这个脚本是在非交互式庄园中运行的,所以它失败了。它不应该使用我的密钥而不是从终端请求密码吗?

【问题讨论】:

    标签: ssh gitlab gitlab-ci


    【解决方案1】:

    您可能需要尝试将模式设置为 644 而不是 700。644 是 Verifying the SSH host keys 文档中建议的内容,也是 SSH 默认用于此文件的内容。 SSH 的某些部分对此非常特别 - 我不确定 known_hosts 是否特别。

    文档还提到您应该将SSH_KNOWN_HOSTS 变量的值设置为ssh-keyscan整个 输出,因为有多个键。

    编辑:

    以下.gitlab-ci.yml 在 GitLab.com 上为我工作。注意使用~/.ssh/ 而不是/.ssh/

    image: ubuntu:latest
    
    test_job:
      script:
      - apt-get update
      - apt-get install openssh-client git-core -y
      - eval $(ssh-agent -s)
      - echo "$SSH_DEPLOY_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
      - mkdir -p ~/.ssh && touch ~/.ssh/known_hosts
      - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
      - git clone git@gitlab.com:gitlab-org/gitlab-ce.git
    

    【讨论】:

    • 我尝试将ssh-keyscan 的整个输出放入SSH_KNOWN_HOSTS,但这不起作用,所以我将本地~/.ssh/known_hosts 文件的内容放入。我试试改成644
    • 不,没有解决它。 PS你的链接坏了。
    • 我修复了链接。
    • 我用有效的.gitlab-ci.yml 更新了我的答案。
    • 就是这样!它正在工作!添加~ 这样一个简单的修复。我简直不敢相信!
    【解决方案2】:

    在负载平衡服务器的情况下,一个服务器可以有多个签名。理想的方式是改变

    - echo "$SSH_KNOWN_HOSTS" >> /.ssh/known_hosts
    

    - ssh-keyscan www.gitlab.com >> /.ssh/known_hosts
    

    【讨论】:

    • 感谢您的回答。我应该提到我已经知道这一点,但这很容易受到中间人攻击,对吧?还有其他解决方案吗?这对我的项目并不重要,但我正在尝试找出一个安全的解决方案。不过,我实际上会对此进行测试,以确保已知主机是问题所在。
    • 顺便说一句,由于复制粘贴,我意识到了一个错误,它应该 echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts,我假设 ~ 在此工作
    • 另外我建议使用详细模式,如askubuntu.com/a/620985
    • 你可以添加 curl ipinfo.io 并确保网络在容器内工作
    • 我刚刚意识到你也说过要包含~ home 符号,所以现在两个人解决了我的问题。当时添加主页符号并没有修复它,因为我正在尝试其他方法来修复它:facepalm:
    【解决方案3】:

    我不建议使用相同的私钥。出于安全原因,也因为它可能会导致其他问题。另外,请确保authorized_keys 文件包含您要访问的服务器上的公钥,否则整个事情将无法正常工作。 我假设您已按照this one 的指南进行操作?

    希望这能有所作为。

    【讨论】:

    • "我不建议使用相同的私钥吗?"您的意思是在本地与 gitlab CI 上的相同?如果是这样,我没有在本地使用那个,我只是在本地测试它,以确定问题是出在键上还是出在 gitlab CI 上。
    • 是的,我遵循了该指南。我无法控制服务器 - 服务器是另一个 gitlab 存储库(我使用他们的用户界面设置公钥)。但是,在本地测试它是有效的,所以据我所知,这不是问题。
    • 是的,这就是我的意思。对不起,只是我的一个误解 :D 对不起,我现在有点没有想法。祝你好运,如果我有任何想法,我会回来的。
    猜你喜欢
    • 2019-08-10
    • 2020-07-15
    • 1970-01-01
    • 2021-05-28
    • 2019-10-21
    • 2021-04-13
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    相关资源
    最近更新 更多