I'm using Ubuntu, and managing my hosts using Puppet. Until a machine or two ago, my hosts resolved to hostname.backend.example.com. The last two machines resolve to hostname.staging.internal. I'm a bit perplexed as to how those names were arrived at.

# /etc/resolv.conf

domain backend.example.com


# /etc/hosts  web01.staging.internal web01     localhost localhost.localdomain

/bin/hostname replies like this:

# hostname -a

# hostname -A
108-166-97-91.static.cloud-ips.com web01.staging.internal 

# hostname -I

There exists a DNS record that maps to web01.backend.example.com. Why, where and how did hostname pick up web01.staging.internal rather than the public DNS record?

1 Answer 1


The hostname command returns results from DNS and /etc/hosts.

hostname is equivilant to uname -n and is the actual "hostname" or "nodename" of the box.
All the other hostname arguments use this nodename to look up info.

So before going any further, I should explain the /etc/hosts file format.
The first field is fairly obvious, its the IP address all the hostnames on the line should resolve to. The second field is the primary hostname for that IP. The remaining fields are aliases.

So if you run hostname -f it will first try to resolve the IP for your nodename. Depending on how you have the hosts: entry configured in /etc/nsswitch.conf this method will vary.

  • If you have it configured to use dns, it will use the search domains configured in /etc/resolv.conf until it gets an IP back from DNS.
  • If you have it configured to use files it will look in /etc/hosts to find a line where either the primary hostname or the alias name is your current nodename (uname -n), and then return the primary hostname in that line.

Once it has the IP it will then try a reverse lookup on that IP. Again it will use DNS for this and your hosts file based on your nsswitch.conf. In the case of using your hosts file, it will return the primary entry (which is the first field after the IP in the file).

hostname -a will only work with the hosts file since doing a reverse lookup in DNS only gives you 1 result. With the hosts file it return the alises in the matching line (which is everything after the first entry, the primary hostname).

If your nodename is 'foobar', and you have an entry in /etc/hosts such as this: foobar.example.com foobar localhost.localdomain localhost

Then you will get the following command results:

# hostname
# uname -n

# hostname -f

# hostname -a
foobar localhost.localdomain localhost
  • Excellent answer. In my case, I had the hostname as one of the alias in /etc/hosts. hostname would find the hostname as one of the aliases, and return the first entry on the line for my host. Thanks! Mar 8, 2012 at 13:49
  • If you have hosts: files dns, will it only use /etc/hosts, or will it try there first and then try DNS? May 23, 2017 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.