How does Go use ssh's hostbased authentication method

Viewed 216

My remote host is enabled HostbasedAuthentication login

cat /etc/ssh/sshd_config

...
# Example of overriding settings on a per-user basis
#Match User anoncvs
#   X11Forwarding no
#   AllowTcpForwarding no
#   PermitTTY no
#   ForceCommand cvs server

UseDNS no
HostbasedAuthentication yes
IgnoreRhosts no

When I login using the command line, it prompts me Authentication succeeded (hostbased)

ssh -v 192.168.1.2

...
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password,hostbased
debug1: Next authentication method: hostbased
debug1: userauth_hostbased: trying hostkey ecdsa-sha2-nistp256 SHA256:nzCIDPxLBPF1qhdmY8yrrZ3DmFzc76oM1Jcx1+BlPRLp
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password,hostbased
debug1: userauth_hostbased: trying hostkey ssh-ed25519 SHA256:DPxLBlPRLp+nzCID+Bbz9KpF1qhdaYJ0oM1JrrZ3Dm
debug1: Authentication succeeded (hostbased).
Authenticated to 192.168.1.2 ([192.168.1.2]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
...

But I get an error when I log in using golang.org/x/crypto/ssh

package main

import (
    "errors"
    "fmt"
    "os"

    "golang.org/x/crypto/ssh"
    "golang.org/x/crypto/ssh/knownhosts"
)

var addr = "192.168.1.2:22"

func main() {
    key, err := os.ReadFile("/etc/ssh/ssh_host_ed25519_key")
    if err != nil {
        panic("read key error," + err.Error())
    }
    signer, err := ssh.ParsePrivateKey(key)
    if err != nil {
        panic("ssh parse private key error," + err.Error())
    }

    check, err := knownhosts.New("/etc/ssh/ssh_known_hosts")
    if err != nil {
        panic(fmt.Errorf("knownhosts error,%s", err.Error()))
    }

    config := &ssh.ClientConfig{
        User:            "root",
        HostKeyCallback: check,
        Auth:            []ssh.AuthMethod{ssh.PublicKeys(signer)},
    }
    client, err := ssh.Dial("tcp", addr, config)
    if err != nil {
        panic(errors.New("ssh new client conn error," + err.Error()))
    }
    _ = client.Close()
}

output:

[root@arm_linux ~]# ./ssh-test 
panic: ssh new client conn error,ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

goroutine 1 [running]:
main.main()
    D:/project/test/main.go:38 +0x354
0 Answers
Related