hostname -s switch is now safe for many people, but the situation is messy
Slightly over a decade ago I wrote an entry about our discovery
hostname -s' sometimes did DNS lookups,
depending on the exact version involved. We discovered this the
hard way, when our DNS lookups failed at one point and suddenly
hostname -s' itself started failing unexpectedly. We recently
had a reason to use '
hostname -s' again, which caused me to
remember this old issue and check the current situation. The good
news is that common versions of
hostname now don't do DNS
Well, probably, because it turns out that the Linux situation with
hostname is much more complicated and tangled than I had any idea
before I started looking. It appears that there are no less than
four sources for
hostname, and which version you wind up using
can depend on your Linux. On top of that, the source you're probably
using is distributed in an unusual way that makes it hard for me
to say exactly when its '
hostname -s' became safe. So let's
start with the basics.
If you check with '
rpm -qf /usr/bin/hostname' or '
/usr/bin/hostname' on appropriate systems (Fedora, CentOS, Debian,
and Ubuntu), you will probably find that the version of
you're using comes from a 'hostname' package. This package has no
upstream as such, and no source repository; the canonical source
seems to be the Debian package. Old versions of
its source can be found in its part of debsources. This version has
hostname -s' correctly since somewhere between 2.95
(which doesn't) and 3.04 (which does).
(Based on the information shown in its part of debsources, hostname 2.95 was part of Debian 5.0 (Lenny), released in 2009, and hostname 3.04 was part of Debian 6.0 (Squeeze), released in 2011.)
Arch Linux seems to use a
hostname that comes
from the GNU inetutils
project. The relevant code
currently appears to do a DNS lookup if you use '
-s', but it will
proceed if the DNS lookup fails instead of erroring out (the way
the decade ago
hostname behaved). This does mean that under some
conditions your '
hostname -s' command may stall for some time
while its DNS lookup times out, instead of running essentially
The Linux manpages project
has two manpages online for
hostname (1, 2). The
default one is from net-tools,
and the other one is from GNU coreutils. The GNU Coreutils version
has no '
-s' option (or other commonly supported ones), and as a
result I would be surprised if many Linuxes used it. The net-tools
version is apparently the original upstream of the plain
package version. Based on the Fedora 11 bug report about this, back a decade
ago Fedora was using the net-tools version of
hostname (I don't
know about Debian). The current net-tools version of hostname.c now
bypasses DNS lookups when used with '
-s', a change that was made
(While Fedora still packages net-tools, their package only has a few of its binaries. And apparently net-tools as a whole may be basically unmaintained; the last commits in the repository seem to be from 2018, and it was 2016 when it was particularly actively developed.)