{"record":{"id":"c12f3d835537e1a5","repo":"kubernetes/kops","slug":"failed-to-get-hostname-w","errorCode":null,"errorMessage":"failed to get hostname: %w","messagePattern":"failed to get hostname: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/toolbox_enroll.go","lineNumber":419,"sourceCode":"\t\t// We send both to stderr, so we don't \"corrupt\" stdout\n\t\tsession.Stdout = io.MultiWriter(os.Stderr, session.Stdout)\n\t\tsession.Stderr = io.MultiWriter(os.Stderr, session.Stderr)\n\t}\n\tif s.sudo {\n\t\tcommand = \"sudo \" + command\n\t}\n\tif err := session.Run(command); err != nil {\n\t\treturn output, fmt.Errorf(\"error running command %q: %w\", command, err)\n\t}\n\treturn output, nil\n}\n\n// getHostname gets the hostname of the SSH target.\n// This is used as the node name when registering the node.\nfunc (s *SSHHost) getHostname(ctx context.Context) (string, error) {\n\toutput, err := s.runCommand(ctx, \"hostname\", ExecOptions{Echo: true})\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to get hostname: %w\", err)\n\t}\n\n\thostname := output.Stdout.String()\n\thostname = strings.TrimSpace(hostname)\n\tif len(hostname) == 0 {\n\t\treturn \"\", fmt.Errorf(\"hostname was empty\")\n\t}\n\treturn hostname, nil\n}\n\ntype BootstrapData struct {\n\t// NodeupScript is a script that can be used to bootstrap the node.\n\tNodeupScript []byte\n\t// NodeupConfig is structured configuration, provided by kops-controller (for example).\n\tNodeupConfig *nodeup.Config\n\t// NodeupScriptAdditionalFiles are additional files that are needed by the nodeup script.\n\tNodeupScriptAdditionalFiles map[string][]byte\n}","sourceCodeStart":401,"sourceCodeEnd":437,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/commands/toolbox_enroll.go#L401-L437","documentation":"getHostname runs `hostname` over SSH and wraps any failure from runCommand. Since the hostname becomes the node name for registration, kOps aborts bootstrap data generation when it cannot be determined. All SSH-layer failures (session open, auth, non-zero exit) surface wrapped here.","triggerScenarios":"buildHostData calls getHostname and the SSH `hostname` command fails: connection dropped, authentication rejected, command exited non-zero, or sudo is misconfigured.","commonSituations":"Wrong SSH credentials/user in enroll flags; host unreachable or firewalled; sshd not running on a freshly provisioned node; sudo not permitted for the enroll user.","solutions":["Test SSH connectivity manually: ssh <user>@<host> hostname","Check SSH user/key and sudo configuration used by kops toolbox enroll","Confirm the node's sshd is up and the security group/firewall allows port 22","Inspect the wrapped cause with errors.Unwrap / %v for the root SSH error"],"exampleFix":"// before\nhostname, err := host.getHostname(ctx) // opaque failure\n// after\nhostname, err := host.getHostname(ctx)\nif err != nil {\n    return fmt.Errorf(\"enroll: check SSH access to %s: %w\", host.addr, err)\n}","handlingStrategy":"try-catch","validationCode":"out, err := exec.Command(\"ssh\", \"-o\", \"BatchMode=yes\", user+\"@\"+host, \"hostname\").Output()\nif err != nil || len(strings.TrimSpace(string(out))) == 0 {\n    return fmt.Errorf(\"cannot determine hostname of %s\", host)\n}","typeGuard":"func isHostnameLookupFailure(err error) bool {\n    return err != nil && strings.Contains(err.Error(), \"failed to get hostname\")\n}","tryCatchPattern":"hostname, err := host.getHostname(ctx)\nif err != nil {\n    return fmt.Errorf(\"verify SSH access/credentials for node enrollment: %w\", err)\n}","preventionTips":["Test SSH access with the exact user/key before enrolling","Confirm sshd is running and port 22 is open","Avoid custom shells/profiles that interfere with non-interactive commands"],"tags":["ssh","hostname"],"backgroundTag":"ssh-command-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}