Beware of using Linux's hostname -s switch

February 10, 2010

The hostname program has a common switch, -s, which is documented (in the Linux version) as:

-s, --short
Display the short host name. This is the host name cut at the first dot.

Although you would not expect it from this description, running 'hostname -s' will do a gethostbyname() and thus often a DNS lookup in most Linux versions of hostname. This can of course fail if your DNS is not working, which winds up with the very peculiar result of hostname failing. And all of this because you innocently decided to trim out any dots that might be present using the most obvious and easiest approach.

(Most scripts don't cope very well with this, partly because the Bourne shell makes it annoyingly difficult to deal with programs failing in command substitutions and partly because come on, who expects hostname to fail?)

Red Hat Enterprise 5, Fedora 8, Ubuntu 6.06 and Ubuntu 8.04 have versions of hostname that behave this way. Fedora 11 has a version that does not, because someone filed a bug about it; unfortunately I can't tell if this has been fixed upstream or if an upstream bug has been filed (or if it would be useful to do so).

The sad conclusion is that for the next several years, if you need the local hostname without any dots on it you should write something like:

hostname | sed 's/\..*//'

instead of using the shorter, nicer hostname -s.

(We found this out the hard way last night, when we had some sort of network issue that made our DNS servers unreachable to some machines while some of our status check scripts were running.)


Comments on this page:

From 68.55.184.240 at 2010-02-10 13:37:10:

An alternative if you're using bash:

HOST=`hostname`
echo ${HOST%%.*}

The %%.* strips off everything from the first dot onwards. This saves on starting another process.

From 128.211.177.10 at 2010-02-10 15:26:10:

The other caution is if you're used to using `hostname -s` on Linux and then you switch to Solaris. At least on Solaris 9, under just the wrong conditions you can blank you hostname which causes all sorts of problems (fortunately easily fixed via reboot).

From 207.5.168.94 at 2010-02-10 17:26:32:

Wouldn't this get read from /etc/hosts first?

By cks at 2010-02-10 17:42:34:

Most of our machines don't have their own name in /etc/hosts; they just have localhost. Among other things, this avoids a lot of heartburn if you want to rename or renumber machines.

From 114.45.169.31 at 2010-02-10 18:56:46:

unfortunately I can't tell if this has been fixed upstream or if an upstream bug has been filed (or if it would be useful to do so).

I looked into this. net-tools upstream hasn't made a release since Apr 2001 (1.60) and the various Linux distributions have been maintaining their own patch sets since.

So I think there is no "upstream bug tracker" to file this bug and you could: 1. File bugs with Debian, Ubuntu and openSUSE or 2. Become the new net-tools upstream and start pulling in the accumulated patches from Linux distributions.

Scott Tsai <scottt.tw AT gmail.com>

From 66.92.52.243 at 2010-02-11 10:41:53:

Ran into this when porting a lot of our software from Linux to Solaris. Nice little gotcha. There was a bug in Sys::Hostname or Sys::Hostname::Long that did the same thing. --bda

Written on 10 February 2010.
« Some thoughts about 6to4
Forcing your webpage content to scroll is generally a bad idea »

Page tools: View Source, View Normal, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Wed Feb 10 11:59:16 2010
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.