【问题标题】:Cant authenticate to remote vps using ssh in gitlab无法在 gitlab 中使用 ssh 对远程 vps 进行身份验证
【发布时间】:2019-03-11 19:30:27
【问题描述】:

我之前使用下面的 gitlab ci/cd 配置连接到 vps。但在最近的服务器上我不能。

我的 .yml 文件如下:

deploy:
  environment:
    name: production
    url: http://erfantahvieh.com/
  image: php:7.1.1
  stage: deploy
  only:
    - master
  before_script:
    - apt-get update -yqq
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - mkdir -p ~/.ssh
    - echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 700 ~/.ssh
    - chmod 700 ~/.ssh/id_rsa
    - eval "$(ssh-agent -s)"
    - ssh-add ~/.ssh/id_rsa
    - ssh-keyscan -H $HOST >> ~/.ssh/known_hosts
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

  script:
    - ssh $USER@$HOST "ls -la"

知道当我运行管道时,输出如下所示:

$ mkdir -p ~/.ssh
$ echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
$ chmod 700 ~/.ssh
$ chmod 700 ~/.ssh/id_rsa
$ eval "$(ssh-agent -s)"
Agent pid 201
$ ssh-add ~/.ssh/id_rsa
Identity added: /root/.ssh/id_rsa (rsa w/o comment)
$ ssh-keyscan -H $HOST >> ~/.ssh/known_hosts
# xxx.xxx.xxx.xxx SSH-2.0-OpenSSH_7.4
# xxx.xxx.xxx.xxx SSH-2.0-OpenSSH_7.4
# xxx.xxx.xxx.xxx SSH-2.0-OpenSSH_7.4
$ [[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
$ ssh $USER@$HOST "ls -la"
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
ERROR: Job failed: exit code 1

怎么了? 为什么我以前用完全相同的代码连接,但现在不能? 有什么我错过的吗?

注意:我在我的 vps 中创建了 ssh 私钥,并将其放入 secret var 中 gitlab ci/cd 设置,例如 $HOST 和 $USER。

【问题讨论】:

    标签: ssh gitlab gitlab-ci vps private-key


    【解决方案1】:

    出现问题是因为您的.ssh 文件夹中应该有authorized_keys 文件,以让其他机器使用ssh private key 登录您的服务器。

    如果没有此文件,您可以通过以下命令创建此文件。

    cd ~/.ss
    cp id_rsa.pub authorized_keys
    systmctl restart sshd
    

    【讨论】:

      猜你喜欢
      • 2018-07-04
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 2022-07-08
      • 2013-05-30
      • 2020-11-08
      • 1970-01-01
      相关资源
      最近更新 更多