I have a CentOS 7 install that is giving me some weird returns when I lookup the hostname of the localmachine. I am used to working with Windows and the hosts file (/etc/hosts in CentOS) so feel like I have this setup correct. I do have a few entries in the hosts file as I am running a group of virtual machines for a test deployment scenario - Hence the extra names defined in there. All the machines in the deployment (3) have the same setup and are returning essentially the same data.

admin@node2$ hostname

admin@node2$ hostname -f

admin@node2$ cat /etc/hosts   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 node1 node1.deployment.local master master.deployment.local node2 node2.deployment.local

admin@node2$ printenv | grep -i -e "hostname"

admin@node2$ cat /etc/environment

What I am trying to figure out is why the system prints the netbios name when running the 'hostname -f' command and prints the FQDN when I run the 'hostname' command. I believe that this is causing me some issue.

Your help understanding and hopefully resolving this is appreciated.

  • Is node2 really in /etc/hosts as "deplyment"? Nov 10, 2016 at 22:24
  • Sorry - No, I have removed that data and replaced with generic (& poorly spelt ) data. I have corrected that to now be consistent. Nov 10, 2016 at 22:36
  • Order matters! You have the entries in /etc/hosts backwards. See unix.stackexchange.com/a/77924/4358 (point #4)
    – phemmer
    Nov 10, 2016 at 23:14
  • OK - I have the understanding of the order - And it resolves my question (Thanks) - To a degree. How is this effected by an interface that is using DHCP? EG: The hosts file will not automatically update this when the IP Address of the interface changes. Nov 11, 2016 at 1:10

2 Answers 2


According to man hostname:

   -f, --fqdn, --long
          DNS host name or FQDN

So hostname -f returns the hostname generated by a reverse DNS lookup. hostname without any arguments just returns whatever's in /etc/hostname.

  • Yes, that much I understand (from the man output). The issues is that $ hostname and $ hostname -f are outputting the opposite of what I am expecting. EG: $ hostname -f returns the netbios name and not the FQDN. Nov 10, 2016 at 22:46
  • Oh, I see what you're asking now. What do you get when you run grep hosts /etc/nsswitch.conf? It's possibly because you have netbios configured as a hostname database.
    – jayhendren
    Nov 10, 2016 at 23:02
  • The output is as follows: $ grep hosts /etc/nsswitch.conf #hosts: db files nisplus nis dns hosts: files dns myhostname Nov 10, 2016 at 23:25

swap the hosts in your /etc/hosts, the format should be long then short.

good: www.mikejonesey.co.uk mike

bad: mike www.mikejonesey.co.uk

hostname will work off this, also I on epel systems i tend to change the domain in /etc/sysconfig/network to be just the hostname, then add the extra def for domain.

  • YES! OK, thank you, the order has resolved the issue. Now, how does this effect a machine that is using DHCP. EG: Should their not be an automatic process somewhere in the system rather than using the hosts file? Nov 11, 2016 at 1:12
  • hosts file will override DNS, but won't effect DHCP, if you have a dhcp server and want the ip of the node to not change then you'll need to add a static ip for the node /etc/dhcp/dhcp.conf. if you're talking about pure host resolution on the machine, there is no auto mechanism for this, just config files /etc/sysconfig/network and /etc/hosts (on epel based systems), some unix os also have /etc/hostname (which should not contain the domain), the domain should always be added as a seperate param in /etc/sysconfig/network; HOSTNAME=bob and DOMAIN=example.com for bob.example.com Nov 11, 2016 at 1:44
  • OK - I will work with the /etc/sysconfig/network file as I don't want to have these systems on static IP' to ensure I replicate the environment correctly. Nov 11, 2016 at 2:02

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.