SSH2 Node.js SFTP协议错误 握手失败。

9

你好,我有一个小问题。我使用Node.js开发了一个SFTP客户端脚本,用于连接到SFTP服务器并获取一些文件。我在本地服务器上测试过了,它可以正常工作。但是当我尝试在生产服务器上使用它时,我收到了以下错误:

Error: 握手失败:没有匹配的密钥交换算法

我已经使用 ssh-keygen 生成了RSA密钥。

下面是脚本的相关部分:

var Client = require('ssh2').Client;
var fs = require('fs');
var path = require('path');

var args = process.argv.slice(2);

var connSettings = {
    host: args[0] || '127.0.0.1',
    port: args[1] || 22,
    username: args[2] || 'karim',
    password: args[3] || 'karimos',
    algorithms: {
        hmac: ['hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1', 'hmac-sha1-96']
    }

};
6个回答

9

我也遇到了同样的问题,并通过添加以下内容解决了它:

algorithms: {
        kex: [
          "diffie-hellman-group1-sha1",
          "ecdh-sha2-nistp256",
          "ecdh-sha2-nistp384",
          "ecdh-sha2-nistp521",
          "diffie-hellman-group-exchange-sha256",
          "diffie-hellman-group14-sha1"
        ],
        cipher: [
          "3des-cbc",
          "aes128-ctr",
          "aes192-ctr",
          "aes256-ctr",
          "aes128-gcm",
          "aes128-gcm@openssh.com",
          "aes256-gcm",
          "aes256-gcm@openssh.com"
        ],
        serverHostKey: [
          "ssh-rsa",
          "ecdsa-sha2-nistp256",
          "ecdsa-sha2-nistp384",
          "ecdsa-sha2-nistp521"
        ],
        hmac: [
          "hmac-sha2-256",
          "hmac-sha2-512",
          "hmac-sha1"
        ]
    }

6

对于自己,我将 debug: console.log 添加到我的配置对象中。这会输出有关连接尝试的更多信息。

{
    "port": 22,
    "host": "test.test.com",
    "user": "test",
    "password": "******",
    "debug": console.log
}

握手: (远程) KEX 方法: diffie-hellman-group14-sha1, diffie-hellman-group-exchange-sha1

握手: 无匹配的密钥交换算法

根据该错误,我更新了我的配置算法:

{
    "port": 22,
    "host": "test.test.com",
    "user": "test",
    "password": "******",
    "algorithms": {
        "kex": [
            "diffie-hellman-group14-sha1","diffie-hellman-group-exchange-sha1"
        ]
    }
}

在我机器上加入这个算法后,连接成功了。


2
你可以在服务器上编辑/etc/ssh/sshd配置文件,以允许使用密钥身份验证方法 :)

你能给我一些参考资料吗?但是我已经使用FileZilla连接到服务器并且它可以工作,我也尝试过不使用algorithms: { hmac: ['hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1', 'hmac-sha1-96'] },但它没有起作用。 - KarimS

2

你尝试过将算法声明更改为...吗?

算法: { 服务器主机密钥: ['hmac-sha2-256','hmac-sha2-512','hmac-sha1','hmac-sha1-96'], }


2
我觉得第一步建议是升级你连接的服务器上的SSH服务器,以获得更安全的配置。这是最佳/最安全的解决方案。
如果你无法对该服务器进行更改并且绝对需要连接,则可以明确设置kex以支持您想要支持的密钥交换方法列表(有效的算法名称可在{{link1:ssh2-streams文档}}中找到)。例如:
algorithms: {
  kex: [ ... ]
}

0
添加了 "diffie-hellman-group-exchange-sha1", 我收到了评论错误未知的DH组
"kex": [
  "ecdh-sha2-nistp256",
  "ecdh-sha2-nistp384",
  "ecdh-sha2-nistp521",
  "diffie-hellman-group-exchange-sha256",
  "diffie-hellman-group14-sha1",
  "diffie-hellman-group-exchange-sha1",
  "diffie-hellman-group1-sha1"
],
"cipher": [
  "3des-cbc",
  "aes128-ctr",
  "aes192-ctr",
  "aes256-ctr",
  "aes128-gcm",
  "aes128-gcm@openssh.com",
  "aes256-gcm",
  "aes256-gcm@openssh.com"
],
"serverHostKey": [
  "ssh-rsa",
  "ecdsa-sha2-nistp256",
  "ecdsa-sha2-nistp384",
  "ecdsa-sha2-nistp521"
],
"hmac": [
  "hmac-sha2-256",
  "hmac-sha2-512",
  "hmac-sha1"
]

引用

 "diffie-hellman-group-exchange-sha1",

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