【问题标题】:Gitlab CI - SSH Permission denied (publickey,password)Gitlab CI - SSH权限被拒绝(公钥,密码)
【发布时间】:2017-06-05 07:11:04
【问题描述】:

我一直在尝试为我的项目设置 CD。我的 Gitlab CI 运行器和我的项目将在同一台服务器上。我关注了https://docs.gitlab.com/ee/ci/examples/deployment/composer-npm-deploy.html,但我不断收到SSH Permission denied (publickey,password). 错误。我所有的变量、私钥和其他变量都在项目设置中正确设置。

我使用没有密码的ssh-keygen -t rsa -C "my.email@example.com" -b 4096 命令创建了我的 ssh 密钥,并将我的PRODUCTION_PRIVATE_KEY 变量设置为~/.ssh/id_rsa 文件的内容。

这是我的gitlab-ci.yml

stages:
  - deploy

deploy_production:
  stage: deploy
  image: tetraweb/php
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$PRODUCTION_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - apt-get install rsync
  script:
    - ssh $PRODUCTION_SERVER_USER@$PRODUCTION_SERVER
    - hostname
  only:
    - master

这是 Gitlab CI 运行器的输出:

Running with gitlab-ci-multi-runner 9.2.0 (adfc387)
  on ci-test (1eada8d0)
Using Docker executor with image tetraweb/php ...
Using docker image sha256:17692e06e6d33d8a421441bbe9adfda5b65c94831c6e64d7e69197e0b51833f8 for predefined container...
Pulling docker image tetraweb/php ...
Using docker image tetraweb/php ID=sha256:474f639dc349f36716fb98b193e6bae771f048cecc9320a270123ac2966b98c6 for build container...
Running on runner-1eada8d0-project-3287351-concurrent-0 via lamp-512mb-ams2-01...
Fetching changes...
HEAD is now at dfdb499 Update .gitlab-ci.yml
Checking out dfdb4992 as master...
Skipping Git submodules setup
$ which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
/usr/bin/ssh-agent
$ eval $(ssh-agent -s)
Agent pid 12
$ ssh-add <(echo "$PRODUCTION_PRIVATE_KEY")
Identity added: /dev/fd/63 (rsa w/o comment)
$ mkdir -p ~/.ssh
$ echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
$ apt-get install rsync
Reading package lists...
Building dependency tree...
Reading state information...
rsync is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
$ ssh $PRODUCTION_SERVER_USER@$PRODUCTION_SERVER
Pseudo-terminal will not be allocated because stdin is not a terminal.
Warning: Permanently added '{MY_SERVER_IP}' (ECDSA) to the list of known hosts.
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
ERROR: Job failed: exit code 1

提前致谢。

【问题讨论】:

  • 您需要将公钥添加到服务器,以便将其识别为身份验证密钥。
  • 我使用来自 gitlab.com 的 gitlab。在这种情况下,服务器是指 gitlab.com 对吗?我已经在我的帐户中添加了公钥。我应该通过 gitlab-ci.yml 添加公钥吗?如果是这样,怎么做?顺便说一句,谢谢您的回复。
  • 没有。我说的是$PRODUCTION_SERVER
  • cat ~/.ssh/id_rsa.pub &gt; ~/.ssh/authorized_keys 并且有效。谢谢!
  • cat ~/.ssh/id_rsa.pub &gt; ~/.ssh/authorized_keys 将覆盖已添加的密钥。使用~/.ssh/id_rsa.pub &gt;&gt; ~/.ssh/authorized_keys

标签: ssh gitlab-ci


【解决方案1】:

您需要将公钥添加到服务器,以便将其识别为身份验证密钥。也就是将你使用的私钥对应的公钥内容粘贴到$PRODUCTION_SERVER上的~/.ssh/authorized_keys

【讨论】:

  • 谢谢,在拔牙一个小时左右后,这救了我 xD
  • 但是我们如何在 gitlab runner 上获取创建的私有 ssh 密钥的公钥?
  • 非常感谢!!!,奇怪的是在 gitlab 文档中他们根本没有提到任何关于此的内容!
  • @JainamJhaveri - 我也这么认为,因为您在 gitlab 上使用服务器私钥,所以您将生产服务器的公钥复制到其自己的 authorized_keys 中。 (需要是公共/私人对)
【解决方案2】:

这是对我有用的脚本:

before_script:
                - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
                - mkdir -p ~/.ssh
                - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
                - chmod 700 ~/.ssh/id_rsa
                - eval "$(ssh-agent -s)"
                - ssh-add ~/.ssh/id_rsa
                - ssh-keyscan -t rsa 64.227.1.160 > ~/.ssh/known_hosts
                - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
                - chmod 644 ~/.ssh/known_hosts

我还必须取消对变量的保护。

【讨论】:

    【解决方案3】:

    以下可以交替使用

    some_stage:
            - eval $(ssh-agent -s)
            - cd ~
            - touch id.rsa
            - echo "$SSH_PRIVATE_KEY" > id.rsa
            - chmod 700 id.rsa
            - ssh -o StrictHostKeyChecking=no -i id.rsa $SSH_USER@$SERVER
    

    【讨论】:

      【解决方案4】:

      还有一些重要的事情... ~/.ssh/authorized_keys 文件的权限应该是600。

      【讨论】:

        【解决方案5】:

        这也可能是由于您可以通过 ssh 访问的用户受到限制。

        就我而言,在服务器上,我得到了以下tail -f /var/log/auth.log

        ..
        Sep  6 19:25:59 server-name sshd[7943]: User johndoe from WW.XX.YY.ZZ not allowed because none of user's groups are listed in AllowGroups
        ..
        

        解决方案在于更新服务器文件/etc/ssh/sshd_config 上的AllowGroups 指令:

        AllowGroups janesmith johndoe
        

        【讨论】:

          【解决方案6】:

          在我们的例子中,我们一无所知,直到我们将标志 -v 添加到 SSH 命令(我们知道公钥设置是好的,因为我们能够从笔记本电脑使用私钥)。

          我们看到了:

          debug1: Offering public key: ... RSA SHA256:... agent
          95debug1: send_pubkey_test: no mutual signature algorithm
          

          并通过以下两个链接了解情况:我们的密钥是使用 RSA 格式生成的,该格式在最新的 openssh 版本中被认为是遗留的。

          所以你有两个解决方案:

          • 使用 ed25519 格式生成新密钥并在您的实例上设置公钥
          • 在您的 ssh 命令中使用下面这个额外的标志

          这应该是一个临时的解决方法:

          ssh -o PubkeyAcceptedKeyTypes=+ssh-rsa -o StrictHostKeyChecking=no your_user@your_instance_url "your command"
          

          如果您正在阅读本文,希望对您有所帮助。

          问候!

          【讨论】:

            猜你喜欢
            • 2019-03-08
            • 1970-01-01
            • 2018-04-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-11-24
            • 2014-11-08
            • 2019-03-11
            相关资源
            最近更新 更多