1

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
node2.deployment.local

admin@node2$ hostname -f
chef-node2

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

admin@node2$ printenv | grep -i -e "hostname"
HOSTNAME=node2.deployment.local

admin@node2$ cat /etc/environment
http_proxy=http://stupidproxy.itsaywehavetohaveit.local:3128
https_proxy=http://stupidproxy.itsaywehavetohaveit.local:3128
java_home=/applications/jre1.8.0_92/
DISPLAY=127.0.0.1:0.0

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.

Sean Vucich
  • 13
  • 1
  • 5
  • Is `node2` really in `/etc/hosts` as "deplyment"? – G-Man Says 'Reinstate Monica' Nov 10 '16 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. – Sean Vucich Nov 10 '16 at 22:36
  • Order matters! You have the entries in `/etc/hosts` backwards. See http://unix.stackexchange.com/a/77924/4358 (point #4) – phemmer Nov 10 '16 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. – Sean Vucich Nov 11 '16 at 01:10

2 Answers2

1

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.

jayhendren
  • 8,224
  • 2
  • 30
  • 55
  • 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. – Sean Vucich Nov 10 '16 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 '16 at 23:02
  • The output is as follows: `$ grep hosts /etc/nsswitch.conf #hosts: db files nisplus nis dns hosts: files dns myhostname` – Sean Vucich Nov 10 '16 at 23:25
0

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

good:

176.31.91.220 www.mikejonesey.co.uk mike

bad:

176.31.91.220 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.

mikejonesey
  • 1,950
  • 10
  • 17
  • 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? – Sean Vucich Nov 11 '16 at 01: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` – mikejonesey Nov 11 '16 at 01: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. – Sean Vucich Nov 11 '16 at 02:02