解决ssh: no common algorithm for key exchange; client offered: [curve25519-sha256@libssh.org ecdh

博客讲述了在使用SSH连接时遇到的‘no common algorithm for key exchange’错误,问题在于客户端和服务器端不支持相同的密钥交换算法。通过在客户端配置中明确指定‘diffie-hellman-group-exchange-sha256’算法,成功解决了该问题,实现了SSH的正常连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错如下

ssh: no common algorithm for key exchange; client offered: [curve25519-sha256@libssh.org ecdh-sha2-nistp256 ecdh-sha2-nistp384 ecdh-sha2-nistp521 diffie-hellman-group14-sha1], server offered: [diffie-hellman-group-exchange-sha256]

基于SSH连接时报的错

报错时的代码:

	var clientConfig *ssh.ClientConfig
	clientConfig = &ssh.ClientConfig{
		User:            c.Username,
		Auth:            auth,
		Timeout:         10 * time.Second,
		HostKeyCallback: ssh.InsecureIgnoreHostKey(), 
	}

	sshClient := new(ssh.Client)
	if sshClient, err = ssh.Dial("tcp", fmt.Sprintf("%s:%d", c.Host, c.Port), clientConfig); err != nil {
		return
	}

解决其实很简单

要指定好"diffie-hellman-group-exchange-sha256"算法,就是报错中提到的;

成功的代码

	var clientConfig *ssh.ClientConfig
	clientConfig = &ssh.ClientConfig{
		User:            c.Username,
		Auth:            auth,
		Timeout:         10 * time.Second,
		HostKeyCallback: ssh.InsecureIgnoreHostKey(), 
	}

    clientConfig.KeyExchanges = append(clientConfig.KeyExchanges, "diffie-hellman-group-exchange-sha256")

	sshClient := new(ssh.Client)
	if sshClient, err = ssh.Dial("tcp", fmt.Sprintf("%s:%d", c.Host, c.Port), clientConfig); err != nil {
		return
	}

如果解决了你的问题,欢迎留下足迹哦!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ProblemTerminator

您的鼓励将是作者最大的动力哦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值