You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure 'Copy files over SSH'任务连接失败,但手动SSH可成功

解决Azure DevOps SSH复制任务失败但手动连接成功的问题

问题概述

同一网络内两台Ubuntu 22虚拟机:一台部署Azure Build Agent,另一台为生产VM,均配置SSH。手动从Build Agent VM用私钥连接生产VM成功,但Azure DevOps流水线的「Copy files over SSH」任务失败,报错:

Error: Failed to connect to remote machine. Verify the SSH service connection details. Failed to connect to remote machine. Verify the SSH service connection details. Error: All configured authentication methods failed..

可能原因及解决方法

1. 私钥文件权限不符合SSH要求

SSH对私钥文件权限要求严格,若权限过松(比如其他用户可读)会直接拒绝认证。手动连接时你可能已设置正确权限,但Azure DevOps Agent运行的用户(如vstsagent)使用的私钥权限错误。

解决步骤:

  • 切换到Agent运行的用户:
    sudo su - vstsagent
    
  • 找到私钥文件,设置权限为仅所有者可读可写:
    chmod 600 /path/to/your/private_key
    
  • 同时确保私钥所在的.ssh目录权限为700:
    chmod 700 ~/.ssh
    

2. 私钥格式不兼容

Azure DevOps的SSH服务连接可能仅支持PEM格式私钥,而你手动使用的是OpenSSH格式(开头为-----BEGIN OPENSSH PRIVATE KEY-----),部分SSH客户端对OpenSSH格式兼容性差。

解决步骤:

  • 将OpenSSH格式私钥转换为PEM格式:
    ssh-keygen -p -m PEM -f /path/to/your/private_key
    
  • 把转换后的PEM格式私钥内容更新到Azure DevOps的SSH服务连接中。

3. Agent用户缺少SSH配置

手动连接时,你的用户可能有~/.ssh/config配置文件(包含Host别名、端口等设置),但Agent运行的用户没有该配置,导致连接参数不匹配。

解决步骤:

  • 将手动连接用户的~/.ssh/config复制到Agent用户的.ssh目录:
    sudo cp /home/your-user/.ssh/config /home/vstsagent/.ssh/
    sudo chown vstsagent:vstsagent /home/vstsagent/.ssh/config
    
  • 设置配置文件权限:
    chmod 600 /home/vstsagent/.ssh/config
    

4. 生产VM的sshd_config限制

生产VM的SSH服务可能限制了允许登录的用户,或关闭了公钥认证(手动能连的话概率低,但需确认)。

解决步骤:

  • 编辑生产VM的sshd_config文件:
    sudo nano /etc/ssh/sshd_config
    
  • 确认以下配置:
    PubkeyAuthentication yes
    # 若设置了AllowUsers,确保包含Agent连接用的用户名
    # AllowUsers your-user agent-user
    
  • 重启SSH服务:
    sudo systemctl restart sshd
    

5. Azure DevOps服务连接配置错误

可能是私钥内容填写错误、私钥密码(Passphrase)未正确设置,或目标主机/用户名填写有误。

解决步骤:

  • 检查Azure DevOps中SSH服务连接的「Server」「Username」是否与手动连接时一致。
  • 若私钥有密码,确保「Passphrase」字段填写正确。
  • 重新粘贴PEM格式的私钥内容,避免有多余空格或换行。

验证方法

在Build Agent VM上,切换到Agent运行的用户,手动执行SSH连接测试:

sudo su - vstsagent
ssh -i /path/to/your/private_key agent-user@production-vm-ip

如果此命令失败,说明问题出在Agent用户的本地配置;如果成功,再检查Azure DevOps服务连接的配置细节。

内容的提问来源于stack exchange,提问作者mr-woodapple

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 20:51:15