Resolve a hostname¶
Source · io.systemd.Resolve · systemd v257+ · Unprivileged
A tiny resolvectl query: resolve one or more names through systemd-resolved
and print every address. It shows the everyday shape of an sdvarlink program —
dial, call, handle typed errors — and how to fall back when the service is
missing.
Run it¶
Output (illustrative)
github.com 140.82.121.3
systemd.io 185.199.108.153
systemd.io 185.199.109.153
systemd.io 185.199.110.153
systemd.io 185.199.111.153
nope.invalid: no such host
The code¶
How it works¶
resolve.Dial(ctx)connects toresolve.DefaultSocket. If resolved isn't running, or systemd predates the interface, the error matchesvarlink.ErrUnsupported, which the program turns into a friendly message.ResolveHostnametakes aResolveHostnameIn; onlyNameis required. SetFamily: varlink.Ptr(int64(syscall.AF_INET))to ask for IPv4 only.- Typed errors. A name that doesn't exist comes back as
*resolve.NoSuchResourceRecordError;errors.As(orerrors.Is(err, resolve.ErrNoSuchResourceRecord)) separates it from real failures. - Addresses arrive as JSON arrays of byte values (
[]int64), whichtoAddrconverts to anetip.Addr.