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?
For those who are facing the same issue, it looks like ecdsa-sha2-nistp256 is used as the host key algorithm by default, at least that's what I experienced in my case.
You can specify your host key algorithm in HostKeyAlgorithms field like so:
conf := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: hostKeyCallback,
HostKeyAlgorithms: []string{ssh.KeyAlgoED25519},
}
And make sure that a public key of type ED25519 is used in known_hosts file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With