主机密钥不匹配

I am trying to connect to a remote server using Go. This is what I am using: (SSH Handshake complains about missing host key)

    key, err := ioutil.ReadFile("/Users/pankaj/.ssh/id_rsa")                                                                
    if err != nil {                                                                                                         
        log.Fatalf("unable to read private key: %v", err)                                                                   
    }                                                                                                                       

    // Create the Signer for this private key.                                                                              
    signer, err := ssh.ParsePrivateKey(key)                                                                                 
    if err != nil {                                                                                                         
        log.Fatalf("unable to parse private key: %v", err)                                                                  
    }        

    hostKeyCallback, err := knownhosts.New("/Users/pankaj/.ssh/known_hosts")
    if err != nil {
        log.Fatal(err)
    }

    sshConfig := &ssh.ClientConfig{
        User: "pankaj",
        Auth: []ssh.AuthMethod{
            ssh.PublicKeys(signer),
        },
        HostKeyCallback: hostKeyCallback,
    }

    conn, err := ssh.Dial("tcp", "dev.letsreap.com:22", sshConfig) 
    if err != nil {                                                                                                         
        log.Fatalf("unable to connect: %v", err)                                                                            
    } 

However on ssh.Dial I am getting:

ssh: handshake failed: knownhosts: key mismatch

I have verified that I can connect to the remote server from the command line. Also I can connect successfully if I use ssh.InsecureIgnoreHostKey(). What am I missing?

ssh
doulei8861
doulei8861 不。只有一个。
一年多之前 回复
dongre6404
dongre6404 您的knownhosts文件中是否有dev.letsreap.com的多个条目?
一年多之前 回复
duanran3115
duanran3115 添加了其他代码。
一年多之前 回复
dongsuoying9059
dongsuoying9059 该错误消息是很清楚的问题。但是您没有包含足够的代码来诊断为什么键不匹配。
一年多之前 回复

1个回答



我最终知道了。 实际上,known_hosts文件中有多个条目,一个条目的主机名,另一个条目的IP地址。 这就是为什么我没有较早发现它的原因。 谢谢@JimB的提示!</ p>
</ div>

展开原文

原文

I figured it out eventually. There were indeed multiple entries in known_hosts file, one with the hostname and another with the IP address. That's why I did not spot it earlier. Thanks @JimB for the hint!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐