13

I am running a RHEL 5.7 and the hostname command gives me the correct hostname.
But hostname -s and hostname -f return: Unknown host. Why?

  • What distro? On my Ubuntu 12.04 system all 3 work. – Anthon Oct 2 '14 at 14:31
  • What operating system are you using? If Linux, which distribution? What are the contents of /etc/hostname? – terdon Oct 2 '14 at 14:31
  • 1
    hostname -f uses DNS, do you have a reverse DNS entry for your IP? – Barmar Oct 2 '14 at 14:33
  • @terdon I'm using RHEL5 (red hat enterprise linux server release 5.7 (tikanga)) and there is no such file (/etc/hostname) – redoc Oct 2 '14 at 14:42
  • @Barmar I don't have the dns configured on my host. I added the ip address and fqdn in my hosts file (/etc/hosts), this error is a result of dns not being configured ? – redoc Oct 2 '14 at 14:47
21

(copied from one of my answers on SF)

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 IP address 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).

 

So in short, the likely reason for your issue is that you have no entry in /etc/hosts that contains your hostname (uname -n).


Examples
If your nodename is 'foobar', and you have an entry in /etc/hosts such as this:

127.0.0.1 foobar.example.com foobar localhost.localdomain localhost

Then you will get the following command results:

# hostname
foobar
# uname -n
foobar

# hostname -f
foobar.example.com

# hostname -a
foobar localhost.localdomain localhost
| improve this answer | |
  • That explanation of how it gets the value is great! I had my FQDN at the end of the 127.0.0.1 line which had as primary hostname localhost. I just deleted them and left the FQDN only on the line that has the real IP, and it fixed my problem of hostname -f returning localhost. – qwertzguy Mar 21 '16 at 15:49

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.