kubernetes/kops · error

hostname was empty

Error message

hostname was empty

What it means

After running `hostname` over SSH, getHostname trims the output and fails if it is empty. A node cannot be registered without a name, so kOps returns this explicit validation error rather than using an empty node name.

Source

Thrown at pkg/commands/toolbox_enroll.go:425

	}
	if err := session.Run(command); err != nil {
		return output, fmt.Errorf("error running command %q: %w", command, err)
	}
	return output, nil
}

// getHostname gets the hostname of the SSH target.
// This is used as the node name when registering the node.
func (s *SSHHost) getHostname(ctx context.Context) (string, error) {
	output, err := s.runCommand(ctx, "hostname", ExecOptions{Echo: true})
	if err != nil {
		return "", fmt.Errorf("failed to get hostname: %w", err)
	}

	hostname := output.Stdout.String()
	hostname = strings.TrimSpace(hostname)
	if len(hostname) == 0 {
		return "", fmt.Errorf("hostname was empty")
	}
	return hostname, nil
}

type BootstrapData struct {
	// NodeupScript is a script that can be used to bootstrap the node.
	NodeupScript []byte
	// NodeupConfig is structured configuration, provided by kops-controller (for example).
	NodeupConfig *nodeup.Config
	// NodeupScriptAdditionalFiles are additional files that are needed by the nodeup script.
	NodeupScriptAdditionalFiles map[string][]byte
}

// ConfigBuilder builds bootstrap configuration for a node.
type ConfigBuilder struct {
	// ClusterName is the name of the cluster to build.
	// Required (unless Cluster is set).
	ClusterName string

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Run `hostname` manually over SSH as the same user and confirm non-empty output
  2. Check the target's shell rc/profile for aliases or stty/echo tricks affecting output
  3. Set the system hostname on the node (hostnamectl set-hostname <name>) and retry
  4. Use a fallback explicit node name if the tooling allows specifying it

Example fix

// before
// relies on remote `hostname` output
// after
$ ssh user@host 'hostname; echo EXIT=$?'  # verify non-empty output and exit 0
$ sudo hostnamectl set-hostname node-1
Defensive patterns

Strategy: validation

Validate before calling

out, _ := exec.Command("ssh", user+"@"+host, "hostname").Output()
if len(strings.TrimSpace(string(out))) == 0 {
    return errors.New("remote hostname is empty; set it with hostnamectl")
}

Type guard

func hasValidRemoteHostname(out *CommandOutput) bool {
    return out != nil && strings.TrimSpace(out.Stdout.String()) != ""
}

Prevention

When it happens

Trigger: The remote `hostname` command succeeds but writes nothing to stdout — e.g. a shell profile suppresses output, a non-standard shell/alias overrides `hostname`, or the SSH server behaves abnormally.

Common situations: Custom shell rc files printing to stderr but the command output being misdirected; restricted shells (rbash) intercepting commands; running `hostname` against an appliance/non-standard SSH target.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/1c54c9a3d47fd8a7. Report an issue: GitHub.