dongmei2351 2019-04-03 05:26
浏览 520

主机密钥不匹配

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?

  • 写回答

1条回答 默认 最新

  • dongtaijiao7140 2019-04-04 02:46
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站