diff --git a/libvirt/uri/ssh.go b/libvirt/uri/ssh.go index 3fd1ef0e0..a0d69c9b2 100644 --- a/libvirt/uri/ssh.go +++ b/libvirt/uri/ssh.go @@ -162,19 +162,20 @@ func (u *ConnectionURI) dialHost(target string, sshcfg *ssh_config.Config, depth q := u.Query() - port := u.Port() - if port == "" { - port = defaultSSHPort - if sshcfg != nil { - configuredPort, err := sshcfg.Get(target, "Port") - if err == nil && configuredPort != "" { - port = configuredPort - log.Printf("[DEBUG] using ssh port from ssh_config: '%s'", port) - } + // port override order of precedence (starting with highest): + // 1. specific stanza entry in ssh_config for this target (this includes default global entries in ssh config) + // 2. port specified in connection string + // 3. defaultSSHPort + port := defaultSSHPort + if u.Port() != "" { + port = u.Port() + } + if sshcfg != nil { + configuredPort, err := sshcfg.Get(target, "Port") + if err == nil && configuredPort != "" { + log.Printf("[DEBUG] using ssh port from ssh_config: '%s'", configuredPort) + port = configuredPort } - - } else { - log.Printf("[DEBUG] using ssh port from querystring: '%s'", port) } log.Printf("[DEBUG] port for ssh connection is: '%s'", port)