通过 SSH 将 gitlab-runner(服务器)连接到虚拟机失败:权限被拒绝(公钥、密码)

Posted

技术标签:

【中文标题】通过 SSH 将 gitlab-runner(服务器)连接到虚拟机失败:权限被拒绝(公钥、密码)【英文标题】:Connecting gitlab-runner (server) to virtual machine via SSH fails: Permission denied (publickey,password) 【发布时间】:2021-03-26 16:06:14 【问题描述】:

我尝试设置一个运行脚本,它将通过 SSH 连接到 VM(应用程序运行的地方)。在环境变量(CICD 配置 -> 变量)中,我存储用于连接到 VM 服务器的私钥(ed25519)。但是随后出现以下错误消息..有人知道该怎么做吗?我可以检查以解决问题的可能问题的建议?

您可以在下面找到运行脚本 .gitlab-ci.yml 和出现的错误消息。

CI-RUNNER 脚本

build_job:
  stage: build
  script:
    - apt-get update -y
    - apt-get install openssh-client bash
    - mkdir ~/.ssh/
    - echo "$SSH_PRIVATE_KEY_CI_RUNNER" > ~/.ssh/id_ed25519
    - echo "$SSH_KNOWN_HOST_VM" > ~/.ssh/known_hosts
    - chmod -R 600 ~/.ssh/
 

    - ssh -v user@host -p 2222 "ls -a"

错误消息

debug1: Host '[server-address]:2222' is known and matches the ECDSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: Will attempt key: /root/.ssh/id_rsa 
debug1: Will attempt key: /root/.ssh/id_dsa 
debug1: Will attempt key: /root/.ssh/id_ecdsa 
debug1: Will attempt key: /root/.ssh/id_ed25519 
debug1: Will attempt key: /root/.ssh/id_xmss 
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /root/.ssh/id_xmss
debug1: Next authentication method: password
debug1: read_passphrase: can't open /dev/tty: No such device or address
debug1: Authentications that can continue: publickey,password
Permission denied, please try again.
debug1: read_passphrase: can't open /dev/tty: No such device or address
debug1: Authentications that can continue: publickey,password
Permission denied, please try again.
debug1: read_passphrase: can't open /dev/tty: No such device or address
debug1: Authentications that can continue: publickey,password
debug1: No more authentication methods to try.
user@serveradress: Permission denied (publickey,password).
ERROR: Job failed: exit code 1

【问题讨论】:

【参考方案1】:

为了进行测试,请先尝试使用不受密码保护的 SSH 私钥。

这将避免“read_passphrase:无法打开/dev/tty:没有这样的设备或地址”并验证密钥是否有效。

这假定公钥已发布到“host~user/.ssh/authorized_keys,并且 SSH 守护程序正在侦听端口 2222。

【讨论】:

以上是关于通过 SSH 将 gitlab-runner(服务器)连接到虚拟机失败:权限被拒绝(公钥、密码)的主要内容,如果未能解决你的问题,请参考以下文章

Gitlab-Runner:从主服务器克隆的权限被拒绝

RoR cap 部署与 gitlab-runner 卡住并且没有触发通过响应

如何利用Gitlab-ci持续部署到远程机器?

为 gitlab 配置 ssh 运行程序 ssh denied

另一台服务器上的Gitlab-runner

gitlab-runner 随机运行为 root 而不是用户 gitlab-runner