gCloud instance: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

huangapple go评论92阅读模式
英文:

gCloud instance: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

问题

当我尝试通过SSH连接到Ubuntu 22.04的gcloud实例时,出现以下错误:

Ubuntu版本:

没有可用的LSB模块。
发行商ID:Ubuntu
描述:Ubuntu 22.04 LTS
版本:22.04
代号:jammy

错误信息:

ssh: 握手失败:ssh: 无法进行身份验证,尝试的方法为[none publickey],没有支持的方法可用

只有在使用golang的ssh模块goph时才会出现此错误,但是当我手动使用ssh -i ~/.ssh/id_rsa 34.132.133.12命令时,我可以成功进行SSH连接。

这是我的连接代码片段:

func Connect(privateKey, username string, ip net.Addr) (*goph.Client, error) {
	// 使用私钥开始新的SSH连接。
	priKey, err := goph.Key(privateKey, "")
	if err != nil {
		return nil, fmt.Errorf("无法获取私钥:%v 错误:%v", privateKey, err)
	}

	c := 0
	for {
		log.Printf("SSH到:%v", ip)

		s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) // 创建新的旋转器
		s.Start()
		time.Sleep(20 * time.Second)
		s.Stop()

		client, err := goph.NewUnknown(username, ip.String(), priKey)
		if err != nil {
			c++
		} else {
			log.Printf("已连接到:%v", ip.String())
			return client, nil
		}

		if c >= 3 {
			return nil, fmt.Errorf("无法连接到 %v,错误:%v", ip.String(), err)
		}
	}
}

当我的代码运行在以下Ubuntu gCloud版本时,可以成功进行SSH连接:

没有可用的LSB模块。
发行商ID:Ubuntu
描述:Ubuntu 16.04.7 LTS
版本:16.04
代号:xenial

我尝试了许多解决方案,例如将私钥权限更改为600(已经设置好了)。我还尝试了其他建议,如ssh-add private key,但仍然遇到相同的问题。

我在SSH连接到两个Ubuntu版本时都运行了详细模式,并且两个操作系统都接受了公钥:

debug1: 可以继续进行的身份验证方法:publickey
debug1: 下一个身份验证方法:publickey
debug2: 我们发送了一个公钥数据包,等待回复
debug1: 服务器接受了密钥:

创建和设置此实例是使用gCloud的golang客户端完成的。似乎需要在实例中进行一些设置,因为我可以在旧的Ubuntu版本上进行SSH连接。

英文:

I'm getting the following error when I try to ssh into a Ubuntu 22.04 gcloud instance:

Ubuntu Version:

No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04 LTS
Release:	22.04
Codename:	jammy

Error:

ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

This error only happens when I use the golang ssh module goph, but I'm able to ssh when ssh manually with: ssh -i ~/.ssh/id_rsa 34.132.133.12.

Here is my connect snippet:

func Connect(privateKey, username string, ip net.Addr) (*goph.Client, error) {
	// Start new ssh connection with private key.
	priKey, err := goph.Key(privateKey, "")
	if err != nil {
		return nil, fmt.Errorf("Could not get privateKey: %v error: %v", privateKey, err)
	}

	c := 0
	for {
		log.Printf("Ssh to: %v", ip)

		s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) // Build our new spinner
		s.Start()
		time.Sleep(20 * time.Second)
		s.Stop()

		client, err := goph.NewUnknown(username, ip.String(), priKey)
		if err != nil {
			c++
		} else {
			log.Printf("Connected to: %v", ip.String())
			return client, nil
		}

		if c >= 3 {
			return nil, fmt.Errorf("Could not connect to %v, error: %v", ip.String(), err)
		}
	}
}

My code is able to ssh when running against this Ubuntu gCloud Version:

No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.7 LTS
Release:	16.04
Codename:	xenial

I tried many solutions like changing the private key permission to 600 which was already setup. I also did other suggestion to ssh-add private key and still having the same issue.

Also I ran verbose when ssh into both ubuntu version and both OS accepted publickey:

debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: 

The creation and setting up of this instance is done by using the gCloud golang client.
It seems that it is a settings that needs to be done in the instance since I'm able to ssh on older Ubuntu versions.

答案1

得分: 1

在https://github.com/melbahja/goph/issues/26上发现了一个错误。在我发布问题之前,当我搜索时,我没有意识到我开启了未解决问题的标志,所以它没有显示这个错误。所以我回去再次检查,并看到了这个问题。

我更新了所有的模块,因为我没有太多的模块,我执行了以下操作:

go get -u ./...
go mod tidy

现在我能够进行ssh连接了。谢谢。

英文:

There was a bug on https://github.com/melbahja/goph/issues/26. When I searched before I posted the question, I did not realize that I left the open issue flag on, so it did not show this bug. So I went back to double-check and saw the issue.

I updated all my modules since I don't have much by doing the following:

go get -u ./...
go mod tidy

Now I'm able to ssh. Thanks

huangapple
  • 本文由 发表于 2022年6月19日 06:09:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/72673084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定