GitLab CI:SSH 失败,无法验证私钥身份证明

3

我参照了这个链接尝试在Gitlab-CI中SSH到我的服务器。对于SSH密钥,我进入服务器并生成了公钥和私钥。私钥被提取到GitLab CI / CD env变量中。

YAML模板如下,大部分内容来自该链接。

    image: docker:19.03.8
      services:
        - docker:19.03.8-dind

    deployment:
      variables:
        ip: <ip-address>
      script:
        - apk add --update openssh-client sshpass
        - eval $(ssh-agent -s)
        - echo "$SSH_PRIVATE_KEY" | ssh-add - > /dev/null
        - mkdir -p ~/.ssh
        - chmod 700 ~/.ssh
        - export SSHPASS=$AWS_PASSWORD
        - sshpass -e ssh -o StrictHostKeyChecking=no -vvv ubuntu@$ip echo testing

然而,我在试图访问私钥时遇到了错误。

    debug1: Authentications that can continue: publickey,password
    debug1: Trying private key: /root/.ssh/id_rsa
    debug3: no such identity: /root/.ssh/id_rsa: No such file or directory
    debug1: Trying private key: /root/.ssh/id_dsa
    debug3: no such identity: /root/.ssh/id_dsa: No such file or directory
    debug1: Trying private key: /root/.ssh/id_ecdsa
    debug3: no such identity: /root/.ssh/id_ecdsa: No such file or directory
    debug1: Trying private key: /root/.ssh/id_ed25519
    debug3: no such identity: /root/.ssh/id_ed25519: No such file or directory
    debug1: Trying private key: /root/.ssh/id_xmss
    debug3: no such identity: /root/.ssh/id_xmss: No such file or directory
    debug2: we did not send a packet, disable method
    debug3: authmethod_lookup password
    debug3: remaining preferred: ,password
    debug3: authmethod_is_enabled password
    debug1: Next authentication method: password
    debug3: send packet: type 50
    debug2: we sent a password packet, wait for reply
    debug3: receive packet: type 51
    debug1: Authentications that can continue: publickey,password
    Permission denied, please try again.

我正在使用GitLab共享的Runner,如果有帮助的话。

[更新]

忘记提到了,在我想要连接的服务器上,我将生成的公钥 id_rsa.pub 添加到了 authorized_keys 文件中。

[编辑1]

如建议所述,我已经使用ssh-keyscan添加了已知主机,并将输出作为变量 $SSH_KNOWN_HOSTS。以下是更新后的yaml文件。但我遇到了相同的错误。

    deployment:
      variables:
        ip: <ip-address>
      script:
        - apk add --update openssh-client sshpass
        - eval $(ssh-agent -s)
        - echo "$SSH_PRIVATE_KEY" | ssh-add - > /dev/null
        - mkdir -p ~/.ssh
        - chmod 700 ~/.ssh
        - touch ~/.ssh/known_hosts
        - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts
        - export SSHPASS=$AWS_PASSWORD
        - sshpass -e ssh -o StrictHostKeyChecking=no -vvv ubuntu@$ip echo testing

你尝试过访问以下链接了吗?https://dev59.com/p67la4cB1Zd3GeqPeogW#57091973 和 https://superuser.com/questions/1222574/sshpass-not-working-permision-denied ? - DV82XL
@DV82XL,我尝试了你之前关于SSH_KNOWN_HOST(编辑)的建议。你添加的另外两个链接我之前也试过了。现在想想,如果我使用公钥-私钥方法,我就不需要使用sshpass登录,不确定为什么它不起作用。 - Jake
你的最终目标是什么?你是想通过SSH连接到远程服务器去运行Linux命令或bash脚本吗?防火墙或代理有关系吗?你是否处于公司网络中? - DV82XL
目标是在服务器上运行一个bash脚本来部署我的容器。不,我不在公司网络中。由于我可以正常使用ssh/sshpass从我的本地机器,所以不应该有任何阻止它的东西。 - Jake
1个回答

4

我不确定sshpass,因为通常我使用公共/私有密钥。以下是我设置的一个示例作业,用于在远程服务器上运行SCP/SSH命令:

deploy:
  stage: deploy
variables:
  hostname: app-dev
before_script:
  # optional step if you decide to use a hostname instead of IP address
  - cp -f ./network/etc/hosts /etc/hosts
  # Setup SSH
  - which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
  - eval $(ssh-agent -s)
  - ssh-add <(cat $SSH_PRIVATE_KEY)
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - ssh-keyscan $HOSTNAME >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts
script:
  # Copy files and execute commands
  - scp ./scripts/install_package.sh root@$HOSTNAME:/tmp/deploy
  - ssh root@$HOSTNAME "/tmp/deploy/install_package.sh && exit"

在运行流水线之前,您需要执行以下操作:

  1. 使用 ssh-keygen 生成 ssh 密钥对。不要使用密码短语。公钥以 .pub 结尾,私钥没有扩展名。
  2. SSH 连接到远程服务器,将 公钥 内容复制到 ~ / .ssh / authorized_keys
  3. 将您的私钥内容复制到 GitLab 的文件环境变量中称为 SSH_PRIVATE_KEY
  4. 如果使用 $HOSTNAME 环境变量,请在流水线中定义该变量,并将 IP / 主机名添加到流水线容器中的 /etc/hosts 文件中。否则,只需使用 IP 地址即可。

谢谢您的帮助~关于ssh-keygen,我应该在哪里生成它?本地机器?还是我想连接的远程服务器?我之前做的是后者。 - Jake
@Jake ssh-keygen是OpenSSH的CLI工具之一,随Windows一起提供,并可在Linux上使用。最重要的是2个输出密钥(文本文件),因此无论在哪里运行都没有关系。我通常在Windows上运行它。 - DV82XL
@Jake 我通常在我的个人笔记本电脑上生成密钥,这将复制我的个人主机名,因此团队中的其他人会知道我在authorized_keys中创建了该密钥。这应该没有关系。当您尝试此方法时,您遇到了什么错误? - DV82XL
1
我认为我在服务器上漏掉了添加授权密钥的步骤...显然我把它放错了服务器~ :x 但是至少现在在您的帮助下更清楚地了解了Gitlab中Ssh的工作原理。 - Jake
1
请确保在GitLab的用户界面中,变量类型为“文件”的末尾添加换行符。 - Roel4811
显示剩余4条评论

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