Skip to content

Describe the host

Source · io.systemd.Hostname · systemd v257+ · Unprivileged

hostnamectl in 40 lines. One Describe call returns everything systemd-hostnamed knows about the machine: hostnames, chassis, OS, kernel, hardware and firmware. Optional fields are pointers; varlink.Deref reads them without nil checks.

Run it

go run ./examples/hostname
Output (illustrative)
   Static hostname: build-07
Transient hostname: build-07
         Icon name: computer-server
           Chassis: server
  Operating system: Fedora Linux 42 (Server Edition)
            Kernel: Linux 6.14.4-300.fc42.x86_64
   Hardware vendor: Dell Inc.
    Hardware model: PowerEdge R650

The code

examples/hostname/main.go
// Command hostname prints what systemd-hostnamed knows about this machine,
// like `hostnamectl`.
package main

import (
    "context"
    "fmt"
    "log"

    "github.com/rossedman/sdvarlink/systemd/v257/hostname"
    "github.com/rossedman/sdvarlink/varlink"
)

func main() {
    ctx := context.Background()
    c, err := hostname.Dial(ctx)
    if err != nil {
        log.Fatal(err)
    }
    defer c.Close()

    h, err := c.Describe(ctx, hostname.DescribeIn{})
    if err != nil {
        log.Fatal(err)
    }
    row := func(k, v string) {
        if v != "" {
            fmt.Printf("%18s: %s\n", k, v)
        }
    }
    row("Static hostname", varlink.Deref(h.StaticHostname))
    row("Transient hostname", h.Hostname)
    row("Pretty hostname", varlink.Deref(h.PrettyHostname))
    row("Icon name", varlink.Deref(h.IconName))
    row("Chassis", varlink.Deref(h.Chassis))
    row("Operating system", varlink.Deref(h.OperatingSystemPrettyName))
    row("Kernel", h.KernelName+" "+h.KernelRelease)
    row("Hardware vendor", varlink.Deref(h.HardwareVendor))
    row("Hardware model", varlink.Deref(h.HardwareModel))
}

How it works

  • Describe takes a DescribeIn whose only field, AllowInteractiveAuthentication, is optional, so an empty struct is fine.
  • Required vs optional. Hostname, KernelName and KernelRelease are plain strings (always present); StaticHostname, Chassis and friends are *string because a host may not set them.
  • varlink.Deref returns "" for unset fields, and row skips empty values, which mirrors hostnamectl's output.
  • The example pins systemd/v257/hostname. See Versioning for a field in this interface whose type changed between releases.

See also